Skip to content

Commit

Permalink
refactor: update setting routes and enhance dependency management
Browse files Browse the repository at this point in the history
- Changed route parameter from ':id' to ':key' in settings-related routes for better clarity and consistency.
- Updated GetSetting, PostSetting, and PutSetting functions to use the new ':key' parameter.
- Introduced IsAutoInstallEnabled method in DependencyInstallerService to check auto-installation status.
- Enhanced the task runner to check if auto installation is enabled before proceeding with dependency installation.
- Improved initialization of settings data in the system service, ensuring proper insertion of initial settings.
  • Loading branch information
tikazyq committed Jan 1, 2025
1 parent b056105 commit 47094b8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
6 changes: 3 additions & 3 deletions core/controllers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,17 @@ func InitRoutes(app *gin.Engine) (err error) {
RegisterActions(groups.AuthGroup, "/settings", []Action{
{
Method: http.MethodGet,
Path: "/:id",
Path: "/:key",
HandlerFunc: GetSetting,
},
{
Method: http.MethodPost,
Path: "/:id",
Path: "/:key",
HandlerFunc: PostSetting,
},
{
Method: http.MethodPut,
Path: "/:id",
Path: "/:key",
HandlerFunc: PutSetting,
},
})
Expand Down
6 changes: 3 additions & 3 deletions core/controllers/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func GetSetting(c *gin.Context) {
// key
key := c.Param("id")
key := c.Param("key")

// setting
s, err := service.NewModelService[models.Setting]().GetOne(bson.M{"key": key}, nil)
Expand All @@ -29,7 +29,7 @@ func GetSetting(c *gin.Context) {

func PostSetting(c *gin.Context) {
// key
key := c.Param("id")
key := c.Param("key")

// settings
var s models.Setting
Expand Down Expand Up @@ -59,7 +59,7 @@ func PostSetting(c *gin.Context) {

func PutSetting(c *gin.Context) {
// key
key := c.Param("id")
key := c.Param("key")

// settings
var s models.Setting
Expand Down
1 change: 1 addition & 0 deletions core/interfaces/dependency_installer_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import (
)

type DependencyInstallerService interface {
IsAutoInstallEnabled() (enabled bool)
GetInstallDependencyRequirementsCmdBySpiderId(id primitive.ObjectID) (cmd *exec.Cmd, err error)
}
48 changes: 29 additions & 19 deletions core/system/service.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package system

import (
"errors"
"github.com/crawlab-team/crawlab/core/interfaces"
"github.com/crawlab-team/crawlab/core/models/models"
"github.com/crawlab-team/crawlab/core/models/service"
"github.com/crawlab-team/crawlab/core/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"sync"
)

type Service struct {
interfaces.Logger
}

func (svc *Service) Init() (err error) {
Expand All @@ -20,36 +25,41 @@ func (svc *Service) Init() (err error) {
}

func (svc *Service) initData() (err error) {
total, err := service.NewModelService[models.Setting]().Count(bson.M{
"key": "site_title",
})
if err != nil {
return err
}
if total > 0 {
return nil
}

// data to initialize
settings := []models.Setting{
// initial settings data
initData := []models.Setting{
{
Key: "site_title",
Key: "dependency",
Value: bson.M{
"customize_site_title": false,
"site_title": "",
"auto_install": true,
},
},
}
_, err = service.NewModelService[models.Setting]().InsertMany(settings)
if err != nil {
return err

for _, setting := range initData {
_, err := service.NewModelService[models.Setting]().GetOne(bson.M{"key": setting.Key}, nil)
if err != nil {
if !errors.Is(err, mongo.ErrNoDocuments) {
svc.Errorf("error getting setting: %v", err)
continue
}

// not found, insert
_, err := service.NewModelService[models.Setting]().InsertOne(setting)
if err != nil {
svc.Errorf("error inserting setting: %v", err)
continue
}
}
}

return nil
}

func newSystemService() *Service {
// service
svc := &Service{}
svc := &Service{
Logger: utils.NewLogger("SystemService"),
}

if err := svc.Init(); err != nil {
panic(err)
Expand Down
8 changes: 8 additions & 0 deletions core/task/handler/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,12 +1044,20 @@ func (r *Runner) installDependenciesIfAvailable() (err error) {
return nil
}

// Get dependency installer service
depSvc := dependency.GetDependencyInstallerRegistryService()
if depSvc == nil {
r.Warnf("dependency installer service not available")
return nil
}

// Check if auto install is enabled
if !depSvc.IsAutoInstallEnabled() {
r.Debug("auto dependency installation is disabled")
return nil
}

// Get install command
cmd, err := depSvc.GetInstallDependencyRequirementsCmdBySpiderId(r.s.Id)
if err != nil {
return err
Expand Down

0 comments on commit 47094b8

Please sign in to comment.