Commit 25e0015504e59050a2b2e299e6bb89ad2522282f

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

exported everything

format_utility.go
... ... @@ -6,15 +6,11 @@ import (
6 6 "strconv"
7 7 )
8 8  
9   -////
10   -//// TIME FORMAT UTILITY
11   -////
12   -
13   -func unixToDate(input int64) time.Time {
  9 +func UnixToDate(input int64) time.Time {
14 10 return time.Unix(input, 0)
15 11 }
16 12  
17   -func dateToUnix(input interface{}) int64 {
  13 +func DateToUnix(input interface{}) int64 {
18 14 if input != nil {
19 15 t := input.(time.Time)
20 16 return t.Unix()
... ... @@ -23,32 +19,14 @@ func dateToUnix(input interface{}) int64 {
23 19 return 0
24 20 }
25 21  
26   -func aersDate(unixString string) (string, error) {
27   - unixTime, err := strconv.ParseInt(unixString, 10, 64)
28   - if err != nil {
29   - return "", err
30   - }
31   -
32   - date := unixToDate(unixTime).String()
33   - tokens := strings.Split(date, "-")
34   - dateString := tokens[0] + tokens[1] + strings.Split(tokens[2], " ")[0]
35   -
36   - return dateString, nil
37   -}
38   -
39   -////
40   -//// STRING UTILITY
41   -////
42   -
43   -// surrondWithSingleQuotes is used when url param is of type string
44   -func putQuotes(input string) string {
  22 +func EqualQuotes(input string) string {
45 23 if input != "" {
46 24 return " = '" + input + "'"
47 25 }
48 26 return ""
49 27 }
50 28  
51   -func putLikeQuotes(input string) string {
  29 +func LikeQuotes(input string) string {
52 30 if input != "" {
53 31 return " LIKE UPPER('%" + input + "%')"
54 32 }
... ...
... ... @@ -5,7 +5,12 @@ import (
5 5 "encoding/json"
6 6 )
7 7  
8   -const APIVersion = "/api/v1"
  8 +var _apiVersion = "/api/v1"
  9 +
  10 +func SetApiVersion(ver string) string {
  11 + _apiVersion = ver
  12 + return _apiVeresion
  13 +}
9 14  
10 15 ////
11 16 //// ERROR UTILITY
... ... @@ -26,7 +31,7 @@ type HttpErrorDesc struct {
26 31 Desc string `json:"description"`
27 32 }
28 33  
29   -func respondWithHttpError(w http.ResponseWriter,
  34 +func RespondWithHttpError(w http.ResponseWriter,
30 35 req *http.Request,
31 36 code int,
32 37 httpErr []HttpErrorDesc) {
... ... @@ -39,35 +44,21 @@ func respondWithHttpError(w http.ResponseWriter,
39 44 json.NewEncoder(w).Encode(err)
40 45 }
41 46  
42   -func respondWithHttpError400(w http.ResponseWriter, req *http.Request) {
43   - respondWithHttpError(w, req, http.StatusBadRequest,
44   - []HttpErrorDesc{
45   - {
46   - Lang: "en",
47   - Desc: templateHttpErr400_EN,
48   - },
49   - {
50   - Lang: "rs",
51   - Desc: templateHttpErr400_RS,
52   - },
53   - })
  47 +func RespondWithHttpError400(w http.ResponseWriter, req *http.Request) {
  48 + RespondWithHttpError(w, req, http.StatusBadRequest, []HttpErrorDesc{
  49 + {Lang: "en", Desc: templateHttpErr400_EN},
  50 + {Lang: "rs", Desc: templateHttpErr400_RS},
  51 + })
54 52 }
55 53  
56   -func respondWithHttpError500(w http.ResponseWriter, req *http.Request) {
57   - respondWithHttpError(w, req, http.StatusInternalServerError,
58   - []HttpErrorDesc{
59   - {
60   - Lang: "en",
61   - Desc: templateHttpErr500_EN,
62   - },
63   - {
64   - Lang: "rs",
65   - Desc: templateHttpErr500_RS,
66   - },
67   - })
  54 +func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) {
  55 + RespondWithHttpError(w, req, http.StatusInternalServerError, []HttpErrorDesc{
  56 + {Lang: "en", Desc: templateHttpErr500_EN},
  57 + {Lang: "rs", Desc: templateHttpErr500_RS},
  58 + })
68 59 }
69 60  
70   -func deliverPayload(w http.ResponseWriter, payload JSONPayload) {
  61 +func DeliverPayload(w http.ResponseWriter, payload JSONPayload) {
71 62 json.NewEncoder(w).Encode(payload)
72 63 payload.Data = nil
73 64 }
... ... @@ -78,7 +69,7 @@ func deliverPayload(w http.ResponseWriter, payload JSONPayload) {
78 69  
79 70 // wrapHandlerFunc is as wrapper function for route handlers.
80 71 // Sets common headers and checks for token validity.
81   -func commonHttpWrap(fn http.HandlerFunc) http.HandlerFunc {
  72 +func HandleFuncWrap(fn http.HandlerFunc) http.HandlerFunc {
82 73 return func(w http.ResponseWriter, req *http.Request) {
83 74 // @TODO: check Content-type header (must be application/json)
84 75 // ctype := w.Header.Get("Content-Type")
... ... @@ -107,19 +98,13 @@ func commonHttpWrap(fn http.HandlerFunc) http.HandlerFunc {
107 98 return
108 99 }
109 100  
110   - if req.URL.Path != APIVersion + "/token/new" {
  101 + if req.URL.Path != _apiVersion + "/token/new" {
111 102 token := req.Header.Get("Authorization")
112   - if _, err := parseAPIToken(token); err != nil {
113   - respondWithHttpError(w, req, http.StatusUnauthorized,
  103 + if _, err := ParseAPIToken(token); err != nil {
  104 + RespondWithHttpError(w, req, http.StatusUnauthorized,
114 105 []HttpErrorDesc{
115   - {
116   - Lang: "en",
117   - Desc: "Unauthorized request.",
118   - },
119   - {
120   - Lang: "rs",
121   - Desc: "Neautorizovani zahtev.",
122   - },
  106 + {Lang: "en", Desc: "Unauthorized request."},
  107 + {Lang: "rs", Desc: "Neautorizovani zahtev."},
123 108 })
124 109 return
125 110 }
... ... @@ -127,16 +112,10 @@ func commonHttpWrap(fn http.HandlerFunc) http.HandlerFunc {
127 112  
128 113 err := req.ParseForm()
129 114 if err != nil {
130   - respondWithHttpError(w, req, http.StatusBadRequest,
  115 + RespondWithHttpError(w, req, http.StatusBadRequest,
131 116 []HttpErrorDesc{
132   - {
133   - Lang: "en",
134   - Desc: templateHttpErr400_EN,
135   - },
136   - {
137   - Lang: "rs",
138   - Desc: templateHttpErr400_RS,
139   - },
  117 + {Lang: "en", Desc: templateHttpErr400_EN},
  118 + {Lang: "rs", Desc: templateHttpErr400_RS},
140 119 })
141 120 return
142 121 }
... ... @@ -148,16 +127,9 @@ func commonHttpWrap(fn http.HandlerFunc) http.HandlerFunc {
148 127 //// NOT FOUND HANDLER
149 128 ////
150 129  
151   -func notFoundHandler(w http.ResponseWriter, req *http.Request) {
152   - respondWithHttpError(w, req, http.StatusNotFound,
153   - []HttpErrorDesc{
154   - {
155   - Lang: "en",
156   - Desc: "Not found.",
157   - },
158   - {
159   - Lang: "rs",
160   - Desc: "Traženi resurs ne postoji.",
161   - },
162   - })
  130 +func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
  131 + RespondWithHttpError(w, req, http.StatusNotFound, []HttpErrorDesc{
  132 + {Lang: "en", Desc: "Not found."},
  133 + {Lang: "rs", Desc: "Traženi resurs ne postoji."},
  134 + })
163 135 }
... ...
tables_utility.go
... ... @@ -82,20 +82,18 @@ func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField {
82 82 var _tables TableConfig
83 83 var _prevProject string
84 84  
85   -func loadTablesConfig(jsonbuf []byte) error {
  85 +func InitTables(jsonbuf []byte) error {
86 86 json.Unmarshal(jsonbuf, &_tables.Tables)
87   -
88 87 if len(_tables.Tables) == 0 {
89 88 return errors.New("tables config is corrupt")
90 89 }
91   -
92 90 return nil
93 91 }
94 92  
95   -func loadTable(table string) JSONParams {
  93 +func LoadTable(table string) JSONParams {
96 94 return NewJSONParams(_tables.LoadTranslations(table),
97   - _tables.LoadFields(table),
98   - _tables.LoadIdField(table),
99   - _tables.LoadCorrelations(table))
  95 + _tables.LoadFields(table),
  96 + _tables.LoadIdField(table),
  97 + _tables.LoadCorrelations(table))
100 98 }
101 99  
... ...