diff --git a/app.env b/app.env index d2400f27..8bd7f24e 100644 --- a/app.env +++ b/app.env @@ -1,4 +1,5 @@ ENVIRONMENT=development +ALLOWED_ORIGINS=http://localhost:3000,https://simplebank.com DB_SOURCE=postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable MIGRATION_URL=file://db/migration HTTP_SERVER_ADDRESS=0.0.0.0:8080 diff --git a/go.mod b/go.mod index 495eedc3..8ff1ad77 100644 --- a/go.mod +++ b/go.mod @@ -57,6 +57,7 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/robfig/cron/v3 v3.0.1 // indirect + github.com/rs/cors v1.10.1 // indirect github.com/spf13/afero v1.8.2 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index 2684507e..439bd9d3 100644 --- a/go.sum +++ b/go.sum @@ -891,6 +891,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo= +github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= diff --git a/main.go b/main.go index 6f111ae8..3fbbbddf 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "github.com/hibiken/asynq" "github.com/jackc/pgx/v5/pgxpool" "github.com/rakyll/statik/fs" + "github.com/rs/cors" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/techschool/simplebank/api" @@ -205,8 +206,27 @@ func runGatewayServer( swaggerHandler := http.StripPrefix("/swagger/", http.FileServer(statikFS)) mux.Handle("/swagger/", swaggerHandler) + c := cors.New(cors.Options{ + AllowedOrigins: config.AllowedOrigins, + AllowedMethods: []string{ + http.MethodHead, + http.MethodOptions, + http.MethodGet, + http.MethodPost, + http.MethodPut, + http.MethodPatch, + http.MethodDelete, + }, + AllowedHeaders: []string{ + "Content-Type", + "Authorization", + }, + AllowCredentials: true, + }) + handler := c.Handler(gapi.HttpLogger(mux)) + httpServer := &http.Server{ - Handler: gapi.HttpLogger(mux), + Handler: handler, Addr: config.HTTPServerAddress, } diff --git a/util/config.go b/util/config.go index 292fe196..59abd808 100644 --- a/util/config.go +++ b/util/config.go @@ -10,6 +10,7 @@ import ( // The values are read by viper from a config file or environment variable. type Config struct { Environment string `mapstructure:"ENVIRONMENT"` + AllowedOrigins []string `mapstructure:"ALLOWED_ORIGINS"` DBSource string `mapstructure:"DB_SOURCE"` MigrationURL string `mapstructure:"MIGRATION_URL"` RedisAddress string `mapstructure:"REDIS_ADDRESS"`