From 4a3fd774762edf4a349b1e1388b2ebb0a5abc98f Mon Sep 17 00:00:00 2001 From: Valerio Santinelli Date: Tue, 25 Oct 2022 10:47:30 +0200 Subject: [PATCH 1/3] Added start command and updated Go version to 1.17 in Dockerfile --- build/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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"] From 64dbadf6e6ba37a3030e0703139985c59cfe34c4 Mon Sep 17 00:00:00 2001 From: Valerio Santinelli Date: Sun, 8 Jan 2023 14:34:52 +0100 Subject: [PATCH 2/3] Added CORS support --- api/app.go | 6 +++--- api/middleware.go | 22 +++++++++++++++++++++- go.sum | 2 -- 3 files changed, 24 insertions(+), 6 deletions(-) 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..0f5dfc3 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,23 @@ 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", "*") +} + +func (m addCorsMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { + addCorsHeaders(w) + 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/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= From d147d8a84d69852a2f6c18f6acd4ae7d527cc0bd Mon Sep 17 00:00:00 2001 From: Valerio Santinelli Date: Sun, 8 Jan 2023 15:56:43 +0100 Subject: [PATCH 3/3] Fixed more issues with CORS and pre-flight --- api/middleware.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/api/middleware.go b/api/middleware.go index 0f5dfc3..7c06e48 100644 --- a/api/middleware.go +++ b/api/middleware.go @@ -204,11 +204,22 @@ type addCorsMiddleware struct { 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) - m.Handler.ServeHTTP(w, r) + 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) {