From 5e720af8584b9b73049a9911f27fd289d56d1e8f Mon Sep 17 00:00:00 2001 From: Stefano Cappa Date: Tue, 20 Aug 2024 17:25:40 +0200 Subject: [PATCH] feat: make oauth callback url configurable via env var Signed-off-by: Stefano Cappa --- .env_template | 1 + initialization/server.go | 8 ++++++-- initialization/start.go | 9 +++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.env_template b/.env_template index 431646e..61b806c 100644 --- a/.env_template +++ b/.env_template @@ -2,6 +2,7 @@ MONGODB_URL=mongodb://localhost:27017/ HTTP_SERVER=http://localhost HTTP_PORT=8082 HTTP_CORS=true +OAUTH_CALLBACK=http://localhost:8082/api/callback/ HTTP_SENSOR_SERVER=http://localhost HTTP_SENSOR_PORT=8000 HTTP_SENSOR_GETVALUE_API=/sensors/ diff --git a/initialization/server.go b/initialization/server.go index fb8b04c..7496dea 100644 --- a/initialization/server.go +++ b/initialization/server.go @@ -32,9 +32,13 @@ var oauthCallbackURL string var oauthScopes = []string{"repo"} //https://developer.github.com/v3/oauth/#scopes // SetupRouter function -func SetupRouter(httpOrigin string, logger *zap.SugaredLogger) (*gin.Engine, cookie.Store) { +func SetupRouter(httpServer string, port string, oauthCallback string, logger *zap.SugaredLogger) (*gin.Engine, cookie.Store) { // init oauthCallbackURL based on httpOrigin - oauthCallbackURL = httpOrigin + "/api/callback" + oauthCallbackURL = oauthCallback + logger.Info("SetupRouter - oauthCallbackURL is = " + oauthCallbackURL) + + httpOrigin := httpServer + ":" + port + logger.Info("SetupRouter - httpOrigin is = " + httpOrigin) // init GIN router := gin.Default() diff --git a/initialization/start.go b/initialization/start.go index f7abe5d..08d8599 100644 --- a/initialization/start.go +++ b/initialization/start.go @@ -21,14 +21,15 @@ func Start() (*zap.SugaredLogger, *gin.Engine, context.Context, *mongo.Collectio // 3. Init server port := os.Getenv("HTTP_PORT") - httpOrigin := os.Getenv("HTTP_SERVER") + ":" + port - router, ctx, collectionProfiles, collectionHomes, collectionDevices := BuildServer(httpOrigin, logger) + httpServer := os.Getenv("HTTP_SERVER") + oauthCallback := os.Getenv("OAUTH_CALLBACK") + router, ctx, collectionProfiles, collectionHomes, collectionDevices := BuildServer(httpServer, port, oauthCallback, logger) return logger, router, ctx, collectionProfiles, collectionHomes, collectionDevices } // BuildServer - Exposed only for testing purposes -func BuildServer(httpOrigin string, logger *zap.SugaredLogger) (*gin.Engine, context.Context, *mongo.Collection, *mongo.Collection, *mongo.Collection) { +func BuildServer(httpServer string, port string, oauthCallback string, logger *zap.SugaredLogger) (*gin.Engine, context.Context, *mongo.Collection, *mongo.Collection, *mongo.Collection) { // Initialization ctx := context.Background() // Create a singleton validator instance. Validate is designed to be used as a singleton instance. @@ -43,7 +44,7 @@ func BuildServer(httpOrigin string, logger *zap.SugaredLogger) (*gin.Engine, con // Instantiate GIN and apply some middlewares logger.Info("BuildServer - GIN - Initializing...") - router, cookieStore := SetupRouter(httpOrigin, logger) + router, cookieStore := SetupRouter(httpServer, port, oauthCallback, logger) RegisterRoutes(ctx, router, &cookieStore, logger, validate, collectionProfiles, collectionHomes, collectionDevices) return router, ctx, collectionProfiles, collectionHomes, collectionDevices }