This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
142 lines (122 loc) · 3.24 KB
/
utils.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
package utils
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
uuid2 "github.com/google/uuid"
"github.com/nyugoh/sagittarius/app/models"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
)
func SendError(c *gin.Context, msg string) {
LogError(msg)
c.JSON(http.StatusBadRequest, gin.H{
"error": msg,
})
}
func SendJson(c *gin.Context, payload gin.H) {
c.JSON(http.StatusOK, payload)
}
func RespondWithError(w http.ResponseWriter, code int, msg string) {
RespondWithJSON(w, code, map[string]interface{}{"error": msg})
}
func RespondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
Log(fmt.Sprintf("RESPONSE:: Status:%d Payload: %v", code, payload))
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
func CurrentTime() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func ValidateEmail(email string) (bool, error) {
var rxEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
if len(email) > 254 || !rxEmail.MatchString(email) {
return false, errors.New("email is invalid")
}
return true, nil
}
func VerifyToken(tokenString string) (*jwt.Token, error) {
token, err := jwt.ParseWithClaims(tokenString, &models.CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(os.Getenv("SESSION_KEY")), nil
})
if err != nil {
return nil, err
}
return token, nil
}
func AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
Log("Hit an auth required endpoint...")
auth := c.Request.Header.Get("Authorization")
if len(auth)== 0 {
SendError(c, "Authorization token is required.")
return
}
token, err := VerifyToken(auth)
if err != nil {
SendError(c, err.Error())
return
}
claims := token.Claims.(*models.CustomClaims)
Log(claims.AppName)
Log("Request made by app name:", claims.AppName)
c.Set("app_name", claims.AppName)
c.Next()
}
}
func MetricsMonitor() gin.HandlerFunc {
return func(c *gin.Context) {
t := time.Now()
// before request
c.Next()
// after request
latency := time.Since(t)
// access the status we are sending
status := c.Writer.Status()
if !strings.Contains(c.Request.URL.Path, "socket.io"){ // Avoid logging for /socket.io
log.Println("Request: ", c.Request.URL.Path, "took:", latency, "Status:", status)
}
}
}
func ExtractAppName(c *gin.Context) (appName string) {
if name, ok := c.Get("app_name"); ok {
appName = fmt.Sprintf("%v", name)
} else {
appName = "Unknown"
}
return
}
func SendGet(url string) (result map[string]interface{}, err error) {
client := http.Client{}
request, err := http.NewRequest(http.MethodGet, url, bytes.NewBuffer([]byte("")))
res, err := client.Do(request)
if err != nil {
return nil, err
}
json.NewDecoder(res.Body).Decode(&result)
Log(result)
if res.StatusCode == 200 {
return result, nil
} else {
errMsg := fmt.Sprintf("%s", result["error"])
LogError(errMsg)
return nil, errors.New(errMsg)
}
}
func ExitApp(code int) {
Log("Exiting app...")
os.Exit(code)
}
func GenerateUUID() string {
uuid := uuid2.New()
return uuid.String()
}