From 1900e99a93988b765876873971d5c4ce108b795b Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 16 Sep 2024 17:57:46 +0200 Subject: [PATCH] handle CORS --- Makefile | 5 +++-- go.mod | 2 ++ go.sum | 6 ++++-- main.go | 13 ++++++++++--- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 3c8c48b..7af342a 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,9 @@ build-linux: out GOOS=linux GOARCH=amd64 go build -o ./out/lyon-transports-linux-amd64 ./ deploy: build-linux - scp ./out/lyon-transports-linux-amd64 ubuntu:/mnt/data/lyon-transports/ - ssh ubuntu-no-tmux "sudo systemctl restart lyon-transports.service" + ssh ubuntu-no-tmux "sudo systemctl stop lyon-transports.service" + scp ./out/lyon-transports-linux-amd64 ubuntu-no-tmux:/mnt/data/lyon-transports/ + ssh ubuntu-no-tmux "sudo systemctl start lyon-transports.service" out: mkdir -p out diff --git a/go.mod b/go.mod index 77e8c93..760a445 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,8 @@ go 1.23.1 require ( github.com/carlmjohnson/requests v0.24.2 github.com/danielgtaylor/huma/v2 v2.22.1 + github.com/go-chi/chi/v5 v5.1.0 + github.com/go-chi/cors v1.2.1 github.com/jarcoal/httpmock v1.3.1 golang.org/x/text v0.18.0 gotest.tools/v3 v3.5.1 diff --git a/go.sum b/go.sum index c737e1f..dfc512c 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,10 @@ github.com/danielgtaylor/huma/v2 v2.22.1 h1:fXhyjGSj5u5VeI+laa+e+7OxiQsP9RC55/tW github.com/danielgtaylor/huma/v2 v2.22.1/go.mod h1:2NZmGf/A+SstJYQlq0Xp4nsTDCmPvKS2w9vI8c9sf1A= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= -github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= +github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= diff --git a/main.go b/main.go index 4beb089..ca56dfc 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,10 @@ import ( "errors" "fmt" "github.com/danielgtaylor/huma/v2" - "github.com/danielgtaylor/huma/v2/adapters/humago" + "github.com/danielgtaylor/huma/v2/adapters/humachi" "github.com/danielgtaylor/huma/v2/humacli" + "github.com/go-chi/chi/v5" + "github.com/go-chi/cors" "net/http" "time" ) @@ -16,6 +18,7 @@ type Options struct { Port int `help:"Port to listen on" default:"8888"` GrandLyonUsername string `help:"Grand Lyon username" short:"u" required:"true"` GrandLyonPassword string `help:"Grand Lyon password" short:"p" required:"true"` + CORSAllowedOrigin string `help:"CORS allowed origin"` } type statusOutput struct { @@ -80,8 +83,12 @@ func main() { // Create a CLI app which takes a port option. cli := humacli.New(func(hooks humacli.Hooks, options *Options) { // Create a new router & API - router := http.NewServeMux() - api := humago.New(router, huma.DefaultConfig("My API", "1.0.0")) + router := chi.NewRouter() + router.Use(cors.Handler(cors.Options{ + AllowedOrigins: []string{options.CORSAllowedOrigin}, + })) + + api := humachi.New(router, huma.DefaultConfig("My API", "1.0.0")) server := http.Server{ Addr: fmt.Sprintf("%s:%d", options.Host, options.Port), Handler: router,