aboutsummaryrefslogtreecommitdiff
path: root/cors.go
blob: a05e6d5dba645036edbf9a1b65c645751df9f8de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main

import (
	"net/http"
)

type CorsAllowAllOrigins struct {
	AndThen http.Handler
}

func (c CorsAllowAllOrigins) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Access-Control-Allow-Origin", "*")
	w.Header().Add("Access-Control-Allow-Methods", "*")
	w.Header().Add("Access-Control-Allow-Headers", "*")
	c.AndThen.ServeHTTP(w, r)
}

type OptionsNoError struct {
	Error ErrorHandler
}

func (c OptionsNoError) WithError(err error) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "OPTIONS" {
			w.WriteHeader(200)
		} else {
			c.Error.WithError(err).ServeHTTP(w, r)
		}
	})
}