mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-28 17:50:56 +01:00
refactor: update setting routes and enhance dependency management
- 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.
This commit is contained in:
@@ -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) {
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user