-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
84 lines (69 loc) · 1.91 KB
/
handler.go
File metadata and controls
84 lines (69 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"net/http"
"strings"
"github.com/labstack/echo/v4"
)
var (
// FallbackQuery is used if no other query was specified
FallbackQuery = "MP"
)
// RequestParams contains all request parameters
type RequestParams struct {
Query string `param:"query" validate:"required,alphaunicode,min=1,max=20"`
}
// QueryResult contains the query as well as the result value of a single request
type QueryResult struct {
Query string `json:"query"`
Result string `json:"result"`
}
// String implements fmt.Stringer used for plain text responses
func (r QueryResult) String() string {
return r.Result
}
// Error holds useful information in case of an error
type Error struct {
Message string `json:"message"`
Cause string `json:"cause"`
}
// Handler is responsible for handling requests using handler functions.
type Handler struct {
Generator Generator
DefaultQuery string
}
func (h *Handler) rootHandler(c echo.Context) error {
query := h.DefaultQuery
if strings.TrimSpace(query) == "" {
query = FallbackQuery
}
result, err := h.Generator.Generate(strings.ToUpper(query))
if err != nil {
return err
}
return Negotiate(http.StatusOK, "index.tmpl", &QueryResult{Query: query, Result: result}, c)
}
func (h *Handler) queryHandler(c echo.Context) error {
req := new(RequestParams)
if err := c.Bind(req); err != nil {
return err
}
if err := c.Validate(req); err != nil {
return err
}
query := strings.ToUpper(req.Query)
result, err := h.Generator.Generate(query)
if err != nil {
return &echo.HTTPError{
Code: http.StatusNotFound,
Message: fmt.Sprintf("Could not generate word for query '%v'.", query),
Internal: err,
}
}
return Negotiate(http.StatusOK, "index.tmpl", &QueryResult{Query: query, Result: result}, c)
}
func redirectHandler(target string) echo.HandlerFunc {
return func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, target)
}
}