diff --git a/api/app.go b/api/app.go index fc00907..2a2e5bb 100644 --- a/api/app.go +++ b/api/app.go @@ -270,7 +270,7 @@ func (app *App) loadConfiguration() error { return nil } -//OnErrorHandler handles panics +// OnErrorHandler handles panics func (app *App) OnErrorHandler(err error, stack []byte) { app.Logger.Error( "Panic occurred.", @@ -343,7 +343,7 @@ func (app *App) createLeaderboardClient() lservice.Leaderboard { return leaderboardService } -//AddError rate statistics +// AddError rate statistics func (app *App) AddError() { app.Errors.Update(1) } @@ -463,7 +463,7 @@ func (app *App) startHTTPServer(ctx context.Context, lis net.Listener) error { } mux := http.NewServeMux() - mux.Handle("/", removeTrailingSlashMiddleware{addVersionMiddleware{gatewayMux}}) + mux.Handle("/", removeTrailingSlashMiddleware{addVersionMiddleware{addCorsMiddleware{gatewayMux}}}) mux.HandleFunc("/healthcheck", addVersionHandlerFunc(app.healthCheckHandler)) mux.HandleFunc("/status", addVersionHandlerFunc(app.statusHandler)) attachSpan := func(span opentracing.Span, r *http.Request) { diff --git a/api/middleware.go b/api/middleware.go index 11cf3e7..7c06e48 100644 --- a/api/middleware.go +++ b/api/middleware.go @@ -94,7 +94,7 @@ func (app *App) loggerMiddleware(ctx context.Context, req interface{}, info *grp return h, err } -//Serve executes on error handler when errors happen +// Serve executes on error handler when errors happen func (app *App) recoveryMiddleware(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { defer func() { if err := recover(); err != nil { @@ -197,3 +197,34 @@ func (m removeTrailingSlashMiddleware) ServeHTTP(w http.ResponseWriter, r *http. r.URL.Path = m.removeTrailingSlash(r.URL.Path) m.Handler.ServeHTTP(w, r) } + +type addCorsMiddleware struct { + Handler http.Handler +} + +func addCorsHeaders(w http.ResponseWriter) { + w.Header().Set("Access-Control-Allow-Origin", "*") + /* + w.Header().Set("Vary", "Origin") + w.Header().Set("Vary", "Access-Control-Request-Method") + w.Header().Set("Vary", "Access-Control-Request-Headers") + */ + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") + w.Header().Set("Access-Control-Allow-Methods", "GET,PATCH,POST,PUT,OPTIONS,DELETE") +} + +func (m addCorsMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { + addCorsHeaders(w) + if r.Method == "OPTIONS" { + w.WriteHeader(http.StatusOK) + } else { + m.Handler.ServeHTTP(w, r) + } +} + +func addCorsHandlerFunc(f func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + addCorsHeaders(w) + f(w, r) + } +} diff --git a/build/Dockerfile b/build/Dockerfile index 8e7a45f..519ffe2 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -4,7 +4,7 @@ # http://www.opensource.org/licenses/mit-license # Copyright © 2019 Top Free Games -FROM golang:1.16.3-alpine as build +FROM golang:1.17-alpine as build MAINTAINER TFG Co @@ -21,4 +21,4 @@ COPY --from=build /podium/config/default.yaml /default.yaml RUN chmod +x /podium -ENTRYPOINT ["/podium", "-c", "/default.yaml"] +ENTRYPOINT ["/podium", "-c", "/default.yaml", "start"] diff --git a/go.sum b/go.sum index 255a4b4..7c594fd 100644 --- a/go.sum +++ b/go.sum @@ -307,8 +307,6 @@ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69 github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/topfreegames/extensions v8.0.2+incompatible h1:4LMv5SBpep8WkUVGmgpGg0TYzYfydWktrh1YW60KAwU= github.com/topfreegames/extensions v8.0.2+incompatible/go.mod h1:VbIAks7aNbkANieZyVShAV3FSxEbV6useS4r3neXVzc= -github.com/uber/jaeger-client-go v2.25.0+incompatible h1:IxcNZ7WRY1Y3G4poYlx24szfsn/3LvK9QHCq9oQw8+U= -github.com/uber/jaeger-client-go v2.25.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= github.com/uber/jaeger-client-go v2.29.1+incompatible h1:R9ec3zO3sGpzs0abd43Y+fBZRJ9uiH6lXyR/+u6brW4= github.com/uber/jaeger-client-go v2.29.1+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=