Skip to content

Commit

Permalink
feat: make oauth callback url configurable via env var
Browse files Browse the repository at this point in the history
Signed-off-by: Stefano Cappa <[email protected]>
  • Loading branch information
Ks89 committed Aug 20, 2024
1 parent 7ea52b7 commit 5e720af
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env_template
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
8 changes: 6 additions & 2 deletions initialization/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions initialization/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down

0 comments on commit 5e720af

Please sign in to comment.