From 9a15c7765334747473bc0e8a899122051ef10bc2 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 1 Nov 2025 19:32:58 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9E=96=20Remove=20dependency=20on=20github.c?= =?UTF-8?q?om/julienschmidt/httprouter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 -- go.sum | 2 -- main.go | 54 ++++++++++++++++++++---------------------------------- 3 files changed, 20 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index 432e1e0..af8254e 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module codeberg.org/danjones000/http-go-status go 1.21.4 - -require github.com/julienschmidt/httprouter v1.3.0 diff --git a/go.sum b/go.sum index 096c54e..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +0,0 @@ -github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= -github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= diff --git a/main.go b/main.go index 90e65e7..65e9301 100644 --- a/main.go +++ b/main.go @@ -23,15 +23,14 @@ import ( "os" "strconv" "strings" - - "github.com/julienschmidt/httprouter" ) -func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func Status(w http.ResponseWriter, r *http.Request) { header := w.Header() header.Set("Access-Control-Allow-Origin", "*") - status := ps.ByName("status") + status := strings.TrimLeft(r.RequestURI, "/") + status, _, _ = strings.Cut(status, "/") fmt.Printf("Got %s status\n", status) if status == "" { status = "200" @@ -52,37 +51,18 @@ func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.WriteHeader(ret) } -func GetRouter() *httprouter.Router { - router := httprouter.New() - - // We don't handle OPTIONS, so that we can support CORS - methods := [8]string{ - http.MethodGet, - http.MethodHead, - http.MethodPost, - http.MethodPut, - http.MethodPatch, - http.MethodDelete, - http.MethodConnect, - http.MethodTrace} - for _, method := range methods { - router.Handle(method, "/:status", Status) +func HandleCors(w http.ResponseWriter, r *http.Request) { + fmt.Println("handling CORS") + if r.Header.Get("Access-Control-Request-Method") != "" { + // Set CORS headers + header := w.Header() + header.Set("Access-Control-Allow-Methods", "*") + header.Set("Access-Control-Allow-Origin", "*") + header.Set("Access-Control-Allow-Headers", "*") } - // Handle CORS preflight requests - router.GlobalOPTIONS = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("Access-Control-Request-Method") != "" { - // Set CORS headers - header := w.Header() - header.Set("Access-Control-Allow-Methods", header.Get("Allow")) - header.Set("Access-Control-Allow-Origin", "*") - } - - // Adjust status code to 204 - w.WriteHeader(http.StatusNoContent) - }) - - return router + // Adjust status code to 204 + w.WriteHeader(http.StatusNoContent) } func GetPort() string { @@ -100,5 +80,11 @@ func GetPort() string { func main() { port := GetPort() fmt.Printf("Listening on %s\n", port) - log.Fatal(http.ListenAndServe(port, GetRouter())) + log.Fatal(http.ListenAndServe(port, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodOptions { + HandleCors(w, r) + } else { + Status(w, r) + } + }))) }