Skip to content

Commit

Permalink
Sentry CORS config (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn authored Apr 12, 2024
1 parent f959ef2 commit c305811
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/sidecar_query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (sqs *sideCarQueryServer) Start(context.Context) error {
func NewSideCarQueryServer(appCodec codec.Codec, config domain.Config, logger log.Logger) (SideCarQueryServer, error) {
// Setup echo server
e := echo.New()
middleware := middleware.InitMiddleware()
middleware := middleware.InitMiddleware(config.CORS)
e.Use(middleware.CORS)
e.Use(middleware.InstrumentMiddleware)
e.Use(middleware.TraceWithParamsMiddleware("sqs"))
Expand Down
5 changes: 5 additions & 0 deletions config-testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
"traces-sample-rate": 1,
"profiles-sample-rate": 1,
"environment" : "production"
},
"cors": {
"allowed-headers": "Origin, Accept, Content-Type, X-Requested-With, X-Server-Time, Origin, Accept, Content-Type, X-Requested-With, X-Server-Time, Accept-Encoding, sentry-trace, baggage",
"allowed-methods": "HEAD, GET, POST, HEAD, GET, POST, DELETE, OPTIONS, PATCH, PUT",
"allowed-origins": "*"
}
}

5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
"traces-sample-rate": 1,
"profiles-sample-rate": 1,
"environment" : "production"
},
"cors": {
"allowed-headers": "Origin, Accept, Content-Type, X-Requested-With, X-Server-Time, Origin, Accept, Content-Type, X-Requested-With, X-Server-Time, Accept-Encoding, sentry-trace, baggage",
"allowed-methods": "HEAD, GET, POST, HEAD, GET, POST, DELETE, OPTIONS, PATCH, PUT",
"allowed-origins": "*"
}
}

17 changes: 17 additions & 0 deletions domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ type Config struct {
GRPCIngester *GRPCIngesterConfig `mapstructure:"grpc-ingester"`

OTEL *OTELConfig `mapstructure:"otel"`

CORS *CORSConfig `mapstructure:"cors"`
}

type OTELConfig struct {
DSN string `mapstructure:"dsn"`
SampleRate float64 `mapstructure:"sample-rate"`
EnableTracing bool `mapstructure:"enable-tracing"`
TracesSampleRate float64 `mapstructure:"traces-sample-rate"`
ProfilesSampleRate float64 `mapstructure:"profiles-sample-rate"`
Environment string `mapstructure:"environment"`
}

type CORSConfig struct {
AllowedHeaders string `mapstructure:"allowed-headers"`
AllowedMethods string `mapstructure:"allowed-methods"`
AllowedOrigin string `mapstructure:"allowed-origin"`
}
9 changes: 0 additions & 9 deletions domain/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ type PoolsConfig struct {
GeneralCosmWasmCodeIDs []uint64 `mapstructure:"general-cosmwasm-code-ids"`
}

type OTELConfig struct {
DSN string `mapstructure:"dsn"`
SampleRate float64 `mapstructure:"sample-rate"`
EnableTracing bool `mapstructure:"enable-tracing"`
TracesSampleRate float64 `mapstructure:"traces-sample-rate"`
ProfilesSampleRate float64 `mapstructure:"profiles-sample-rate"`
Environment string `mapstructure:"environment"`
}

const DisableSplitRoutes = 0

type RouterState struct {
Expand Down
13 changes: 8 additions & 5 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// GoMiddleware represent the data-struct for middleware
type GoMiddleware struct {
// another stuff , may be needed by middleware
corsConfig domain.CORSConfig
}

var (
Expand Down Expand Up @@ -47,15 +47,18 @@ func init() {
// CORS will handle the CORS middleware
func (m *GoMiddleware) CORS(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set("Access-Control-Allow-Origin", "*")
c.Response().Header().Set("Access-Control-Allow-Headers", "sentry-trace, baggage")
c.Response().Header().Set("Access-Control-Allow-Origin", m.corsConfig.AllowedOrigin)
c.Response().Header().Set("Access-Control-Allow-Headers", m.corsConfig.AllowedHeaders)
c.Response().Header().Set("Access-Control-Allow-Methods", m.corsConfig.AllowedMethods)
return next(c)
}
}

// InitMiddleware initialize the middleware
func InitMiddleware() *GoMiddleware {
return &GoMiddleware{}
func InitMiddleware(corsConfig *domain.CORSConfig) *GoMiddleware {
return &GoMiddleware{
corsConfig: *corsConfig,
}
}

// InstrumentMiddleware will handle the instrumentation middleware
Expand Down

0 comments on commit c305811

Please sign in to comment.