diff --git a/middleware.go b/middleware.go deleted file mode 100644 index 2a3aea1..0000000 --- a/middleware.go +++ /dev/null @@ -1,81 +0,0 @@ -package webutility - -import ( - "net/http" - "time" - - "git.to-net.rs/marko.tikvic/gologger" -) - -var httpLogger *gologger.Logger - -// SetHeaders ... -func SetHeaders(h http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, req *http.Request) { - SetDefaultHeaders(w) - if req.Method == http.MethodOptions { - return - } - h(w, req) - } -} - -// ParseForm ... -func ParseForm(h http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, req *http.Request) { - err := req.ParseForm() - if err != nil { - BadRequest(w, req, err.Error()) - return - } - h(w, req) - } -} - -// ParseMultipartForm ... -func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, req *http.Request) { - err := req.ParseMultipartForm(32 << 20) - if err != nil { - BadRequest(w, req, err.Error()) - return - } - h(w, req) - } -} - -// EnableLogging ... -func EnableLogging(log string) (err error) { - httpLogger, err = gologger.New(log, gologger.MaxLogSize5MB) - return err -} - -// Log ... -func Log(h http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, req *http.Request) { - t1 := time.Now() - - claims, _ := GetTokenClaims(req) - in := httpLogger.LogHTTPRequest(req, claims.Username) - - rec := NewStatusRecorder(w) - - h(rec, req) - - t2 := time.Now() - out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size()) - - httpLogger.CombineHTTPLogs(in, out) - } -} - -// Auth ... -func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, req *http.Request) { - if _, err := AuthCheck(req, roles); err != nil { - Unauthorized(w, req, err.Error()) - return - } - h(w, req) - } -} diff --git a/middleware/main.go b/middleware/main.go index e86f039..55e8654 100644 --- a/middleware/main.go +++ b/middleware/main.go @@ -2,22 +2,20 @@ package middleware import ( "net/http" - - web "git.to-net.rs/marko.tikvic/webutility" ) func Headers(h http.HandlerFunc) http.HandlerFunc { - return web.SetHeaders(web.ParseForm(h)) + return IgnoreOptionsRequests(ParseForm(h)) } func AuthOnly(roles string, h http.HandlerFunc) http.HandlerFunc { - return web.SetHeaders(web.ParseForm(web.Auth(roles, h))) + return IgnoreOptionsRequests(ParseForm(Auth(roles, h))) } func Full(roles string, h http.HandlerFunc) http.HandlerFunc { - return web.SetHeaders(web.ParseForm(web.Log(web.Auth(roles, h)))) + return IgnoreOptionsRequests(ParseForm(LogTraffic(Auth(roles, h)))) } -func Log(h http.HandlerFunc) http.HandlerFunc { - return web.SetHeaders(web.ParseForm(web.Log(h))) +func LogTraffic(h http.HandlerFunc) http.HandlerFunc { + return IgnoreOptionsRequests(ParseForm(LogRequestAndResponse(h))) } diff --git a/middleware/middleware.go b/middleware/middleware.go new file mode 100644 index 0000000..b106281 --- /dev/null +++ b/middleware/middleware.go @@ -0,0 +1,86 @@ +package middleware + +import ( + "net/http" + "time" + + "git.to-net.rs/marko.tikvic/gologger" + + web "git.to-net.rs/marko.tikvic/webutility" +) + +var httpLogger *gologger.Logger + +// IgnoreOptionsRequests ... +func IgnoreOptionsRequests(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + web.SetDefaultHeaders(w) + if req.Method == http.MethodOptions { + return + } + h(w, req) + } +} + +// ParseForm ... +func ParseForm(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + err := req.ParseForm() + if err != nil { + web.BadRequest(w, req, err.Error()) + return + } + h(w, req) + } +} + +// ParseMultipartForm ... +func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + err := req.ParseMultipartForm(32 << 20) + if err != nil { + web.BadRequest(w, req, err.Error()) + return + } + h(w, req) + } +} + +// SetLogger ... +func SetLogger(logger *gologger.Logger) { + httpLogger = logger +} + +// LogRequestAndResponse ... +func LogRequestAndResponse(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + if httpLogger != nil { + t1 := time.Now() + + claims, _ := web.GetTokenClaims(req) + in := httpLogger.LogHTTPRequest(req, claims.Username) + + rec := web.NewStatusRecorder(w) + + h(rec, req) + + t2 := time.Now() + out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size()) + + httpLogger.CombineHTTPLogs(in, out) + } else { + h(w, req) + } + } +} + +// Auth ... +func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + if _, err := web.AuthCheck(req, roles); err != nil { + web.Unauthorized(w, req, err.Error()) + return + } + h(w, req) + } +} diff --git a/payload.go b/payload.go index da875a3..4699524 100644 --- a/payload.go +++ b/payload.go @@ -10,8 +10,6 @@ import ( "strings" "sync" "time" - - "git.to-net.rs/marko.tikvic/gologger" ) var ( @@ -25,7 +23,6 @@ var ( inited bool metaDriver string - logger *gologger.Logger ) // LangMap ... @@ -143,11 +140,6 @@ func InitPayloadsMetadata(drv string, db *sql.DB, project string) error { 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) @@ -211,20 +203,17 @@ func UpdateEntityModels(command string) (total, upd, add int, err error) { if metaDriver == "ora" { uStmt, err = metadataDB.Prepare("update entities set entity_model = :1 where entity_type = :2") if err != nil { - logger.Trace(err.Error()) return } } else if metaDriver == "mysql" { uStmt, err = metadataDB.Prepare("update entities set entity_model = ? where entity_type = ?") if err != nil { - logger.Trace(err.Error()) return } } for _, k := range toUpdate { _, err = uStmt.Exec(string(updateQue[k]), k) if err != nil { - logger.Trace(err.Error()) return } upd++ @@ -235,20 +224,17 @@ func UpdateEntityModels(command string) (total, upd, add int, err error) { if metaDriver == "ora" { iStmt, err = metadataDB.Prepare("insert into entities(projekat, metadata, entity_type, entity_model) values(:1, :2, :3, :4)") if err != nil { - logger.Trace(err.Error()) return } } else if metaDriver == "mysql" { iStmt, err = metadataDB.Prepare("insert into entities(projekat, metadata, entity_type, entity_model) values(?, ?, ?, ?)") if err != nil { - logger.Trace(err.Error()) return } } for _, k := range toAdd { _, err = iStmt.Exec(activeProject, string(blankPayload), k, string(updateQue[k])) if err != nil { - logger.Trace(err.Error()) return } metadata[k] = Payload{} @@ -280,7 +266,7 @@ func initMetadata(project string) error { p := Payload{} err := json.Unmarshal([]byte(load), &p) if err != nil { - logger.Log("webutility: couldn't init: '%s' metadata: %s:\n%s\n", name, err.Error(), load) + fmt.Printf("webutility: couldn't init: '%s' metadata: %s:\n%s\n", name, err.Error(), load) } else { metadata[name] = p } @@ -353,7 +339,7 @@ func hotload(n int) { entity_type from entities where projekat = ` + fmt.Sprintf("'%s'", activeProject)) if err != nil { - logger.Log("webutility: hotload failed: %v\n", err) + fmt.Printf("webutility: hotload failed: %v\n", err) time.Sleep(time.Duration(n) * time.Second) continue } @@ -372,7 +358,7 @@ func hotload(n int) { rows.Close() if rows.Err() != nil { - logger.Log("webutility: hotload rset error: %v\n", rows.Err()) + fmt.Printf("webutility: hotload rset error: %v\n", rows.Err()) time.Sleep(time.Duration(n) * time.Second) continue } @@ -398,7 +384,7 @@ func refreshMetadata(entities []string) { ` and entity_type = ` + fmt.Sprintf("'%s'", e)) if err != nil { - logger.Log("webutility: refresh: prep: %v\n", err) + fmt.Printf("webutility: refresh: prep: %v\n", err) rows.Close() continue } @@ -409,7 +395,7 @@ func refreshMetadata(entities []string) { p := Payload{} err := json.Unmarshal([]byte(load), &p) if err != nil { - logger.Log("webutility: couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), load) + fmt.Printf("webutility: couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), load) } else { metadata[e] = p }