This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from vidar-team/dev
v0.6.0
- Loading branch information
Showing
11 changed files
with
72 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jinzhu/gorm" | ||
"strconv" | ||
"github.com/gin-gonic/gin" | ||
"github.com/vidar-team/Cardinal/src/conf" | ||
"github.com/vidar-team/Cardinal/src/locales" | ||
"github.com/vidar-team/Cardinal/src/utils" | ||
) | ||
|
||
// DynamicConfig is the config which is stored in database. | ||
// So it's a GORM model for users can edit it anytime. | ||
type DynamicConfig struct { | ||
gorm.Model | ||
Key string | ||
Value string | ||
// RefreshConfig put the config into the struct from database. | ||
func (s *Service) RefreshConfig() { | ||
conf.Get().DynamicConfig = new(conf.DynamicConfig) | ||
s.Mysql.Model(conf.DynamicConfig{}).Last(conf.Get().DynamicConfig) | ||
} | ||
|
||
func (s *Service) initDynamicConfig() { | ||
if (!s.Mysql.HasTable(&DynamicConfig{})) { | ||
s.Mysql.AutoMigrate(&DynamicConfig{}) | ||
|
||
// Set default data | ||
s.Set("title", "HCTF") | ||
s.Set("asteroid_enabled", false) | ||
} | ||
} | ||
|
||
func (s *Service) Set(key string, value interface{}) { | ||
v := fmt.Sprintf("%v", value) | ||
var count int | ||
s.Mysql.Model(&DynamicConfig{}).Where(&DynamicConfig{Key: key}).Count(&count) | ||
if count == 0 { | ||
s.Mysql.Create(&DynamicConfig{ | ||
Key: key, | ||
Value: v, | ||
}) | ||
} else { | ||
s.Mysql.Model(&DynamicConfig{}).Where("key = ?", key).Updates(map[string]interface{}{"value": v}) | ||
} | ||
} | ||
|
||
func (s *Service) GetString(key string) string { | ||
var conf DynamicConfig | ||
s.Mysql.Model(&DynamicConfig{}).Where(&DynamicConfig{Key: key}).Find(&conf) | ||
return conf.Value | ||
// SetConfig update the config by insert a new record into database, for we can make a config version control soon. | ||
// Then refresh the config in struct. | ||
func (s *Service) SetConfig(config *conf.DynamicConfig) { | ||
s.Mysql.Model(conf.DynamicConfig{}).Create(config) | ||
s.RefreshConfig() | ||
} | ||
|
||
func (s *Service) GetBool(key string) bool { | ||
var conf DynamicConfig | ||
s.Mysql.Model(&DynamicConfig{}).Where(&DynamicConfig{Key: key}).Find(&conf) | ||
if value, ok := strconv.ParseBool(conf.Value); ok == nil { | ||
return value | ||
} | ||
return false | ||
func (s *Service) getConfig(c *gin.Context) (int, interface{}) { | ||
return utils.MakeSuccessJSON(conf.Get().DynamicConfig) | ||
} | ||
|
||
func (s *Service) GetInt(key string) int { | ||
var conf DynamicConfig | ||
s.Mysql.Model(&DynamicConfig{}).Where(&DynamicConfig{Key: key}).Find(&conf) | ||
if value, ok := strconv.ParseInt(conf.Value, 10, 32); ok == nil { | ||
return int(value) | ||
func (s *Service) editConfig(c *gin.Context) (int, interface{}) { | ||
var formData conf.DynamicConfig | ||
err := c.BindJSON(&formData) | ||
if err != nil { | ||
return utils.MakeErrJSON(400, 40000, | ||
locales.I18n.T(c.GetString("lang"), "general.error_payload"), | ||
) | ||
} | ||
return 0 | ||
s.SetConfig(&formData) | ||
return utils.MakeSuccessJSON(locales.I18n.T(c.GetString("lang"), "general.success")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters