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]
|
[server]
|
||||||
listen = 127.0.0.1
|
listen = 127.0.0.1
|
||||||
port = 3000
|
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()
|
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{
|
return &models.Config{
|
||||||
Port: port,
|
Port: port,
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
@ -57,6 +64,7 @@ func readConfig() *models.Config {
|
|||||||
DbPassword: dbPassword,
|
DbPassword: dbPassword,
|
||||||
DbName: dbName,
|
DbName: dbName,
|
||||||
DbDialect: "mysql",
|
DbDialect: "mysql",
|
||||||
|
CustomLanguages: customLangs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,4 +8,5 @@ type Config struct {
|
|||||||
DbPassword string
|
DbPassword string
|
||||||
DbName string
|
DbName string
|
||||||
DbDialect string
|
DbDialect string
|
||||||
|
CustomLanguages map[string]string
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -25,12 +26,30 @@ type Heartbeat struct {
|
|||||||
Editor string `json:"editor"`
|
Editor string `json:"editor"`
|
||||||
OperatingSystem string `json:"operating_system"`
|
OperatingSystem string `json:"operating_system"`
|
||||||
Time *HeartbeatReqTime `json:"time" gorm:"type:timestamp; default:now(); index:idx_time,idx_time_user"`
|
Time *HeartbeatReqTime `json:"time" gorm:"type:timestamp; default:now(); index:idx_time,idx_time_user"`
|
||||||
|
languageRegex *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Heartbeat) Valid() bool {
|
func (h *Heartbeat) Valid() bool {
|
||||||
return h.User != nil && h.UserID != "" && h.Time != nil
|
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 {
|
func (j *HeartbeatReqTime) UnmarshalJSON(b []byte) error {
|
||||||
s := strings.Split(strings.Trim(string(b), "\""), ".")[0]
|
s := strings.Split(strings.Trim(string(b), "\""), ".")[0]
|
||||||
i, err := strconv.ParseInt(s, 10, 64)
|
i, err := strconv.ParseInt(s, 10, 64)
|
||||||
|
@ -32,13 +32,14 @@ func (h *HeartbeatHandler) Post(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, h := range heartbeats {
|
for _, hb := range heartbeats {
|
||||||
h.OperatingSystem = opSys
|
hb.OperatingSystem = opSys
|
||||||
h.Editor = editor
|
hb.Editor = editor
|
||||||
h.User = user
|
hb.User = user
|
||||||
h.UserID = user.ID
|
hb.UserID = user.ID
|
||||||
|
hb.Augment(h.HeartbeatSrvc.Config.CustomLanguages)
|
||||||
|
|
||||||
if !h.Valid() {
|
if !hb.Valid() {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("Invalid heartbeat object."))
|
w.Write([]byte("Invalid heartbeat object."))
|
||||||
return
|
return
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
const TableHeartbeat = "heartbeat"
|
const TableHeartbeat = "heartbeat"
|
||||||
|
|
||||||
type HeartbeatService struct {
|
type HeartbeatService struct {
|
||||||
Cofnig *models.Config
|
Config *models.Config
|
||||||
Db *gorm.DB
|
Db *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user