Example Usage
package main
import (
"io"
"log"
"net/http"
"github.com/jub0bs/cors"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /hello", handleHello) // note: not configured for CORS
// create CORS middleware
cors, err := cors.NewMiddleware(cors.Config{
Origins: []string{"https://example.com"},
Methods: []string{http.MethodGet, http.MethodPost},
RequestHeaders: []string{"Authorization"},
})
if err != nil {
log.Fatal(err)
}
cors.SetDebug(true) // turn debug mode on (optional)
api := http.NewServeMux()
api.HandleFunc("GET /users", handleUsersGet)
api.HandleFunc("POST /users", handleUsersPost)
mux.Handle("/api/", http.StripPrefix("/api", cors.Wrap(api))) // note: method-less pattern here
if err := http.ListenAndServe(":8080", mux); err != http.ErrServerClosed {
log.Fatal(err)
}
}
func handleHello(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "Hello, World!")
}
func handleUsersGet(_ http.ResponseWriter, _ *http.Request) {
// omitted
}
func handleUsersPost(_ http.ResponseWriter, _ *http.Request) {
// omitted
}
See also
Corsy
Open Source
Lightweight program to find all known misconfigurations in CORS (Cross-Origin Resource Sharing) impl…
Aquatone
Open Source
Tool for visual inspection of websites across large numbers of hosts. Takes screenshots of web pages…
OWASP ZAP
Open Source
Kali
OWASP's Zed Attack Proxy — one of the world's most popular free web application security scanners. F…
Evilginx2
Open Source
Standalone man-in-the-middle attack framework that bypasses 2FA by proxying authentication sessions …
articles-translator
Open Source
:books:Translate the distinct technical blogs. Please star or watch. Welcome to join me.…
AllHackingTools
Open Source
All-in-One Hacking Tools For Hackers! And more hacking tools! For termux.…