From ba1577e5ec7327159d8282273581d9c63a038c94 Mon Sep 17 00:00:00 2001 From: ViBiOh Date: Sun, 19 Apr 2020 16:25:22 +0200 Subject: [PATCH] refactor: Harmonizing frontend with hue (#19) --- pkg/model/model.go | 22 ++++++++++++++++++++++ pkg/renderer/handlers.go | 8 +++----- pkg/renderer/model.go | 7 ------- pkg/renderer/renderer.go | 11 +++-------- 4 files changed, 28 insertions(+), 20 deletions(-) delete mode 100644 pkg/renderer/model.go diff --git a/pkg/model/model.go b/pkg/model/model.go index 5b8785a3..9a16e81c 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -4,3 +4,25 @@ package model type RowScanner interface { Scan(...interface{}) error } + +// Message for render +type Message struct { + Level string + Content string +} + +// NewSuccessMessage create a success message +func NewSuccessMessage(content string) Message { + return Message{ + Level: "success", + Content: content, + } +} + +// NewErrorMessage create a error message +func NewErrorMessage(content string) Message { + return Message{ + Level: "error", + Content: content, + } +} diff --git a/pkg/renderer/handlers.go b/pkg/renderer/handlers.go index bd2e00b1..3d2ea2a2 100644 --- a/pkg/renderer/handlers.go +++ b/pkg/renderer/handlers.go @@ -8,6 +8,7 @@ import ( "github.com/ViBiOh/httputils/v3/pkg/httperror" "github.com/ViBiOh/httputils/v3/pkg/logger" "github.com/ViBiOh/httputils/v3/pkg/templates" + "github.com/ViBiOh/ketchup/pkg/model" ) func (a app) getData(r *http.Request) (interface{}, error) { @@ -16,7 +17,7 @@ func (a app) getData(r *http.Request) (interface{}, error) { return targets, err } -func (a app) uiHandler(w http.ResponseWriter, r *http.Request, status int, message Message) { +func (a app) uiHandler(w http.ResponseWriter, r *http.Request, status int, message model.Message) { targets, err := a.getData(r) if err != nil { a.errorHandler(w, http.StatusInternalServerError, err, nil) @@ -45,10 +46,7 @@ func (a app) errorHandler(w http.ResponseWriter, status int, errs ...error) { } if len(errs) > 0 { - content["Message"] = Message{ - Level: "error", - Content: errs[0].Error(), - } + content["Message"] = model.NewErrorMessage(errs[0].Error()) if len(errs) > 1 { content["Errors"] = errs[1:] diff --git a/pkg/renderer/model.go b/pkg/renderer/model.go deleted file mode 100644 index e1719b5d..00000000 --- a/pkg/renderer/model.go +++ /dev/null @@ -1,7 +0,0 @@ -package renderer - -// Message for render -type Message struct { - Level string - Content string -} diff --git a/pkg/renderer/renderer.go b/pkg/renderer/renderer.go index 9f6115cf..4c153773 100644 --- a/pkg/renderer/renderer.go +++ b/pkg/renderer/renderer.go @@ -1,7 +1,6 @@ package renderer import ( - "errors" "fmt" "html/template" "net/http" @@ -10,6 +9,7 @@ import ( "github.com/ViBiOh/httputils/v3/pkg/query" "github.com/ViBiOh/httputils/v3/pkg/templates" + "github.com/ViBiOh/ketchup/pkg/model" "github.com/ViBiOh/ketchup/pkg/target" ) @@ -56,18 +56,13 @@ func (a app) Handler() http.Handler { } if query.IsRoot(r) { - a.uiHandler(w, r, http.StatusOK, Message{ + a.uiHandler(w, r, http.StatusOK, model.Message{ Level: r.URL.Query().Get("messageLevel"), Content: r.URL.Query().Get("messageContent"), }) return } - if strings.HasPrefix(r.URL.Path, targetsPath) { - targetsHandler.ServeHTTP(w, r) - return - } - - a.errorHandler(w, http.StatusNotFound, errors.New("page not found"), nil) + targetsHandler.ServeHTTP(w, r) }) }