diff --git a/config.ini b/config.ini index 27b018b..4cf8e96 100644 --- a/config.ini +++ b/config.ini @@ -1,3 +1,6 @@ [server] listen = 127.0.0.1 -port = 3000 \ No newline at end of file +port = 3000 + +[languages] +vue = Vue \ No newline at end of file diff --git a/main.go b/main.go index dcb3bf1..58f6f7e 100644 --- a/main.go +++ b/main.go @@ -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, } } diff --git a/middlewares/authenticate.go b/middlewares/authenticate.go index ec7ed12..a6e8a63 100644 --- a/middlewares/authenticate.go +++ b/middlewares/authenticate.go @@ -14,8 +14,8 @@ import ( ) type AuthenticateMiddleware struct { - UserSrvc *services.UserService - Cache *cache.Cache + UserSrvc *services.UserService + Cache *cache.Cache Initialized bool } diff --git a/models/config.go b/models/config.go index f719ce9..dd61204 100644 --- a/models/config.go +++ b/models/config.go @@ -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 } diff --git a/models/heartbeat.go b/models/heartbeat.go index cdb7463..c121c09 100644 --- a/models/heartbeat.go +++ b/models/heartbeat.go @@ -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) diff --git a/routes/heartbeat.go b/routes/heartbeat.go index 5537b69..3c4ea51 100644 --- a/routes/heartbeat.go +++ b/routes/heartbeat.go @@ -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 diff --git a/services/heartbeat.go b/services/heartbeat.go index 8f5ef1f..267cb0e 100644 --- a/services/heartbeat.go +++ b/services/heartbeat.go @@ -11,7 +11,7 @@ import ( const TableHeartbeat = "heartbeat" type HeartbeatService struct { - Cofnig *models.Config + Config *models.Config Db *gorm.DB }