mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
- 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.
83 lines
1.5 KiB
Go
83 lines
1.5 KiB
Go
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) {
|
|
// initialize data
|
|
if err := svc.initData(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (svc *Service) initData() (err error) {
|
|
// initial settings data
|
|
initData := []models.Setting{
|
|
{
|
|
Key: "dependency",
|
|
Value: bson.M{
|
|
"auto_install": true,
|
|
},
|
|
},
|
|
}
|
|
|
|
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{
|
|
Logger: utils.NewLogger("SystemService"),
|
|
}
|
|
|
|
if err := svc.Init(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return svc
|
|
}
|
|
|
|
var _service *Service
|
|
var _serviceOnce sync.Once
|
|
|
|
func GetSystemService() *Service {
|
|
if _service == nil {
|
|
_service = newSystemService()
|
|
}
|
|
_serviceOnce.Do(func() {
|
|
_service = newSystemService()
|
|
})
|
|
return _service
|
|
}
|