Commit 1b7dfab733de136a9d3547b16f7373d8f8252803

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

Payload changed to inteface{}

Showing 4 changed files with 34 additions and 45 deletions   Show diff stats
... ... @@ -37,20 +37,20 @@ func ReqLocale(req *http.Request, dflt string) string {
37 37 }
38 38  
39 39 // 2xx
40   -func Success(w http.ResponseWriter, payload *Payload, code int) {
  40 +func Success(w http.ResponseWriter, payload interface{}, code int) {
41 41 w.WriteHeader(code)
42 42 if payload != nil {
43   - json.NewEncoder(w).Encode(*payload)
  43 + json.NewEncoder(w).Encode(payload)
44 44 }
45 45 }
46 46  
47 47 // 200
48   -func OK(w http.ResponseWriter, payload *Payload) {
  48 +func OK(w http.ResponseWriter, payload interface{}) {
49 49 Success(w, payload, http.StatusOK)
50 50 }
51 51  
52 52 // 201
53   -func Created(w http.ResponseWriter, payload *Payload) {
  53 +func Created(w http.ResponseWriter, payload interface{}) {
54 54 Success(w, payload, http.StatusCreated)
55 55 }
56 56  
... ... @@ -66,11 +66,6 @@ func BadRequest(w http.ResponseWriter, r *http.Request, err string) {
66 66 Error(w, r, http.StatusBadRequest, err)
67 67 }
68 68  
69   -// 404
70   -func NotFound(w http.ResponseWriter, r *http.Request, err string) {
71   - Error(w, r, http.StatusNotFound, err)
72   -}
73   -
74 69 // 401
75 70 func Unauthorized(w http.ResponseWriter, r *http.Request, err string) {
76 71 Error(w, r, http.StatusUnauthorized, err)
... ... @@ -81,7 +76,12 @@ func Forbidden(w http.ResponseWriter, r *http.Request, err string) {
81 76 Error(w, r, http.StatusForbidden, err)
82 77 }
83 78  
84   -// 403
  79 +// 404
  80 +func NotFound(w http.ResponseWriter, r *http.Request, err string) {
  81 + Error(w, r, http.StatusNotFound, err)
  82 +}
  83 +
  84 +// 409
85 85 func Conflict(w http.ResponseWriter, r *http.Request, err string) {
86 86 Error(w, r, http.StatusConflict, err)
87 87 }
... ...
... ... @@ -9,6 +9,8 @@ import (
9 9 "net/http"
10 10 "sync"
11 11 "time"
  12 +
  13 + "git.to-net.rs/marko.tikvic/gologger"
12 14 )
13 15  
14 16 var (
... ... @@ -21,6 +23,7 @@ var (
21 23  
22 24 inited bool
23 25 driver string
  26 + logger *gologger.Logger
24 27 )
25 28  
26 29 type LangMap map[string]map[string]string
... ... @@ -75,16 +78,24 @@ func DecodeJSON(r io.Reader, v interface{}) error {
75 78  
76 79 // InitPayloadsMetadata loads all payloads' information into 'metadata' variable.
77 80 func InitPayloadsMetadata(drv string, db *sql.DB, project string) error {
  81 + var err error
78 82 if drv != "ora" && drv != "mysql" {
79   - return errors.New("driver not supported")
  83 + err = errors.New("driver not supported")
  84 + return err
80 85 }
  86 +
81 87 driver = drv
82 88 metadataDB = db
83 89 activeProject = project
84 90  
  91 + logger, err = gologger.New("metadata", gologger.MaxLogSize100KB)
  92 + if err != nil {
  93 + fmt.Printf("webutility: %s\n", err.Error())
  94 + }
  95 +
85 96 mu.Lock()
86 97 defer mu.Unlock()
87   - err := initMetadata(project)
  98 + err = initMetadata(project)
88 99 if err != nil {
89 100 return err
90 101 }
... ... @@ -149,11 +160,8 @@ func UpdateEntityModels(command string) (total, upd, add int, err error) {
149 160 }
150 161 }
151 162 for _, k := range toUpdate {
152   - //fmt.Printf("Updating: %s\n", k)
153   - //fmt.Printf("New model: %s\n", updateQue[k])
154 163 _, err = uStmt.Exec(string(updateQue[k]), k)
155 164 if err != nil {
156   - logger.Log("webutility: %v\n", err)
157 165 return
158 166 }
159 167 upd++
... ... @@ -175,7 +183,6 @@ func UpdateEntityModels(command string) (total, upd, add int, err error) {
175 183 for _, k := range toAdd {
176 184 _, err = iStmt.Exec(activeProject, string(blankPayload), k, string(updateQue[k]))
177 185 if err != nil {
178   - logger.Log("webutility: %v\n", err)
179 186 return
180 187 }
181 188 metadata[k] = Payload{}
... ... @@ -196,8 +203,6 @@ func initMetadata(project string) error {
196 203 }
197 204 defer rows.Close()
198 205  
199   - count := 0
200   - success := 0
201 206 if len(metadata) > 0 {
202 207 metadata = nil
203 208 }
... ... @@ -211,13 +216,9 @@ func initMetadata(project string) error {
211 216 if err != nil {
212 217 logger.Log("webutility: couldn't init: '%s' metadata: %s:\n%s\n", name, err.Error(), load)
213 218 } else {
214   - success++
215 219 metadata[name] = p
216 220 }
217   - count++
218 221 }
219   - perc := float32(success) / float32(count) * 100.0
220   - logger.Log("webutility: loaded %d/%d (%.1f%%) entities\n", success, count, perc)
221 222  
222 223 return nil
223 224 }
... ...
... ... @@ -1,22 +0,0 @@
1   -package webutility
2   -
3   -import (
4   - "fmt"
5   -
6   - "git.to-net.rs/marko.tikvic/gologger"
7   -)
8   -
9   -var logger *gologger.Logger
10   -var reqLogger *gologger.Logger
11   -
12   -func init() {
13   - var err error
14   - logger, err = gologger.New("metadata", gologger.MaxLogSize100KB)
15   - if err != nil {
16   - fmt.Printf("webutility: %s\n", err.Error())
17   - }
18   - reqLogger, err = gologger.New("http", gologger.MaxLogSize5MB)
19   - if err != nil {
20   - fmt.Printf("webutility: %s\n", err.Error())
21   - }
22   -}
... ... @@ -3,8 +3,12 @@ package webutility
3 3 import (
4 4 "net/http"
5 5 "time"
  6 +
  7 + "git.to-net.rs/marko.tikvic/gologger"
6 8 )
7 9  
  10 +var reqLogger *gologger.Logger
  11 +
8 12 func WithSetHeaders(h http.HandlerFunc) http.HandlerFunc {
9 13 return func(w http.ResponseWriter, req *http.Request) {
10 14 SetDefaultHeaders(w)
... ... @@ -26,6 +30,12 @@ func WithParseForm(h http.HandlerFunc) http.HandlerFunc {
26 30 }
27 31 }
28 32  
  33 +func EnableLogging(log string) error {
  34 + var err error
  35 + reqLogger, err = gologger.New(log, gologger.MaxLogSize5MB)
  36 + return err
  37 +}
  38 +
29 39 func WithLog(h http.HandlerFunc) http.HandlerFunc {
30 40 return func(w http.ResponseWriter, req *http.Request) {
31 41 reqLogger.LogRequest(req, "")
... ... @@ -36,7 +46,7 @@ func WithLog(h http.HandlerFunc) http.HandlerFunc {
36 46 }
37 47 }
38 48  
39   -func WithAuth(h http.HandlerFunc, authorizedRoles []string) http.HandlerFunc {
  49 +func WithAuth(authorizedRoles []string, h http.HandlerFunc) http.HandlerFunc {
40 50 return func(w http.ResponseWriter, req *http.Request) {
41 51 if _, ok := AuthCheck(req, authorizedRoles); !ok {
42 52 Unauthorized(w, req, "")
... ...