Add two preference edit api.

This commit is contained in:
lishuang
2020-07-11 14:40:03 +08:00
parent 69820bfcb9
commit 096eb7c73e
9 changed files with 186 additions and 25 deletions

View File

@ -1,6 +1,9 @@
package rest
import "github.com/eyebluecn/tank/code/core"
import (
"github.com/eyebluecn/tank/code/core"
jsoniter "github.com/json-iterator/go"
)
type Preference struct {
Base
@ -14,6 +17,7 @@ type Preference struct {
DefaultTotalSizeLimit int64 `json:"defaultTotalSizeLimit" gorm:"type:bigint(20) not null;default:-1"`
AllowRegister bool `json:"allowRegister" gorm:"type:tinyint(1) not null;default:0"`
PreviewConfig string `json:"previewConfig" gorm:"type:text"`
ScanConfig string `json:"scanConfig" gorm:"type:text"`
Version string `json:"version" gorm:"-"`
}
@ -21,3 +25,53 @@ type Preference struct {
func (this *Preference) TableName() string {
return core.TABLE_PREFIX + "preference"
}
const (
//scan scope all.
SCAN_SCOPE_ALL = "ALL"
//scan scope custom.
SCAN_SCOPE_CUSTOM = "CUSTOM"
)
//scan config struct.
type ScanConfig struct {
//whether enable the scan task.
Enable bool `json:"enable"`
//when to process the task.
Cron string `json:"cron"`
//username
Usernames []string `json:"usernames"`
//scan scope. see SCAN_SCOPE
Scope string `json:"scope"`
}
//fetch the scan config
func (this *Preference) FetchScanConfig() *ScanConfig {
json := this.ScanConfig
if json == "" || json == EMPTY_JSON_MAP {
return &ScanConfig{
Enable: false,
}
} else {
m := &ScanConfig{}
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(json), &m)
if err != nil {
panic(err)
}
return m
}
}
//set the scan config
func (this *Preference) SetScanConfig(scanConfigJson string) {
b, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(scanConfigJson)
if err != nil {
panic(err)
}
this.ScanConfig = string(b)
}