Skip to content

Commit

Permalink
refactor: Harmonizing frontend with hue (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ViBiOh authored Apr 19, 2020
1 parent 7c136cb commit ba1577e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
22 changes: 22 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
8 changes: 3 additions & 5 deletions pkg/renderer/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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:]
Expand Down
7 changes: 0 additions & 7 deletions pkg/renderer/model.go

This file was deleted.

11 changes: 3 additions & 8 deletions pkg/renderer/renderer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package renderer

import (
"errors"
"fmt"
"html/template"
"net/http"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
})
}

0 comments on commit ba1577e

Please sign in to comment.