Add support for custom languages.

This commit is contained in:
Ferdinand Mütsch 2019-05-21 17:16:46 +02:00
parent c2b705f172
commit c821e02b82
7 changed files with 56 additions and 24 deletions

View File

@ -1,3 +1,6 @@
[server]
listen = 127.0.0.1
port = 3000
port = 3000
[languages]
vue = Vue

22
main.go
View File

@ -49,14 +49,22 @@ 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,
DbHost: dbHost,
DbUser: dbUser,
DbPassword: dbPassword,
DbName: dbName,
DbDialect: "mysql",
Port: port,
Addr: addr,
DbHost: dbHost,
DbUser: dbUser,
DbPassword: dbPassword,
DbName: dbName,
DbDialect: "mysql",
CustomLanguages: customLangs,
}
}

View File

@ -14,8 +14,8 @@ import (
)
type AuthenticateMiddleware struct {
UserSrvc *services.UserService
Cache *cache.Cache
UserSrvc *services.UserService
Cache *cache.Cache
Initialized bool
}

View File

@ -1,11 +1,12 @@
package models
type Config struct {
Port int
Addr string
DbHost string
DbUser string
DbPassword string
DbName string
DbDialect string
Port int
Addr string
DbHost string
DbUser string
DbPassword string
DbName string
DbDialect string
CustomLanguages map[string]string
}

View File

@ -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)

View File

@ -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

View File

@ -11,7 +11,7 @@ import (
const TableHeartbeat = "heartbeat"
type HeartbeatService struct {
Cofnig *models.Config
Config *models.Config
Db *gorm.DB
}