Commit ad8e9dd2af2320b920444b98a0d71d8c0a565734

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

added middleware and functions to support getting default values from nil interfaces

default_values.go
... ... @@ -0,0 +1,50 @@
  1 +package webutility
  2 +
  3 +func Int32ValueOrDefault(val *interface{}) (def int32) {
  4 + if *val != nil {
  5 + def = (*val).(int32)
  6 + }
  7 + return def
  8 +}
  9 +
  10 +func Int64ValueOrDefault(val *interface{}) (def int64) {
  11 + if *val != nil {
  12 + def = (*val).(int64)
  13 + }
  14 + return def
  15 +}
  16 +
  17 +func Uint32ValueOrDefault(val *interface{}) (def uint32) {
  18 + if *val != nil {
  19 + def = (*val).(uint32)
  20 + }
  21 + return def
  22 +}
  23 +
  24 +func Uint64ValueOrDefault(val *interface{}) (def uint64) {
  25 + if *val != nil {
  26 + def = (*val).(uint64)
  27 + }
  28 + return def
  29 +}
  30 +
  31 +func Float32ValueOrDefault(val *interface{}) (def float32) {
  32 + if *val != nil {
  33 + def = (*val).(float32)
  34 + }
  35 + return def
  36 +}
  37 +
  38 +func Float64ValueOrDefault(val *interface{}) (def float64) {
  39 + if *val != nil {
  40 + return (*val).(float64)
  41 + }
  42 + return def
  43 +}
  44 +
  45 +func StringValueOrDefault(val *interface{}) (def string) {
  46 + if *val != nil {
  47 + def = (*val).(string)
  48 + }
  49 + return def
  50 +}
... ...
... ... @@ -2,6 +2,7 @@ package webutility
2 2  
3 3 import (
4 4 "encoding/json"
  5 + "fmt"
5 6 "net/http"
6 7 )
7 8  
... ... @@ -10,21 +11,20 @@ type webError struct {
10 11 Error string `json:"error"`
11 12 }
12 13  
13   -// NotFoundHandler writes HTTP error 404 to w.
14   -func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
  14 +// NotFoundHandlerFunc writes HTTP error 404 to w.
  15 +func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) {
15 16 SetDefaultHeaders(w)
16 17 if req.Method == "OPTIONS" {
17 18 return
18 19 }
19   - NotFound(w, req, "Not found")
  20 + NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String()))
20 21 }
21 22  
22 23 // SetDefaultHeaders set's default headers for an HTTP response.
23 24 func SetDefaultHeaders(w http.ResponseWriter) {
24 25 w.Header().Set("Access-Control-Allow-Origin", "*")
25 26 w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
26   - w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type,
27   - Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`)
  27 + w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`)
28 28 w.Header().Set("Content-Type", "application/json; charset=utf-8")
29 29 }
30 30  
... ...
... ... @@ -9,8 +9,6 @@ import (
9 9 "net/http"
10 10 "sync"
11 11 "time"
12   -
13   - "git.to-net.rs/marko.tikvic/gologger"
14 12 )
15 13  
16 14 var (
... ... @@ -25,16 +23,6 @@ var (
25 23 driver string
26 24 )
27 25  
28   -var logger *gologger.Logger
29   -
30   -func init() {
31   - var err error
32   - logger, err = gologger.New("webutility", gologger.MaxLogSize100KB)
33   - if err != nil {
34   - fmt.Printf("webutility: %s\n", err.Error())
35   - }
36   -}
37   -
38 26 type LangMap map[string]map[string]string
39 27  
40 28 type Field struct {
... ... @@ -68,6 +56,10 @@ type Payload struct {
68 56 Data interface{} `json:"data"`
69 57 }
70 58  
  59 +func (p *Payload) SetData(data interface{}) {
  60 + p.Data = data
  61 +}
  62 +
71 63 // NewPayload returs a payload sceleton for entity described with etype.
72 64 func NewPayload(r *http.Request, etype string) Payload {
73 65 pload := metadata[etype]
... ...
... ... @@ -12,8 +12,8 @@ type Dictionary struct {
12 12 defaultLocale string
13 13 }
14 14  
15   -func NewDictionary() Dictionary {
16   - return Dictionary{
  15 +func NewDictionary() *Dictionary {
  16 + return &Dictionary{
17 17 locales: map[string]map[string]string{},
18 18 }
19 19 }
... ...
... ... @@ -0,0 +1,22 @@
  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 +}
... ...
... ... @@ -0,0 +1,47 @@
  1 +package webutility
  2 +
  3 +import (
  4 + "net/http"
  5 + "time"
  6 +)
  7 +
  8 +func WithSetHeaders(h http.Handler) http.Handler {
  9 + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  10 + SetDefaultHeaders(w)
  11 + if req.Method == http.MethodOptions {
  12 + return
  13 + }
  14 + h.ServeHTTP(w, req)
  15 + })
  16 +}
  17 +
  18 +func WithParseForm(h http.Handler) http.Handler {
  19 + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  20 + err := req.ParseForm()
  21 + if err != nil {
  22 + BadRequest(w, req, err.Error())
  23 + return
  24 + }
  25 + h.ServeHTTP(w, req)
  26 + })
  27 +}
  28 +
  29 +func WithLog(h http.Handler) http.Handler {
  30 + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  31 + reqLogger.LogRequest(req, "")
  32 + t1 := time.Now()
  33 + h.ServeHTTP(w, req)
  34 + t2 := time.Now()
  35 + reqLogger.LogResponse(w, t2.Sub(t1))
  36 + })
  37 +}
  38 +
  39 +func WithAuth(h http.Handler, authorizedRoles []string) http.Handler {
  40 + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  41 + if _, ok := AuthCheck(req, authorizedRoles); !ok {
  42 + Unauthorized(w, req, "")
  43 + return
  44 + }
  45 + h.ServeHTTP(w, req)
  46 + })
  47 +}
... ...