-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
111 lines (90 loc) · 2.26 KB
/
config.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
package cipherPayload
import (
"github.com/gofiber/fiber/v2"
)
type Config struct {
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Required. Default: KeyPairs{}
KeyPairs KeyPairs
// Optional. Default: OPTIONS, POST, PUT, DELETE
AllowMethod []string
// Optional. Default: false
DebugMode bool
// Optional. [Default: false]
StrictMode bool
// Optional. Default: true
ExcludeHealthAPI bool
// Optional. Default: BadRequestResponse
FailResponse func(c *fiber.Ctx, msg string) error
// Optional. Default: InternalServerErrorResponse
ErrorResponse func(c *fiber.Ctx, msg string) error
}
var ConfigDefault = Config{
Next: nil,
KeyPairs: KeyPairs{},
AllowMethod: []string{
fiber.MethodOptions,
fiber.MethodPost,
fiber.MethodPut,
fiber.MethodDelete,
},
DebugMode: false,
StrictMode: false,
ExcludeHealthAPI: true,
FailResponse: BadRequestResponse,
ErrorResponse: InternalServerErrorResponse,
}
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// set default values
if cfg.AllowMethod == nil {
cfg.AllowMethod = ConfigDefault.AllowMethod
}
// Note: cfg.DebugMode: it's false by default.
// Note: cfg.StrictMode: it's false by default.
// NOTE: cfg.ExcludeHealthAPI: it's true by default
// set default values
if cfg.FailResponse == nil {
cfg.FailResponse = ConfigDefault.FailResponse
}
// set default values
if cfg.ErrorResponse == nil {
cfg.ErrorResponse = ConfigDefault.ErrorResponse
}
return cfg
}
type PayloadBody struct {
Payload string `json:"payload"`
}
type KeyPairs struct {
AESKeyForEncrypt []byte
AESIVForEncrypt []byte
AESKeyForDecrypt []byte
AESIVForDecrypt []byte
}
func BadRequestResponse(c *fiber.Ctx, msg string) error { // 400
if msg == "" {
msg = "Bad Request"
}
res := fiber.Map{
"status": "bad_request",
"message": msg,
}
return c.Status(fiber.StatusBadRequest).JSON(res)
}
func InternalServerErrorResponse(c *fiber.Ctx, msg string) error { // 500
if msg == "" {
msg = "Internal Server Error"
}
res := fiber.Map{
"status": "internal_server_error",
"message": msg,
}
return c.Status(fiber.StatusInternalServerError).JSON(res)
}