-
Notifications
You must be signed in to change notification settings - Fork 0
/
shorten.go
177 lines (145 loc) · 5.65 KB
/
shorten.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package webhttp
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/pavelmemory/jobtome/internal/logging"
"github.com/pavelmemory/jobtome/internal/shorten"
)
//go:generate mockgen -source=shorten.go -destination mock.go -package webhttp ShortenService
// ShortenService provides set of operations available to operate on the user entity.
type ShortenService interface {
// Create creates a new shorten and returns its unique identifier.
Create(ctx context.Context, entity shorten.Entity) (int64, error)
// Get returns a single shorten by its unique identifier.
Get(ctx context.Context, id int64) (shorten.Entity, error)
// List returns subset of all shortens.
List(ctx context.Context, pager shorten.Pager) ([]shorten.Entity, error)
// Delete removes shorten by its unique identifier.
Delete(ctx context.Context, id int64) error
// Resolve returns a full URL accessioned with the hash.
Resolve(ctx context.Context, hash string) (string, error)
}
// NewShortenHandler returns HTTP baseHandler initialized with provided service abstraction.
func NewShortenHandler(shortenService ShortenService) ShortenHandler {
return ShortenHandler{shortenService: shortenService}
}
// ShortenHandler handles request for the user entity(-ies).
type ShortenHandler struct {
baseHandler
shortenService ShortenService
mapper Mapper
}
// Register creates a binding between method handlers and endpoints.
func (uh ShortenHandler) Register(router chi.Router) {
router = router.With(LogRequest())
router.With(ProducesJSON, AcceptsJSON).Method(http.MethodPost, uh.urlPrefix(), http.HandlerFunc(uh.Create))
router.With(ProducesJSON).Method(http.MethodGet, uh.urlPrefix(), http.HandlerFunc(uh.List))
router.With(ProducesJSON).Method(http.MethodGet, uh.urlPrefix()+"/{id}", http.HandlerFunc(uh.Get))
router.Method(http.MethodDelete, uh.urlPrefix()+"/{id}", http.HandlerFunc(uh.Delete))
}
func (uh ShortenHandler) Create(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := uh.logger(ctx, "Create")
logger.Debug("start")
defer logger.Debug("end")
var req CreateShortenReq
if err := Decode(r.Body, &req); err != nil {
logger.WithError(err).Error("decode payload")
ErrorResponse{Cause: err, StatusCode: http.StatusBadRequest}.Write(logger, w)
return
}
id, err := uh.shortenService.Create(ctx, uh.mapper.createShortenReq2Entity(req))
if err != nil {
logger.WithError(err).Error("creation of the shorten")
WriteError(w, logger, err)
return
}
w.Header().Set("location", uh.urlPrefix()+"/"+strconv.FormatInt(id, 10))
w.WriteHeader(http.StatusCreated)
}
func (uh ShortenHandler) Get(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := uh.logger(ctx, "Get")
logger.Debug("start")
defer logger.Debug("end")
id, err := uh.pathParamInt64(r, ParamInt64Opts{P: ParamOpts{Name: "id"}})
if err != nil {
cause := fmt.Errorf(`parameter "id": %w`, err)
logger.WithError(cause).Error("extract path parameter")
ErrorResponse{Cause: cause, StatusCode: http.StatusBadRequest}.Write(logger, w)
return
}
entity, err := uh.shortenService.Get(ctx, id)
if err != nil {
logger.WithError(err).WithInt64("id", id).Error(`get shorten by "id"`)
WriteError(w, logger, err)
return
}
if err := Encode(w, uh.mapper.entity2GetShortenResp(entity)); err != nil {
logger.WithError(err).Error("encode entity")
ErrorResponse{Cause: err, StatusCode: http.StatusInternalServerError}.Write(logger, w)
return
}
}
const (
defaultListLimit = int64(50)
)
func (uh ShortenHandler) List(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := uh.logger(ctx, "List")
logger.Debug("start")
defer logger.Debug("end")
limit, err := uh.queryParamInt64(r, ParamInt64Opts{P: ParamOpts{Name: "limit", Optional: true}, Default: defaultListLimit})
if err != nil {
cause := fmt.Errorf(`parameter "limit": %w`, err)
logger.WithError(cause).Error("extract query parameter")
ErrorResponse{Cause: cause, StatusCode: http.StatusBadRequest}.Write(logger, w)
return
}
offset, err := uh.queryParamInt64(r, ParamInt64Opts{P: ParamOpts{Name: "offset", Optional: true}})
if err != nil {
cause := fmt.Errorf(`parameter "offset": %w`, err)
logger.WithError(cause).Error("extract query parameter")
ErrorResponse{Cause: cause, StatusCode: http.StatusBadRequest}.Write(logger, w)
return
}
entities, err := uh.shortenService.List(ctx, shorten.Pager{Limit: limit, Offset: offset})
if err != nil {
logger.WithError(err).Error("extract shortens")
WriteError(w, logger, err)
return
}
if err := Encode(w, uh.mapper.entities2ListShortenResp(entities)); err != nil {
logger.WithError(err).Error("encode shortens")
ErrorResponse{Cause: err, StatusCode: http.StatusInternalServerError}.Write(logger, w)
return
}
}
func (uh ShortenHandler) Delete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := uh.logger(ctx, "Delete")
logger.Debug("start")
defer logger.Debug("end")
id, err := uh.pathParamInt64(r, ParamInt64Opts{P: ParamOpts{Name: "id"}})
if err != nil {
cause := fmt.Errorf(`parameter "id": %w`, err)
logger.WithError(cause).Error("extract path parameter")
ErrorResponse{Cause: cause, StatusCode: http.StatusBadRequest}.Write(logger, w)
return
}
if err := uh.shortenService.Delete(ctx, id); err != nil {
logger.WithError(err).WithInt64("id", id).Error("delete shorten")
WriteError(w, logger, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (uh ShortenHandler) urlPrefix() string {
return "/api/shorten"
}
func (uh ShortenHandler) logger(ctx context.Context, method string) logging.Logger {
return logging.FromContext(ctx).WithString("component", "ShortenHandler").WithString("method", method)
}