forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
117 lines (98 loc) · 3.15 KB
/
Copy pathhandler.go
File metadata and controls
117 lines (98 loc) · 3.15 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
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
package handlers
import (
"net/url"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/middlewares"
"github.com/gofiber/fiber/v2"
)
// handler is the base struct for handling requests
type handler struct{}
func (h *handler) responseBadRequest(c *fiber.Ctx, err error) error {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"status": "error",
"message": "The request isn't properly formed",
"data": err,
})
}
func (h *handler) responseInternalServerError(c *fiber.Ctx) error {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"status": "error",
"message": "We ran into an internal error while handling the request.",
})
}
func (h *handler) responseUnauthorized(c *fiber.Ctx) error {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"status": "error",
"message": "You are not authorized to carry out this request.",
"data": "Make sure your API key is set in the [X-API-Key] header in the request",
})
}
func (h *handler) responseForbidden(c *fiber.Ctx) error {
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{
"status": "error",
"message": fiber.ErrForbidden.Message,
})
}
func (h *handler) responseUnprocessableEntity(c *fiber.Ctx, errors url.Values, message string) error {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"status": "error",
"message": message,
"data": errors,
})
}
func (h *handler) responseNotFound(c *fiber.Ctx, message string) error {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"status": "error",
"message": message,
})
}
func (h *handler) responsePaymentRequired(c *fiber.Ctx, message string) error {
return c.Status(fiber.StatusPaymentRequired).JSON(fiber.Map{
"status": "error",
"message": message,
})
}
func (h *handler) responseNoContent(c *fiber.Ctx, message string) error {
return c.Status(fiber.StatusNoContent).JSON(fiber.Map{
"status": "success",
"message": message,
})
}
func (h *handler) responseAccepted(c *fiber.Ctx, message string) error {
return c.Status(fiber.StatusAccepted).JSON(fiber.Map{
"status": "success",
"message": message,
})
}
func (h *handler) responseOK(c *fiber.Ctx, message string, data interface{}) error {
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"status": "success",
"message": message,
"data": data,
})
}
func (h *handler) responseCreated(c *fiber.Ctx, message string, data interface{}) error {
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
"status": "success",
"message": message,
"data": data,
})
}
func (h *handler) pluralize(value string, count int) string {
if count == 1 {
return value
}
return value + "s"
}
func (h *handler) userFromContext(c *fiber.Ctx) entities.AuthUser {
if tokenUser, ok := c.Locals(middlewares.ContextKeyAuthUserID).(entities.AuthUser); ok && !tokenUser.IsNoop() {
return tokenUser
}
panic("user does not exist in context.")
}
func (h *handler) userIDFomContext(c *fiber.Ctx) entities.UserID {
return h.userFromContext(c).ID
}
func (h *handler) computeRoute(middlewares []fiber.Handler, route fiber.Handler) []fiber.Handler {
return append(append([]fiber.Handler{}, middlewares...), route)
}