Commit b3f1275cdad84845a70b20f51afd17937363c502

Authored by Marko Tikvić
1 parent 776b4c95bd
Exists in master

refactored middleware

middleware.go
... ... @@ -1,81 +0,0 @@
1   -package webutility
2   -
3   -import (
4   - "net/http"
5   - "time"
6   -
7   - "git.to-net.rs/marko.tikvic/gologger"
8   -)
9   -
10   -var httpLogger *gologger.Logger
11   -
12   -// SetHeaders ...
13   -func SetHeaders(h http.HandlerFunc) http.HandlerFunc {
14   - return func(w http.ResponseWriter, req *http.Request) {
15   - SetDefaultHeaders(w)
16   - if req.Method == http.MethodOptions {
17   - return
18   - }
19   - h(w, req)
20   - }
21   -}
22   -
23   -// ParseForm ...
24   -func ParseForm(h http.HandlerFunc) http.HandlerFunc {
25   - return func(w http.ResponseWriter, req *http.Request) {
26   - err := req.ParseForm()
27   - if err != nil {
28   - BadRequest(w, req, err.Error())
29   - return
30   - }
31   - h(w, req)
32   - }
33   -}
34   -
35   -// ParseMultipartForm ...
36   -func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc {
37   - return func(w http.ResponseWriter, req *http.Request) {
38   - err := req.ParseMultipartForm(32 << 20)
39   - if err != nil {
40   - BadRequest(w, req, err.Error())
41   - return
42   - }
43   - h(w, req)
44   - }
45   -}
46   -
47   -// EnableLogging ...
48   -func EnableLogging(log string) (err error) {
49   - httpLogger, err = gologger.New(log, gologger.MaxLogSize5MB)
50   - return err
51   -}
52   -
53   -// Log ...
54   -func Log(h http.HandlerFunc) http.HandlerFunc {
55   - return func(w http.ResponseWriter, req *http.Request) {
56   - t1 := time.Now()
57   -
58   - claims, _ := GetTokenClaims(req)
59   - in := httpLogger.LogHTTPRequest(req, claims.Username)
60   -
61   - rec := NewStatusRecorder(w)
62   -
63   - h(rec, req)
64   -
65   - t2 := time.Now()
66   - out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size())
67   -
68   - httpLogger.CombineHTTPLogs(in, out)
69   - }
70   -}
71   -
72   -// Auth ...
73   -func Auth(roles string, h http.HandlerFunc) http.HandlerFunc {
74   - return func(w http.ResponseWriter, req *http.Request) {
75   - if _, err := AuthCheck(req, roles); err != nil {
76   - Unauthorized(w, req, err.Error())
77   - return
78   - }
79   - h(w, req)
80   - }
81   -}
middleware/main.go
... ... @@ -2,22 +2,20 @@ package middleware
2 2  
3 3 import (
4 4 "net/http"
5   -
6   - web "git.to-net.rs/marko.tikvic/webutility"
7 5 )
8 6  
9 7 func Headers(h http.HandlerFunc) http.HandlerFunc {
10   - return web.SetHeaders(web.ParseForm(h))
  8 + return IgnoreOptionsRequests(ParseForm(h))
11 9 }
12 10  
13 11 func AuthOnly(roles string, h http.HandlerFunc) http.HandlerFunc {
14   - return web.SetHeaders(web.ParseForm(web.Auth(roles, h)))
  12 + return IgnoreOptionsRequests(ParseForm(Auth(roles, h)))
15 13 }
16 14  
17 15 func Full(roles string, h http.HandlerFunc) http.HandlerFunc {
18   - return web.SetHeaders(web.ParseForm(web.Log(web.Auth(roles, h))))
  16 + return IgnoreOptionsRequests(ParseForm(LogTraffic(Auth(roles, h))))
19 17 }
20 18  
21   -func Log(h http.HandlerFunc) http.HandlerFunc {
22   - return web.SetHeaders(web.ParseForm(web.Log(h)))
  19 +func LogTraffic(h http.HandlerFunc) http.HandlerFunc {
  20 + return IgnoreOptionsRequests(ParseForm(LogRequestAndResponse(h)))
23 21 }
... ...
middleware/middleware.go
... ... @@ -0,0 +1,86 @@
  1 +package middleware
  2 +
  3 +import (
  4 + "net/http"
  5 + "time"
  6 +
  7 + "git.to-net.rs/marko.tikvic/gologger"
  8 +
  9 + web "git.to-net.rs/marko.tikvic/webutility"
  10 +)
  11 +
  12 +var httpLogger *gologger.Logger
  13 +
  14 +// IgnoreOptionsRequests ...
  15 +func IgnoreOptionsRequests(h http.HandlerFunc) http.HandlerFunc {
  16 + return func(w http.ResponseWriter, req *http.Request) {
  17 + web.SetDefaultHeaders(w)
  18 + if req.Method == http.MethodOptions {
  19 + return
  20 + }
  21 + h(w, req)
  22 + }
  23 +}
  24 +
  25 +// ParseForm ...
  26 +func ParseForm(h http.HandlerFunc) http.HandlerFunc {
  27 + return func(w http.ResponseWriter, req *http.Request) {
  28 + err := req.ParseForm()
  29 + if err != nil {
  30 + web.BadRequest(w, req, err.Error())
  31 + return
  32 + }
  33 + h(w, req)
  34 + }
  35 +}
  36 +
  37 +// ParseMultipartForm ...
  38 +func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc {
  39 + return func(w http.ResponseWriter, req *http.Request) {
  40 + err := req.ParseMultipartForm(32 << 20)
  41 + if err != nil {
  42 + web.BadRequest(w, req, err.Error())
  43 + return
  44 + }
  45 + h(w, req)
  46 + }
  47 +}
  48 +
  49 +// SetLogger ...
  50 +func SetLogger(logger *gologger.Logger) {
  51 + httpLogger = logger
  52 +}
  53 +
  54 +// LogRequestAndResponse ...
  55 +func LogRequestAndResponse(h http.HandlerFunc) http.HandlerFunc {
  56 + return func(w http.ResponseWriter, req *http.Request) {
  57 + if httpLogger != nil {
  58 + t1 := time.Now()
  59 +
  60 + claims, _ := web.GetTokenClaims(req)
  61 + in := httpLogger.LogHTTPRequest(req, claims.Username)
  62 +
  63 + rec := web.NewStatusRecorder(w)
  64 +
  65 + h(rec, req)
  66 +
  67 + t2 := time.Now()
  68 + out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size())
  69 +
  70 + httpLogger.CombineHTTPLogs(in, out)
  71 + } else {
  72 + h(w, req)
  73 + }
  74 + }
  75 +}
  76 +
  77 +// Auth ...
  78 +func Auth(roles string, h http.HandlerFunc) http.HandlerFunc {
  79 + return func(w http.ResponseWriter, req *http.Request) {
  80 + if _, err := web.AuthCheck(req, roles); err != nil {
  81 + web.Unauthorized(w, req, err.Error())
  82 + return
  83 + }
  84 + h(w, req)
  85 + }
  86 +}
