This is an automated email from the ASF dual-hosted git repository.

lynwee pushed a commit to branch release-v0.20
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/release-v0.20 by this push:
     new 94623f123 refactor: seperate sensitive APIs into different functions 
(#6699)
94623f123 is described below

commit 94623f1239677ea0c3f8ffddd5f6543abad90831
Author: Klesh Wong <[email protected]>
AuthorDate: Thu Dec 28 13:42:12 2023 +0800

    refactor: seperate sensitive APIs into different functions (#6699)
---
 backend/server/api/api.go         | 40 ++++++++++++++++++++++++++-------------
 backend/server/api/middlewares.go |  2 +-
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/backend/server/api/api.go b/backend/server/api/api.go
index 0ee5021c1..f9cfea8c1 100644
--- a/backend/server/api/api.go
+++ b/backend/server/api/api.go
@@ -32,6 +32,7 @@ import (
        ginSwagger "github.com/swaggo/gin-swagger"
        "github.com/swaggo/swag"
 
+       "github.com/apache/incubator-devlake/core/context"
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/impls/logruslog"
@@ -49,6 +50,14 @@ To proceed, please send a request to 
<config-ui-endpoint>/api/proceed-db-migrati
 Alternatively, you may downgrade back to the previous DevLake version.
 `
 
+var basicRes context.BasicRes
+
+func Init() {
+       // Initialize services
+       services.Init()
+       basicRes = services.GetBasicRes()
+}
+
 // @title  DevLake Swagger API
 // @version 0.1
 // @description  <h2>This is the main page of devlake api</h2>
@@ -56,23 +65,33 @@ Alternatively, you may downgrade back to the previous 
DevLake version.
 // @host localhost:8080
 // @BasePath /
 func CreateAndRunApiServer() {
-       // Create router
-       router := gin.New()
        // Setup and run the server
+       Init()
+       router := CreateApiServer()
        SetupApiServer(router)
        RunApiServer(router)
 }
 
-func SetupApiServer(router *gin.Engine) {
-       // Initialize services
-       services.Init()
-       // Set gin mode
-       gin.SetMode(services.GetBasicRes().GetConfig("MODE"))
+func CreateApiServer() *gin.Engine {
+       // Create router
+       router := gin.New()
 
        // For both protected and unprotected routes
        router.GET("/ping", ping.Get)
+       router.GET("/health", ping.Get)
        router.GET("/version", version.Get)
 
+       // Api keys
+       router.Use(RestAuthentication(router, basicRes))
+       router.Use(OAuth2ProxyAuthentication(basicRes))
+
+       return router
+}
+
+func SetupApiServer(router *gin.Engine) {
+       // Set gin mode
+       gin.SetMode(basicRes.GetConfig("MODE"))
+
        // Endpoint to proceed database migration
        router.GET("/proceed-db-migration", func(ctx *gin.Context) {
                // Check if migration requires confirmation
@@ -92,11 +111,6 @@ func SetupApiServer(router *gin.Engine) {
                shared.ApiOutputSuccess(ctx, nil, http.StatusOK)
        })
 
-       // Api keys
-       basicRes := services.GetBasicRes()
-       router.Use(RestAuthentication(router, basicRes))
-       router.Use(OAuth2ProxyAuthentication(basicRes))
-
        // Restrict access if database migration is required
        router.Use(func(ctx *gin.Context) {
                if !services.MigrationRequireConfirmation() {
@@ -141,7 +155,7 @@ func SetupApiServer(router *gin.Engine) {
 
 func RunApiServer(router *gin.Engine) {
        // Get port from config
-       port := services.GetBasicRes().GetConfig("PORT")
+       port := basicRes.GetConfig("PORT")
        // Trim any : from the start
        port = strings.TrimLeft(port, ":")
        // Convert to int
diff --git a/backend/server/api/middlewares.go 
b/backend/server/api/middlewares.go
index b283fb727..9e274c259 100644
--- a/backend/server/api/middlewares.go
+++ b/backend/server/api/middlewares.go
@@ -110,7 +110,7 @@ func RestAuthentication(router *gin.Engine, basicRes 
context.BasicRes) gin.Handl
                path := c.Request.URL.Path
                // Only open api needs to check api key
                if !strings.HasPrefix(path, "/rest") {
-                       logger.Info("path %s will continue", path)
+                       logger.Debug("path %s will continue", path)
                        c.Next()
                        return
                }

Reply via email to