This commit is contained in:
Daniel 2024-08-14 19:12:20 -04:00
parent 7638564b81
commit 9d4468d3cf
5 changed files with 21 additions and 0 deletions

View file

@ -1,6 +1,10 @@
db: db:
driver: pgx driver: pgx
connect: 'postgres://postgres:password@localhost:5432/example' connect: 'postgres://postgres:password@localhost:5432/example'
cors:
allowedOrigins:
- 'http://localhost:*'
- 'https://stillbox.server'
auth: auth:
jwtsecret: 'super secret string' jwtsecret: 'super secret string'
# this is the JWT cookie domain # this is the JWT cookie domain

1
go.mod
View file

@ -7,6 +7,7 @@ require (
github.com/go-audio/wav v1.1.0 github.com/go-audio/wav v1.1.0
github.com/go-chi/chi v1.5.5 github.com/go-chi/chi v1.5.5
github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/cors v1.2.1
github.com/go-chi/httprate v0.9.0 github.com/go-chi/httprate v0.9.0
github.com/go-chi/jwtauth/v5 v5.3.1 github.com/go-chi/jwtauth/v5 v5.3.1
github.com/go-chi/render v1.0.3 github.com/go-chi/render v1.0.3

2
go.sum
View file

@ -36,6 +36,8 @@ github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw= github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= 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/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/go-chi/httprate v0.9.0 h1:21A+4WDMDA5FyWcg7mNrhj63aNT8CGh+Z1alOE/piU8= github.com/go-chi/httprate v0.9.0 h1:21A+4WDMDA5FyWcg7mNrhj63aNT8CGh+Z1alOE/piU8=
github.com/go-chi/httprate v0.9.0/go.mod h1:6GOYBSwnpra4CQfAKXu8sQZg+nZ0M1g9QnyFvxrAB8A= github.com/go-chi/httprate v0.9.0/go.mod h1:6GOYBSwnpra4CQfAKXu8sQZg+nZ0M1g9QnyFvxrAB8A=
github.com/go-chi/jwtauth/v5 v5.3.1 h1:1ePWrjVctvp1tyBq5b/2ER8Th/+RbYc7x4qNsc5rh5A= github.com/go-chi/jwtauth/v5 v5.3.1 h1:1ePWrjVctvp1tyBq5b/2ER8Th/+RbYc7x4qNsc5rh5A=

View file

@ -10,6 +10,7 @@ import (
type Config struct { type Config struct {
DB DB `yaml:"db"` DB DB `yaml:"db"`
CORS CORS `yaml:"cors"`
Auth Auth `yaml:"auth"` Auth Auth `yaml:"auth"`
Listen string `yaml:"listen"` Listen string `yaml:"listen"`
Public bool `yaml:"public"` Public bool `yaml:"public"`
@ -23,6 +24,10 @@ type Auth struct {
AllowInsecure map[string]bool `yaml:"allowInsecureFor"` AllowInsecure map[string]bool `yaml:"allowInsecureFor"`
} }
type CORS struct {
AllowedOrigins []string `yaml:"allowedOrigins"`
}
type DB struct { type DB struct {
Connect string `yaml:"connect"` Connect string `yaml:"connect"`
Driver string `yaml:"driver"` Driver string `yaml:"driver"`

View file

@ -12,6 +12,7 @@ import (
"dynatron.me/x/stillbox/pkg/gordio/sources" "dynatron.me/x/stillbox/pkg/gordio/sources"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
) )
type Server struct { type Server struct {
@ -48,6 +49,14 @@ func New(cfg *config.Config) (*Server, error) {
r.Use(middleware.RealIP) r.Use(middleware.RealIP)
r.Use(middleware.Logger) r.Use(middleware.Logger)
r.Use(middleware.Recoverer) r.Use(middleware.Recoverer)
r.Use(cors.Handler(cors.Options{
AllowedOrigins: srv.conf.CORS.AllowedOrigins,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "Upgrade"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
srv.setupRoutes() srv.setupRoutes()
return srv, nil return srv, nil