mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Add support for custom languages.
This commit is contained in:
parent
c2b705f172
commit
c821e02b82
@ -1,3 +1,6 @@
|
||||
[server]
|
||||
listen = 127.0.0.1
|
||||
port = 3000
|
||||
|
||||
[languages]
|
||||
vue = Vue
|
8
main.go
8
main.go
@ -49,6 +49,13 @@ func readConfig() *models.Config {
|
||||
port = cfg.Section("server").Key("port").MustInt()
|
||||
}
|
||||
|
||||
// Read custom languages
|
||||
customLangs := make(map[string]string)
|
||||
languageKeys := cfg.Section("languages").Keys()
|
||||
for _, k := range languageKeys {
|
||||
customLangs[k.Name()] = k.MustString("unknown")
|
||||
}
|
||||
|
||||
return &models.Config{
|
||||
Port: port,
|
||||
Addr: addr,
|
||||
@ -57,6 +64,7 @@ func readConfig() *models.Config {
|
||||
DbPassword: dbPassword,
|
||||
DbName: dbName,
|
||||
DbDialect: "mysql",
|
||||
CustomLanguages: customLangs,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,4 +8,5 @@ type Config struct {
|
||||
DbPassword string
|
||||
DbName string
|
||||
DbDialect string
|
||||
CustomLanguages map[string]string
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -25,12 +26,30 @@ type Heartbeat struct {
|
||||
Editor string `json:"editor"`
|
||||
OperatingSystem string `json:"operating_system"`
|
||||
Time *HeartbeatReqTime `json:"time" gorm:"type:timestamp; default:now(); index:idx_time,idx_time_user"`
|
||||
languageRegex *regexp.Regexp
|
||||
}
|
||||
|
||||
func (h *Heartbeat) Valid() bool {
|
||||
return h.User != nil && h.UserID != "" && h.Time != nil
|
||||
}
|
||||
|
||||
func (h *Heartbeat) Augment(customLangs map[string]string) {
|
||||
if h.Language == "" {
|
||||
if h.languageRegex == nil {
|
||||
h.languageRegex = regexp.MustCompile(`^.+\.(.+)$`)
|
||||
}
|
||||
groups := h.languageRegex.FindAllStringSubmatch(h.Entity, -1)
|
||||
if len(groups) == 0 || len(groups[0]) != 2 {
|
||||
return
|
||||
}
|
||||
ending := groups[0][1]
|
||||
if _, ok := customLangs[ending]; !ok {
|
||||
return
|
||||
}
|
||||
h.Language, _ = customLangs[ending]
|
||||
}
|
||||
}
|
||||
|
||||
func (j *HeartbeatReqTime) UnmarshalJSON(b []byte) error {
|
||||
s := strings.Split(strings.Trim(string(b), "\""), ".")[0]
|
||||
i, err := strconv.ParseInt(s, 10, 64)
|
||||
|
@ -32,13 +32,14 @@ func (h *HeartbeatHandler) Post(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, h := range heartbeats {
|
||||
h.OperatingSystem = opSys
|
||||
h.Editor = editor
|
||||
h.User = user
|
||||
h.UserID = user.ID
|
||||
for _, hb := range heartbeats {
|
||||
hb.OperatingSystem = opSys
|
||||
hb.Editor = editor
|
||||
hb.User = user
|
||||
hb.UserID = user.ID
|
||||
hb.Augment(h.HeartbeatSrvc.Config.CustomLanguages)
|
||||
|
||||
if !h.Valid() {
|
||||
if !hb.Valid() {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("Invalid heartbeat object."))
|
||||
return
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
const TableHeartbeat = "heartbeat"
|
||||
|
||||
type HeartbeatService struct {
|
||||
Cofnig *models.Config
|
||||
Config *models.Config
|
||||
Db *gorm.DB
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user