handle CORS

This commit is contained in:
Gabriel Augendre 2024-09-16 17:57:46 +02:00
parent 4ec08222cc
commit 1900e99a93
4 changed files with 19 additions and 7 deletions

View file

@ -7,8 +7,9 @@ build-linux: out
GOOS=linux GOARCH=amd64 go build -o ./out/lyon-transports-linux-amd64 ./ GOOS=linux GOARCH=amd64 go build -o ./out/lyon-transports-linux-amd64 ./
deploy: build-linux deploy: build-linux
scp ./out/lyon-transports-linux-amd64 ubuntu:/mnt/data/lyon-transports/ ssh ubuntu-no-tmux "sudo systemctl stop lyon-transports.service"
ssh ubuntu-no-tmux "sudo systemctl restart 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: out:
mkdir -p out mkdir -p out

2
go.mod
View file

@ -5,6 +5,8 @@ go 1.23.1
require ( require (
github.com/carlmjohnson/requests v0.24.2 github.com/carlmjohnson/requests v0.24.2
github.com/danielgtaylor/huma/v2 v2.22.1 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 github.com/jarcoal/httpmock v1.3.1
golang.org/x/text v0.18.0 golang.org/x/text v0.18.0
gotest.tools/v3 v3.5.1 gotest.tools/v3 v3.5.1

6
go.sum
View file

@ -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/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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= 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 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=

13
main.go
View file

@ -5,8 +5,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/danielgtaylor/huma/v2" "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/danielgtaylor/huma/v2/humacli"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"net/http" "net/http"
"time" "time"
) )
@ -16,6 +18,7 @@ type Options struct {
Port int `help:"Port to listen on" default:"8888"` Port int `help:"Port to listen on" default:"8888"`
GrandLyonUsername string `help:"Grand Lyon username" short:"u" required:"true"` GrandLyonUsername string `help:"Grand Lyon username" short:"u" required:"true"`
GrandLyonPassword string `help:"Grand Lyon password" short:"p" required:"true"` GrandLyonPassword string `help:"Grand Lyon password" short:"p" required:"true"`
CORSAllowedOrigin string `help:"CORS allowed origin"`
} }
type statusOutput struct { type statusOutput struct {
@ -80,8 +83,12 @@ func main() {
// Create a CLI app which takes a port option. // Create a CLI app which takes a port option.
cli := humacli.New(func(hooks humacli.Hooks, options *Options) { cli := humacli.New(func(hooks humacli.Hooks, options *Options) {
// Create a new router & API // Create a new router & API
router := http.NewServeMux() router := chi.NewRouter()
api := humago.New(router, huma.DefaultConfig("My API", "1.0.0")) router.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{options.CORSAllowedOrigin},
}))
api := humachi.New(router, huma.DefaultConfig("My API", "1.0.0"))
server := http.Server{ server := http.Server{
Addr: fmt.Sprintf("%s:%d", options.Host, options.Port), Addr: fmt.Sprintf("%s:%d", options.Host, options.Port),
Handler: router, Handler: router,