diff --git a/auth_utility.go b/auth_utility.go index d4abaaf..387392d 100644 --- a/auth_utility.go +++ b/auth_utility.go @@ -21,20 +21,21 @@ var secret = "webutility" type Role struct { Name string `json:"name"` - ID int32 `json:"id"` + ID int64 `json:"id"` } // TokenClaims are JWT token claims. type TokenClaims struct { + // extending a struct + jwt.StandardClaims + + // custom claims Token string `json:"access_token"` TokenType string `json:"token_type"` Username string `json:"username"` Role string `json:"role"` - RoleID int32 `json:"role_id"` + RoleID int64 `json:"role_id"` ExpiresIn int64 `json:"expires_in"` - - // extending a struct - jwt.StandardClaims } func InitJWT(appName, secret string) { diff --git a/format_utility.go b/format_utility.go index 99a4126..b40dbf3 100644 --- a/format_utility.go +++ b/format_utility.go @@ -1,8 +1,8 @@ package webutility import ( - "time" "fmt" + "time" ) // UnixToDate converts given Unix time to local time in format and returns result: @@ -14,7 +14,10 @@ func UnixToDate(unix int64) time.Time { // DateToUnix converts given date in Unix timestamp. func DateToUnix(date interface{}) int64 { if date != nil { - t := date.(time.Time) + t, ok := date.(time.Time) + if !ok { + return 0 + } return t.Unix() } @@ -45,4 +48,3 @@ func LikeQuotes(stmt string) string { } return stmt } - diff --git a/http_utility.go b/http_utility.go index 8ca7e55..07a79ab 100644 --- a/http_utility.go +++ b/http_utility.go @@ -28,6 +28,14 @@ func SetDefaultHeaders(w http.ResponseWriter) { w.Header().Set("Content-Type", "application/json; charset=utf-8") } +func ReqLocale(req *http.Request, dflt string) string { + loc := req.FormValue("locale") + if loc == "" { + return dflt + } + return loc +} + // 2xx func Success(w http.ResponseWriter, payload *Payload, code int) { w.WriteHeader(code) diff --git a/json_utility.go b/json_utility.go index 71d848c..2fbeb45 100644 --- a/json_utility.go +++ b/json_utility.go @@ -29,7 +29,7 @@ var logger *gologger.Logger func init() { var err error - logger, err = gologger.New("metadata", gologger.MaxLogSize100KB) + logger, err = gologger.New("webutility", gologger.MaxLogSize100KB) if err != nil { fmt.Printf("webutility: %s\n", err.Error()) } diff --git a/locale_utility.go b/locale_utility.go index 2c0b64e..298f4b9 100644 --- a/locale_utility.go +++ b/locale_utility.go @@ -2,11 +2,14 @@ package webutility import ( "encoding/json" + "errors" "io/ioutil" ) type Dictionary struct { - locales map[string]map[string]string + locales map[string]map[string]string + supported []string + defaultLocale string } func NewDictionary() Dictionary { @@ -32,6 +35,7 @@ func (d *Dictionary) AddLocale(loc, filePath string) error { l[k] = v.(string) } d.locales[loc] = l + d.supported = append(d.supported, loc) return nil } @@ -40,11 +44,23 @@ func (d *Dictionary) Translate(loc, key string) string { return d.locales[loc][key] } -func (d *Dictionary) SupportsLocale(loc string) bool { - for k, _ := range d.locales { - if k == loc { +func (d *Dictionary) HasLocale(loc string) bool { + for _, v := range d.supported { + if v == loc { return true } } return false } + +func (d *Dictionary) SetDefaultLocale(loc string) error { + if !d.HasLocale(loc) { + return errors.New("dictionary does not contain translations for " + loc) + } + d.defaultLocale = loc + return nil +} + +func (d *Dictionary) GetDefaultLocale() string { + return d.defaultLocale +}