From a3b286558b128b2d97b01544a7509d7c39b6a404 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Mon, 18 Nov 2024 16:48:09 +0800 Subject: [PATCH] refactor: consolidated configs --- .github/workflows/docker-crawlab.yml | 6 +- backend/conf/config.yml | 25 --- core/apps/api.go | 31 +-- core/apps/docker.go | 189 ------------------ core/apps/interfaces.go | 15 +- core/apps/server.go | 30 +-- core/config/config.go | 20 +- core/config/config_test.go | 3 +- core/controllers/base_file.go | 4 +- core/controllers/router.go | 48 +++-- core/controllers/spider.go | 7 +- core/controllers/sync.go | 5 +- core/controllers/task.go | 5 +- core/entity/address.go | 56 ------ core/fs/default.go | 3 +- core/grpc/client/client.go | 43 +--- core/grpc/middlewares/auth_token.go | 20 +- core/grpc/server/server.go | 50 +---- core/middlewares/auth.go | 21 +- core/middlewares/cors.go | 16 +- core/middlewares/{middlewares.go => utils.go} | 0 core/task/handler/runner.go | 99 +++++---- core/task/log/file_driver.go | 2 +- core/utils/config.go | 143 +++++++++++++ core/utils/spider.go | 3 +- core/utils/system.go | 7 - core/utils/task.go | 4 +- docker/base-image/Dockerfile | 3 - 28 files changed, 329 insertions(+), 529 deletions(-) delete mode 100644 core/apps/docker.go delete mode 100644 core/entity/address.go rename core/middlewares/{middlewares.go => utils.go} (100%) create mode 100644 core/utils/config.go delete mode 100644 core/utils/system.go diff --git a/.github/workflows/docker-crawlab.yml b/.github/workflows/docker-crawlab.yml index 1634f1d1..7f1b953c 100644 --- a/.github/workflows/docker-crawlab.yml +++ b/.github/workflows/docker-crawlab.yml @@ -282,7 +282,7 @@ jobs: --health-interval 10s --health-timeout 5s --health-retries 5 - crawlab: + master: image: ghcr.io/${{ github.repository_owner }}/crawlab:${{ needs.setup.outputs.version }} env: CRAWLAB_NODE_MASTER: Y @@ -295,6 +295,10 @@ jobs: --health-interval 30s --health-timeout 10s --health-retries 5 + worker: + image: ghcr.io/${{ github.repository_owner }}/crawlab:${{ needs.setup.outputs.version }} + env: + CRAWLAB_NODE_MASTER: N steps: - name: Checkout repository uses: actions/checkout@v4 diff --git a/backend/conf/config.yml b/backend/conf/config.yml index 828480b7..a1701de3 100644 --- a/backend/conf/config.yml +++ b/backend/conf/config.yml @@ -1,27 +1,2 @@ -# Crawlab Configuration File edition: global.edition.community version: v0.7.0 - -mongo: - host: localhost - port: 27017 - db: crawlab_test - username: "" - password: "" - authSource: "admin" - -server: - host: 0.0.0.0 - port: 8000 - -grpc: - address: localhost:9666 - server: - address: 0.0.0.0:9666 - authKey: Crawlab2021! - -api: - endpoint: http://localhost:8000 - -log: - path: /var/log/crawlab diff --git a/core/apps/api.go b/core/apps/api.go index e73b7716..0323824f 100644 --- a/core/apps/api.go +++ b/core/apps/api.go @@ -5,11 +5,9 @@ import ( "errors" "github.com/apex/log" "github.com/crawlab-team/crawlab/core/controllers" - "github.com/crawlab-team/crawlab/core/interfaces" "github.com/crawlab-team/crawlab/core/middlewares" "github.com/crawlab-team/crawlab/core/utils" "github.com/gin-gonic/gin" - "github.com/spf13/viper" "net" "net/http" "sync" @@ -18,22 +16,14 @@ import ( func init() { // set gin mode - if viper.GetString("gin.mode") == "" { - gin.SetMode(gin.ReleaseMode) - } else { - gin.SetMode(viper.GetString("gin.mode")) - } + gin.SetMode(utils.GetGinMode()) } type Api struct { - // dependencies - interfaces.WithConfigPath - // internals - app *gin.Engine - ln net.Listener - srv *http.Server - ready bool + app *gin.Engine + ln net.Listener + srv *http.Server } func (app *Api) Init() { @@ -46,8 +36,8 @@ func (app *Api) Init() { func (app *Api) Start() { // address - host := viper.GetString("server.host") - port := viper.GetString("server.port") + host := utils.GetServerHost() + port := utils.GetServerPort() address := net.JoinHostPort(host, port) // http server @@ -62,7 +52,6 @@ func (app *Api) Start() { if err != nil { panic(err) } - app.ready = true // serve if err := http.Serve(app.ln, app.app); err != nil { @@ -95,17 +84,13 @@ func (app *Api) GetHttpServer() *http.Server { return app.srv } -func (app *Api) Ready() (ok bool) { - return app.ready -} - func (app *Api) initModuleWithApp(name string, fn func(app *gin.Engine) error) (err error) { return initModule(name, func() error { return fn(app.app) }) } -func NewApi() *Api { +func newApi() *Api { api := &Api{ app: gin.New(), } @@ -118,7 +103,7 @@ var apiOnce sync.Once func GetApi() *Api { apiOnce.Do(func() { - api = NewApi() + api = newApi() }) return api } diff --git a/core/apps/docker.go b/core/apps/docker.go deleted file mode 100644 index 50e391b2..00000000 --- a/core/apps/docker.go +++ /dev/null @@ -1,189 +0,0 @@ -package apps - -import ( - "bufio" - "fmt" - "github.com/crawlab-team/crawlab/core/interfaces" - "github.com/crawlab-team/crawlab/core/sys_exec" - "github.com/crawlab-team/crawlab/core/utils" - "github.com/crawlab-team/crawlab/trace" - "github.com/imroc/req" - "github.com/spf13/viper" - "os" - "os/exec" - "strings" - "time" -) - -type Docker struct { - // parent - parent ServerApp - - // dependencies - interfaces.WithConfigPath - - // seaweedfs log - fsLogFilePath string - fsLogFile *os.File - fsReady bool -} - -func (app *Docker) Init() { - var err error - app.fsLogFile, err = os.OpenFile(app.fsLogFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.FileMode(0777)) - if err != nil { - trace.PrintError(err) - } - - // replace paths - if err := app.replacePaths(); err != nil { - panic(err) - } - - // start nginx - go app.startNginx() - - // start seaweedfs - go app.startSeaweedFs() -} - -func (app *Docker) Start() { - // import demo - //if utils.IsDemo() && utils.InitializedDemo() { - // go app.importDemo() - //} -} - -func (app *Docker) Wait() { - DefaultWait() -} - -func (app *Docker) Stop() { -} - -func (app *Docker) GetParent() (parent ServerApp) { - return app.parent -} - -func (app *Docker) SetParent(parent ServerApp) { - app.parent = parent -} - -func (app *Docker) Ready() (ok bool) { - return app.fsReady && - app.parent.GetApi().Ready() -} - -func (app *Docker) replacePaths() (err error) { - // read - indexHtmlPath := "/app/dist/index.html" - indexHtmlBytes, err := os.ReadFile(indexHtmlPath) - if err != nil { - return trace.TraceError(err) - } - indexHtml := string(indexHtmlBytes) - - // replace paths - baseUrl := viper.GetString("base.url") - if baseUrl != "" { - indexHtml = app._replacePath(indexHtml, "js", baseUrl) - indexHtml = app._replacePath(indexHtml, "css", baseUrl) - indexHtml = app._replacePath(indexHtml, "