From 1b7dfab733de136a9d3547b16f7373d8f8252803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Wed, 9 May 2018 11:56:58 +0200 Subject: [PATCH] Payload changed to inteface{} --- http.go | 20 ++++++++++---------- json.go | 25 +++++++++++++------------ main.go | 22 ---------------------- middleware.go | 12 +++++++++++- 4 files changed, 34 insertions(+), 45 deletions(-) delete mode 100644 main.go diff --git a/http.go b/http.go index 482aac0..ea5c69e 100644 --- a/http.go +++ b/http.go @@ -37,20 +37,20 @@ func ReqLocale(req *http.Request, dflt string) string { } // 2xx -func Success(w http.ResponseWriter, payload *Payload, code int) { +func Success(w http.ResponseWriter, payload interface{}, code int) { w.WriteHeader(code) if payload != nil { - json.NewEncoder(w).Encode(*payload) + json.NewEncoder(w).Encode(payload) } } // 200 -func OK(w http.ResponseWriter, payload *Payload) { +func OK(w http.ResponseWriter, payload interface{}) { Success(w, payload, http.StatusOK) } // 201 -func Created(w http.ResponseWriter, payload *Payload) { +func Created(w http.ResponseWriter, payload interface{}) { Success(w, payload, http.StatusCreated) } @@ -66,11 +66,6 @@ func BadRequest(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusBadRequest, err) } -// 404 -func NotFound(w http.ResponseWriter, r *http.Request, err string) { - Error(w, r, http.StatusNotFound, err) -} - // 401 func Unauthorized(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusUnauthorized, err) @@ -81,7 +76,12 @@ func Forbidden(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusForbidden, err) } -// 403 +// 404 +func NotFound(w http.ResponseWriter, r *http.Request, err string) { + Error(w, r, http.StatusNotFound, err) +} + +// 409 func Conflict(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusConflict, err) } diff --git a/json.go b/json.go index 24699b1..e0a927b 100644 --- a/json.go +++ b/json.go @@ -9,6 +9,8 @@ import ( "net/http" "sync" "time" + + "git.to-net.rs/marko.tikvic/gologger" ) var ( @@ -21,6 +23,7 @@ var ( inited bool driver string + logger *gologger.Logger ) type LangMap map[string]map[string]string @@ -75,16 +78,24 @@ func DecodeJSON(r io.Reader, v interface{}) error { // InitPayloadsMetadata loads all payloads' information into 'metadata' variable. func InitPayloadsMetadata(drv string, db *sql.DB, project string) error { + var err error if drv != "ora" && drv != "mysql" { - return errors.New("driver not supported") + err = errors.New("driver not supported") + return err } + driver = drv metadataDB = db activeProject = project + logger, err = gologger.New("metadata", gologger.MaxLogSize100KB) + if err != nil { + fmt.Printf("webutility: %s\n", err.Error()) + } + mu.Lock() defer mu.Unlock() - err := initMetadata(project) + err = initMetadata(project) if err != nil { return err } @@ -149,11 +160,8 @@ func UpdateEntityModels(command string) (total, upd, add int, err error) { } } for _, k := range toUpdate { - //fmt.Printf("Updating: %s\n", k) - //fmt.Printf("New model: %s\n", updateQue[k]) _, err = uStmt.Exec(string(updateQue[k]), k) if err != nil { - logger.Log("webutility: %v\n", err) return } upd++ @@ -175,7 +183,6 @@ func UpdateEntityModels(command string) (total, upd, add int, err error) { for _, k := range toAdd { _, err = iStmt.Exec(activeProject, string(blankPayload), k, string(updateQue[k])) if err != nil { - logger.Log("webutility: %v\n", err) return } metadata[k] = Payload{} @@ -196,8 +203,6 @@ func initMetadata(project string) error { } defer rows.Close() - count := 0 - success := 0 if len(metadata) > 0 { metadata = nil } @@ -211,13 +216,9 @@ func initMetadata(project string) error { if err != nil { logger.Log("webutility: couldn't init: '%s' metadata: %s:\n%s\n", name, err.Error(), load) } else { - success++ metadata[name] = p } - count++ } - perc := float32(success) / float32(count) * 100.0 - logger.Log("webutility: loaded %d/%d (%.1f%%) entities\n", success, count, perc) return nil } diff --git a/main.go b/main.go deleted file mode 100644 index 761af13..0000000 --- a/main.go +++ /dev/null @@ -1,22 +0,0 @@ -package webutility - -import ( - "fmt" - - "git.to-net.rs/marko.tikvic/gologger" -) - -var logger *gologger.Logger -var reqLogger *gologger.Logger - -func init() { - var err error - logger, err = gologger.New("metadata", gologger.MaxLogSize100KB) - if err != nil { - fmt.Printf("webutility: %s\n", err.Error()) - } - reqLogger, err = gologger.New("http", gologger.MaxLogSize5MB) - if err != nil { - fmt.Printf("webutility: %s\n", err.Error()) - } -} diff --git a/middleware.go b/middleware.go index 2ea875c..4e3e4a2 100644 --- a/middleware.go +++ b/middleware.go @@ -3,8 +3,12 @@ package webutility import ( "net/http" "time" + + "git.to-net.rs/marko.tikvic/gologger" ) +var reqLogger *gologger.Logger + func WithSetHeaders(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { SetDefaultHeaders(w) @@ -26,6 +30,12 @@ func WithParseForm(h http.HandlerFunc) http.HandlerFunc { } } +func EnableLogging(log string) error { + var err error + reqLogger, err = gologger.New(log, gologger.MaxLogSize5MB) + return err +} + func WithLog(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { reqLogger.LogRequest(req, "") @@ -36,7 +46,7 @@ func WithLog(h http.HandlerFunc) http.HandlerFunc { } } -func WithAuth(h http.HandlerFunc, authorizedRoles []string) http.HandlerFunc { +func WithAuth(authorizedRoles []string, h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { if _, ok := AuthCheck(req, authorizedRoles); !ok { Unauthorized(w, req, "") -- 1.8.1.2