Skip to content
Snippets Groups Projects
Select Git revision
  • 729c7706c0f98c049965840d1996259550b81479
  • main default protected
  • drip-server-timing
  • compress-middleware
  • v2.11.0
  • v2.10.0
  • v2.9.2
  • v2.9.1
  • v2.9.0
  • v2.8.0
  • v2.7.0
  • v2.6.0
  • v2.5.6
  • v2.5.5
  • v2.5.4
  • v2.5.3
  • v2.5.2
  • v2.5.1
  • v2.5.0
  • v2.4.2
  • v2.4.1
  • v2.4.0
  • v2.3.0
  • v2.2.2
24 results

middleware.go

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