/
handler.go
114 lines (94 loc) · 3.31 KB
/
handler.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package httphandler
import (
"context"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/mohamadafzal06/purl/config"
"github.com/mohamadafzal06/purl/param"
)
type PurlService interface {
Short(ctx context.Context, sReq param.ShortRequest) (param.ShortResponse, error)
GetLong(ctx context.Context, surl param.LongRequest) (param.LongResponse, error)
GetLongInfo(ctx context.Context, surl param.LongInfoRequest) (param.LongInfoResponse, error)
}
type Handler struct {
// TODO: the config of webserver should be move to another directory
schema string
host string
service PurlService
}
func NewServer(srv PurlService) Handler {
return Handler{
schema: config.ServerSchema,
host: config.ServerHost,
service: srv,
}
}
func (h Handler) Short(c echo.Context) error {
if c.Request().Method != http.MethodPost {
return echo.NewHTTPError(http.StatusBadRequest, echo.Map{"message": "Only except POST method"})
}
reqPram := new(param.ShortRequest)
err := c.Bind(reqPram)
if err != nil {
log.Errorf("cannot bind the Body request into request param: %v\n", err)
return c.JSON(http.StatusBadRequest, echo.Map{"message": "the requsted url for shortning has problem"})
}
err = reqPram.ValidateShort()
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "the requsted url for shortning has problem"})
}
sResp, err := h.service.Short(c.Request().Context(), *reqPram)
if err != nil {
// TODO: check other possible error
return c.JSON(http.StatusInternalServerError, echo.Map{"message": "cannot short the url"})
}
// TODO: check better response format
return c.JSON(http.StatusOK, echo.Map{"message": sResp})
}
func (h Handler) Redirect(c echo.Context) error {
if c.Request().Method != http.MethodGet {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "Only except GET method"})
}
key := c.Param("key")
reqPram := param.LongRequest{
Key: key,
}
err := reqPram.ValidateLong()
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "the requsted key for getting original url has problem"})
}
sResp, err := h.service.GetLong(c.Request().Context(), reqPram)
if err != nil {
// TODO: check other possible error
return c.JSON(http.StatusInternalServerError, echo.Map{"message": "cannot get the original url"})
}
// TODO: check better response format
return c.Redirect(http.StatusPermanentRedirect, sResp.LongURL)
}
func (h Handler) LongInfo(c echo.Context) error {
if c.Request().Method != http.MethodGet {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "Only except GET method"})
}
key := c.Param("key")
reqPram := param.LongInfoRequest{
Key: key,
}
err := reqPram.ValidateLongInfo()
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "the requsted key for getting info of original url has problem"})
}
response, err := h.service.GetLongInfo(c.Request().Context(), reqPram)
if err != nil {
// TODO: check other possible error
return c.JSON(http.StatusInternalServerError, echo.Map{"message": "cannot get infor of the original url"})
}
// TODO: check better response format
return c.JSON(http.StatusOK, echo.Map{"url info": response})
}
func (h Handler) Register(g *echo.Group) {
g.POST("/short", h.Short)
g.GET("/long/:key", h.Redirect)
g.GET("/long/:key/info", h.LongInfo)
}