From d0caf9f5ad1d4d1cd83d4a984ccbac17378a2736 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Mon, 18 Nov 2024 17:25:13 +0800 Subject: [PATCH] refactor: consolidated configs --- .github/workflows/docker-crawlab.yml | 2 ++ core/apps/api.go | 4 +-- core/utils/config.go | 53 +++++++++++++++++++--------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/.github/workflows/docker-crawlab.yml b/.github/workflows/docker-crawlab.yml index 7f1b953c..895eff22 100644 --- a/.github/workflows/docker-crawlab.yml +++ b/.github/workflows/docker-crawlab.yml @@ -34,6 +34,7 @@ jobs: workflow_changed: ${{ steps.check_changed_files.outputs.workflow_changed }} base_image_changed: ${{ steps.check_changed_files.outputs.base_image_changed }} version: ${{ steps.version.outputs.version }} + test_script: ${{ steps.test_config.outputs.test_script }} steps: - uses: actions/checkout@v4 @@ -299,6 +300,7 @@ jobs: image: ghcr.io/${{ github.repository_owner }}/crawlab:${{ needs.setup.outputs.version }} env: CRAWLAB_NODE_MASTER: N + CRAWLAB_MASTER_HOST: master steps: - name: Checkout repository uses: actions/checkout@v4 diff --git a/core/apps/api.go b/core/apps/api.go index 0323824f..c6b062ec 100644 --- a/core/apps/api.go +++ b/core/apps/api.go @@ -36,9 +36,7 @@ func (app *Api) Init() { func (app *Api) Start() { // address - host := utils.GetServerHost() - port := utils.GetServerPort() - address := net.JoinHostPort(host, port) + address := utils.GetServerAddress() // http server app.srv = &http.Server{ diff --git a/core/utils/config.go b/core/utils/config.go index 40286a2e..549b9ca4 100644 --- a/core/utils/config.go +++ b/core/utils/config.go @@ -1,16 +1,20 @@ package utils import ( + "fmt" "github.com/gin-gonic/gin" "github.com/spf13/viper" ) const ( - DefaultServerHost = "0.0.0.0" - DefaultServerPort = "8000" + DefaultWorkspace = "crawlab_workspace" DefaultTaskLogPath = "/var/log/crawlab/tasks" + DefaultServerHost = "0.0.0.0" + DefaultServerPort = 8000 DefaultGrpcHost = "localhost" - DefaultGrpcPort = "9666" + DefaultGrpcPort = 9666 + DefaultGrpcServerHost = "127.0.0.1" + DefaultGrpcServerPort = 9666 DefaultAuthKey = "Crawlab2024!" DefaultApiEndpoint = "http://localhost:8000" DefaultApiAllowOrigin = "*" @@ -71,7 +75,10 @@ func IsPro() bool { } func GetWorkspace() string { - return viper.GetString("workspace") + if res := viper.GetString("workspace"); res != "" { + return res + } + return DefaultWorkspace } func GetTaskLogPath() string { @@ -81,18 +88,18 @@ func GetTaskLogPath() string { return DefaultTaskLogPath } -func GetServerHost() string { - if res := viper.GetString("server.host"); res != "" { - return res +func GetServerAddress() string { + host := viper.GetString("server.host") + if host == "" { + host = DefaultServerHost } - return DefaultServerHost -} -func GetServerPort() string { - if res := viper.GetString("server.port"); res != "" { - return res + port := viper.GetInt("server.port") + if port == 0 { + port = DefaultServerPort } - return DefaultServerPort + + return fmt.Sprintf("%s:%d", host, port) } func GetMasterHost() string { @@ -101,7 +108,6 @@ func GetMasterHost() string { func GetGrpcAddress() string { host := viper.GetString("grpc.host") - port := viper.GetString("grpc.port") if host == "" { masterHost := GetMasterHost() if masterHost != "" { @@ -110,14 +116,27 @@ func GetGrpcAddress() string { host = DefaultGrpcHost } } - if port == "" { + + port := viper.GetInt("grpc.port") + if port == 0 { port = DefaultGrpcPort } - return host + ":" + port + + return fmt.Sprintf("%s:%d", host, port) } func GetGrpcServerAddress() string { - return viper.GetString("grpc.server.address") + host := viper.GetString("grpc.server.host") + if host == "" { + host = DefaultGrpcServerHost + } + + port := viper.GetInt("grpc.server.port") + if port == 0 { + port = DefaultGrpcServerPort + } + + return fmt.Sprintf("%s:%d", host, port) } func GetAuthKey() string {