Commit a205e8f40ddf1621558e6c29c3fed7aa2134f1a6

Authored by Marko Tikvić
1 parent f84e7607d3
Exists in master and in 1 other branch v2

changes

... ... @@ -21,20 +21,21 @@ var secret = "webutility"
21 21  
22 22 type Role struct {
23 23 Name string `json:"name"`
24   - ID int32 `json:"id"`
  24 + ID int64 `json:"id"`
25 25 }
26 26  
27 27 // TokenClaims are JWT token claims.
28 28 type TokenClaims struct {
  29 + // extending a struct
  30 + jwt.StandardClaims
  31 +
  32 + // custom claims
29 33 Token string `json:"access_token"`
30 34 TokenType string `json:"token_type"`
31 35 Username string `json:"username"`
32 36 Role string `json:"role"`
33   - RoleID int32 `json:"role_id"`
  37 + RoleID int64 `json:"role_id"`
34 38 ExpiresIn int64 `json:"expires_in"`
35   -
36   - // extending a struct
37   - jwt.StandardClaims
38 39 }
39 40  
40 41 func InitJWT(appName, secret string) {
... ...
format_utility.go
1 1 package webutility
2 2  
3 3 import (
4   - "time"
5 4 "fmt"
  5 + "time"
6 6 )
7 7  
8 8 // UnixToDate converts given Unix time to local time in format and returns result:
... ... @@ -14,7 +14,10 @@ func UnixToDate(unix int64) time.Time {
14 14 // DateToUnix converts given date in Unix timestamp.
15 15 func DateToUnix(date interface{}) int64 {
16 16 if date != nil {
17   - t := date.(time.Time)
  17 + t, ok := date.(time.Time)
  18 + if !ok {
  19 + return 0
  20 + }
18 21 return t.Unix()
19 22  
20 23 }
... ... @@ -45,4 +48,3 @@ func LikeQuotes(stmt string) string {
45 48 }
46 49 return stmt
47 50 }
48   -
... ...
... ... @@ -28,6 +28,14 @@ func SetDefaultHeaders(w http.ResponseWriter) {
28 28 w.Header().Set("Content-Type", "application/json; charset=utf-8")
29 29 }
30 30  
  31 +func ReqLocale(req *http.Request, dflt string) string {
  32 + loc := req.FormValue("locale")
  33 + if loc == "" {
  34 + return dflt
  35 + }
  36 + return loc
  37 +}
  38 +
31 39 // 2xx
32 40 func Success(w http.ResponseWriter, payload *Payload, code int) {
33 41 w.WriteHeader(code)
... ...
... ... @@ -29,7 +29,7 @@ var logger *gologger.Logger
29 29  
30 30 func init() {
31 31 var err error
32   - logger, err = gologger.New("metadata", gologger.MaxLogSize100KB)
  32 + logger, err = gologger.New("webutility", gologger.MaxLogSize100KB)
33 33 if err != nil {
34 34 fmt.Printf("webutility: %s\n", err.Error())
35 35 }
... ...
locale_utility.go
... ... @@ -2,11 +2,14 @@ package webutility
2 2  
3 3 import (
4 4 "encoding/json"
  5 + "errors"
5 6 "io/ioutil"
6 7 )
7 8  
8 9 type Dictionary struct {
9   - locales map[string]map[string]string
  10 + locales map[string]map[string]string
  11 + supported []string
  12 + defaultLocale string
10 13 }
11 14  
12 15 func NewDictionary() Dictionary {
... ... @@ -32,6 +35,7 @@ func (d *Dictionary) AddLocale(loc, filePath string) error {
32 35 l[k] = v.(string)
33 36 }
34 37 d.locales[loc] = l
  38 + d.supported = append(d.supported, loc)
35 39  
36 40 return nil
37 41 }
... ... @@ -40,11 +44,23 @@ func (d *Dictionary) Translate(loc, key string) string {
40 44 return d.locales[loc][key]
41 45 }
42 46  
43   -func (d *Dictionary) SupportsLocale(loc string) bool {
44   - for k, _ := range d.locales {
45   - if k == loc {
  47 +func (d *Dictionary) HasLocale(loc string) bool {
  48 + for _, v := range d.supported {
  49 + if v == loc {
46 50 return true
47 51 }
48 52 }
49 53 return false
50 54 }
  55 +
  56 +func (d *Dictionary) SetDefaultLocale(loc string) error {
  57 + if !d.HasLocale(loc) {
  58 + return errors.New("dictionary does not contain translations for " + loc)
  59 + }
  60 + d.defaultLocale = loc
  61 + return nil
  62 +}
  63 +
  64 +func (d *Dictionary) GetDefaultLocale() string {
  65 + return d.defaultLocale
  66 +}
... ...