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
148 lines (126 loc) · 4.15 KB
/
handler.go
File metadata and controls
148 lines (126 loc) · 4.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
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
package handlers
import (
"fmt"
"net/url"
"slices"
"strings"
"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) responsePhoneAPIKeyUnauthorized(c *fiber.Ctx, owner string, authCtx entities.AuthContext) error {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"status": "error",
"message": "You are not authorized to carry out the request for this phone number",
"data": fmt.Sprintf("The phone API key is does not have permission to carry out actions on the phone number [%s]. The API key is only configured for these phone numbers [%s]", owner, strings.Join(authCtx.PhoneNumbers, ",")),
})
}
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.AuthContext {
if tokenUser, ok := c.Locals(middlewares.ContextKeyAuthUserID).(entities.AuthContext); 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)
}
func (h *handler) mergeErrors(errors ...url.Values) url.Values {
result := url.Values{}
for _, item := range errors {
for key, values := range item {
for _, value := range values {
result.Add(key, value)
}
}
}
return result
}
func (h *handler) authorizePhoneAPIKey(c *fiber.Ctx, phoneNumber string) bool {
user := h.userFromContext(c)
if user.PhoneAPIKeyID == nil {
return true
}
return slices.Contains(user.PhoneNumbers, phoneNumber)
}