Skip to content
Snippets Groups Projects
Select Git revision
  • 71703e6145bf2daf87208e71dadc4313a7fc6fce
  • master default protected
  • 1.31
  • 4.25.2
  • 4.25.1
  • 4.25.0
  • 4.24.3
  • 4.24.2
  • 4.24.1
  • 4.24.0
  • 4.23.6
  • 4.23.5
  • 4.23.4
  • 4.23.3
  • 4.23.2
  • 4.23.1
  • 4.23.0
  • 4.22.3
  • 4.22.2
  • 4.22.1
  • 4.22.0
  • 4.21.0
  • 4.20.1
23 results

Monster_Constraints.html

Blame
  • middleware.go 1.34 KiB
    package httpbin
    
    import (
    	"fmt"
    	"log"
    	"net/http"
    )
    
    func logger(h http.Handler) http.Handler {
    	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    		h.ServeHTTP(w, r)
    		log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
    	})
    }
    
    func cors(h http.Handler) http.Handler {
    	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    		origin := r.Header.Get("Origin")
    		if origin == "" {
    			origin = "*"
    		}
    		respHeader := w.Header()
    		respHeader.Set("Access-Control-Allow-Origin", origin)
    		respHeader.Set("Access-Control-Allow-Credentials", "true")
    
    		if r.Method == "OPTIONS" {
    			respHeader.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
    			respHeader.Set("Access-Control-Max-Age", "3600")
    			if r.Header.Get("Access-Control-Request-Headers") != "" {
    				respHeader.Set("Access-Control-Allow-Headers", r.Header.Get("Access-Control-Request-Headers"))
    			}
    		}
    		h.ServeHTTP(w, r)
    	})
    }
    
    func methods(h http.HandlerFunc, methods ...string) http.HandlerFunc {
    	methodMap := make(map[string]struct{}, len(methods))
    	for _, m := range methods {
    		methodMap[m] = struct{}{}
    	}
    	return func(w http.ResponseWriter, r *http.Request) {
    		if _, ok := methodMap[r.Method]; !ok {
    			http.Error(w, fmt.Sprintf("method %s not allowed", r.Method), http.StatusMethodNotAllowed)
    			return
    		}
    		h.ServeHTTP(w, r)
    	}
    }