aboutsummaryrefslogblamecommitdiff
path: root/cors.go
blob: a05e6d5dba645036edbf9a1b65c645751df9f8de (plain) (tree)





























                                                                                
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)
		}
	})
}