... ...
... ... @@ -10,8 +10,6 @@ import (
10 10 "strings"
11 11 "sync"
12 12 "time"
13   -
14   - "git.to-net.rs/marko.tikvic/gologger"
15 13 )
16 14  
17 15 var (
... ... @@ -25,7 +23,6 @@ var (
25 23  
26 24 inited bool
27 25 metaDriver string
28   - logger *gologger.Logger
29 26 )
30 27  
31 28 // LangMap ...
... ... @@ -143,11 +140,6 @@ func InitPayloadsMetadata(drv string, db *sql.DB, project string) error {
143 140 metadataDB = db
144 141 activeProject = project
145 142  
146   - logger, err = gologger.New("metadata", gologger.MaxLogSize100KB)
147   - if err != nil {
148   - fmt.Printf("webutility: %s\n", err.Error())
149   - }
150   -
151 143 mu.Lock()
152 144 defer mu.Unlock()
153 145 err = initMetadata(project)
... ... @@ -211,20 +203,17 @@ func UpdateEntityModels(command string) (total, upd, add int, err error) {
211 203 if metaDriver == "ora" {
212 204 uStmt, err = metadataDB.Prepare("update entities set entity_model = :1 where entity_type = :2")
213 205 if err != nil {
214   - logger.Trace(err.Error())
215 206 return
216 207 }
217 208 } else if metaDriver == "mysql" {
218 209 uStmt, err = metadataDB.Prepare("update entities set entity_model = ? where entity_type = ?")
219 210 if err != nil {
220   - logger.Trace(err.Error())
221 211 return
222 212 }
223 213 }
224 214 for _, k := range toUpdate {
225 215 _, err = uStmt.Exec(string(updateQue[k]), k)
226 216 if err != nil {
227   - logger.Trace(err.Error())
228 217 return
229 218 }
230 219 upd++
... ... @@ -235,20 +224,17 @@ func UpdateEntityModels(command string) (total, upd, add int, err error) {
235 224 if metaDriver == "ora" {
236 225 iStmt, err = metadataDB.Prepare("insert into entities(projekat, metadata, entity_type, entity_model) values(:1, :2, :3, :4)")
237 226 if err != nil {
238   - logger.Trace(err.Error())
239 227 return
240 228 }
241 229 } else if metaDriver == "mysql" {
242 230 iStmt, err = metadataDB.Prepare("insert into entities(projekat, metadata, entity_type, entity_model) values(?, ?, ?, ?)")
243 231 if err != nil {
244   - logger.Trace(err.Error())
245 232 return
246 233 }
247 234 }
248 235 for _, k := range toAdd {
249 236 _, err = iStmt.Exec(activeProject, string(blankPayload), k, string(updateQue[k]))
250 237 if err != nil {
251   - logger.Trace(err.Error())
252 238 return
253 239 }
254 240 metadata[k] = Payload{}
... ... @@ -280,7 +266,7 @@ func initMetadata(project string) error {
280 266 p := Payload{}
281 267 err := json.Unmarshal([]byte(load), &p)
282 268 if err != nil {
283   - logger.Log("webutility: couldn't init: '%s' metadata: %s:\n%s\n", name, err.Error(), load)
  269 + fmt.Printf("webutility: couldn't init: '%s' metadata: %s:\n%s\n", name, err.Error(), load)
284 270 } else {
285 271 metadata[name] = p
286 272 }
... ... @@ -353,7 +339,7 @@ func hotload(n int) {
353 339 entity_type
354 340 from entities where projekat = ` + fmt.Sprintf("'%s'", activeProject))
355 341 if err != nil {
356   - logger.Log("webutility: hotload failed: %v\n", err)
  342 + fmt.Printf("webutility: hotload failed: %v\n", err)
357 343 time.Sleep(time.Duration(n) * time.Second)
358 344 continue
359 345 }
... ... @@ -372,7 +358,7 @@ func hotload(n int) {
372 358 rows.Close()
373 359  
374 360 if rows.Err() != nil {
375   - logger.Log("webutility: hotload rset error: %v\n", rows.Err())
  361 + fmt.Printf("webutility: hotload rset error: %v\n", rows.Err())
376 362 time.Sleep(time.Duration(n) * time.Second)
377 363 continue
378 364 }
... ... @@ -398,7 +384,7 @@ func refreshMetadata(entities []string) {
398 384 ` and entity_type = ` + fmt.Sprintf("'%s'", e))
399 385  
400 386 if err != nil {
401   - logger.Log("webutility: refresh: prep: %v\n", err)
  387 + fmt.Printf("webutility: refresh: prep: %v\n", err)
402 388 rows.Close()
403 389 continue
404 390 }
... ... @@ -409,7 +395,7 @@ func refreshMetadata(entities []string) {
409 395 p := Payload{}
410 396 err := json.Unmarshal([]byte(load), &p)
411 397 if err != nil {
412   - logger.Log("webutility: couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), load)
  398 + fmt.Printf("webutility: couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), load)
413 399 } else {
414 400 metadata[e] = p
415 401 }
... ...