Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #51 from vidar-team/dev
Browse files Browse the repository at this point in the history
v0.6.0
  • Loading branch information
wuhan005 authored Jun 19, 2020
2 parents 856249a + 8a36c15 commit 97618c0
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 57 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ require (
github.com/theplant/htmltestingutils v0.0.0-20190423050759-0e06de7b6967 // indirect
github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61 // indirect
github.com/vidar-team/Cardinal_frontend v0.5.2-alpha
github.com/vidar-team/Cardinal_manager_frontend v0.5.2-alpha
github.com/vidar-team/Cardinal_manager_frontend v0.6.0
github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd // indirect
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd
golang.org/x/text v0.3.0
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
moul.io/http2curl v1.0.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ github.com/vidar-team/Cardinal_frontend v0.5.2-alpha h1:sG37Ypm0G25bdfrvmNh3n6rD
github.com/vidar-team/Cardinal_frontend v0.5.2-alpha/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI=
github.com/vidar-team/Cardinal_manager_frontend v0.5.2-alpha h1:xgXwPqXCjHiHVQC7qoj54Cf750cwENXutqnYK13+US8=
github.com/vidar-team/Cardinal_manager_frontend v0.5.2-alpha/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk=
github.com/vidar-team/Cardinal_manager_frontend v0.6.0 h1:OMsb3O7raah7r37ppyjm83iMnV4B+1OTWG38VTuT0T8=
github.com/vidar-team/Cardinal_manager_frontend v0.6.0/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk=
github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd h1:LxsPdxzQnF9yE7Xv/1Ln8C8JJbEh1SlRyIDvgR/bsKg=
github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -198,6 +200,7 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384 h1:TFlARGu6Czu1z7q93HTxcP1P+/ZFC/IKythI5RzrnRg=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down
2 changes: 1 addition & 1 deletion src/asteroid.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s *Service) asteroidGreetData() (result asteroid.Greet) {

func (s *Service) getAsteroidStatus() (int, interface{}) {
return utils.MakeSuccessJSON(gin.H{
"status": s.GetBool("asteroid_enabled"),
// TODO
})
}

Expand Down
4 changes: 4 additions & 0 deletions src/asteroid/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ func (c *client) writePump() {
w.Write(message)
if err := w.Close(); err != nil {
log.Printf("error: %v", err)
delete(hub.clients, c)
close(c.send)
return
}
case <-ticker.C:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
delete(hub.clients, c)
close(c.send)
return
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/conf/model.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package conf

import "time"
import (
"github.com/jinzhu/gorm"
"time"
)

// Config is the config of the cardinal.
type Config struct {
Base `toml:"base"`
MySQL `toml:"mysql"`
Base `toml:"base";gorm:"-"`
MySQL `toml:"mysql";gorm:"-"`

*DynamicConfig
}

// 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 `json:"-"`

// TODO
//Title string `gorm:"default:'HCTF'";json:"Title";binding:"required"`
}

// Base is the basic config of the cardinal.
Expand Down
74 changes: 24 additions & 50 deletions src/dynamic_config.go
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"))
}
1 change: 1 addition & 0 deletions src/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ en-US:
no_auth: "Request Unauthorized!"
not_found: "Resource Not Found!"
method_not_allow: "Request Method Not Allowed!"
database_charset_error: "Database Charset Error. Please Make Sure the Charset of the Database is utf8mb4. e.g. CREATE DATABASE `cardinal` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

bulletin:
post_error: "Add Bulletin Failed!"
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ zh-CN:
no_auth: "未授权访问"
not_found: "资源不存在"
method_not_allow: "请求方法不允许"
database_charset_error: "数据库编码格式错误。可能导致无法正确处理中文字符,请删除并重新创建数据库,设置编码格式为 utf8mb4。示例:CREATE DATABASE `cardinal` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

bulletin:
post_error: "添加公告失败!"
Expand Down
9 changes: 9 additions & 0 deletions src/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/vidar-team/Cardinal/src/conf"
"github.com/vidar-team/Cardinal/src/locales"
"log"
)

Expand Down Expand Up @@ -41,5 +42,13 @@ func (s *Service) initMySQL() {
&Flag{},
&Log{},
&WebHook{},

conf.DynamicConfig{},
)

// Test the database charset.
if s.Mysql.Exec("SELECT * FROM `logs` WHERE `Content` = '中文测试';").Error != nil {
log.Fatalln(locales.I18n.T(conf.Get().SystemLanguage, "general.database_charset_error"))
}

}
8 changes: 8 additions & 0 deletions src/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ func (s *Service) initRouter() *gin.Engine {
manager.DELETE("/webhook", func(c *gin.Context) {
c.JSON(s.deleteWebHook(c))
})

// Config
//manager.GET("/config", func(c *gin.Context) {
// c.JSON(s.getConfig(c))
//})
//manager.PUT("/config", func(c *gin.Context) {
// c.JSON(s.editConfig(c))
//})
}

// 404
Expand Down
4 changes: 2 additions & 2 deletions src/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (s *Service) init() {
// Init database.
s.initMySQL()

// Init the dynamic config from the database.
s.initDynamicConfig()
// Refresh the dynamic config from the database.
s.RefreshConfig()

// Unity3D Asteroid
asteroid.InitAsteroid(s.asteroidGreetData)
Expand Down

0 comments on commit 97618c0

Please sign in to comment.