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

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


The following commit(s) were added to refs/heads/main by this push:
     new 878278739 refactor: added CreateAndRunApiServer (#6581)
878278739 is described below

commit 87827873997053d24d3b591910bbd7b25885b721
Author: Klesh Wong <[email protected]>
AuthorDate: Thu Dec 7 11:51:00 2023 +0800

    refactor: added CreateAndRunApiServer (#6581)
---
 backend/server/api/api.go     | 24 ++++++++++++++----------
 backend/server/main.go        |  2 +-
 backend/test/helper/client.go |  2 +-
 3 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/backend/server/api/api.go b/backend/server/api/api.go
index 698e9e1c9..0ee5021c1 100644
--- a/backend/server/api/api.go
+++ b/backend/server/api/api.go
@@ -32,7 +32,6 @@ import (
        ginSwagger "github.com/swaggo/gin-swagger"
        "github.com/swaggo/swag"
 
-       "github.com/apache/incubator-devlake/core/config"
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/impls/logruslog"
@@ -56,17 +55,19 @@ Alternatively, you may downgrade back to the previous 
DevLake version.
 // @license.name Apache-2.0
 // @host localhost:8080
 // @BasePath /
-func CreateApiService(router *gin.Engine) {
-       // Get configuration
-       v := config.GetConfig()
+func CreateAndRunApiServer() {
+       // Create router
+       router := gin.New()
+       // Setup and run the server
+       SetupApiServer(router)
+       RunApiServer(router)
+}
+
+func SetupApiServer(router *gin.Engine) {
        // Initialize services
        services.Init()
        // Set gin mode
-       gin.SetMode(v.GetString("MODE"))
-       // Create a gin router
-       if router == nil {
-               router = gin.Default()
-       }
+       gin.SetMode(services.GetBasicRes().GetConfig("MODE"))
 
        // For both protected and unprotected routes
        router.GET("/ping", ping.Get)
@@ -136,8 +137,11 @@ func CreateApiService(router *gin.Engine) {
 
        // Register API endpoints
        RegisterRouter(router, basicRes)
+}
+
+func RunApiServer(router *gin.Engine) {
        // Get port from config
-       port := v.GetString("PORT")
+       port := services.GetBasicRes().GetConfig("PORT")
        // Trim any : from the start
        port = strings.TrimLeft(port, ":")
        // Convert to int
diff --git a/backend/server/main.go b/backend/server/main.go
index 514a179e6..664eaf0b8 100644
--- a/backend/server/main.go
+++ b/backend/server/main.go
@@ -30,5 +30,5 @@ func main() {
        if encryptionSecret == "" {
                panic("ENCRYPTION_SECRET must be set in environment variable or 
.env file")
        }
-       api.CreateApiService(nil)
+       api.CreateAndRunApiServer()
 }
diff --git a/backend/test/helper/client.go b/backend/test/helper/client.go
index 840dbc25c..dcc30cba2 100644
--- a/backend/test/helper/client.go
+++ b/backend/test/helper/client.go
@@ -172,7 +172,7 @@ func ConnectLocalServer(t *testing.T, clientConfig 
*LocalClientConfig) *DevlakeC
                cfg.Set("PLUGIN_DIR", throwawayDir)
                cfg.Set("LOGGING_DIR", throwawayDir)
                go func() {
-                       initService.Do(func() { api.CreateApiService(nil) })
+                       initService.Do(func() { api.CreateAndRunApiServer() })
                }()
                req, err2 := http.NewRequest(http.MethodGet, 
fmt.Sprintf("%s/proceed-db-migration", addr), nil)
                require.NoError(t, err2)

Reply via email to