-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (45 loc) · 1.02 KB
/
main.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
package main
import (
"fmt"
"gin/basic/db"
"gin/basic/router"
"gin/basic/utils"
"log"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
)
func AuthMiddleware() gin.HandlerFunc {
checksum := func(c *gin.Context) {
log.Println(c.Request.URL)
if c.Request.URL.String() == "/login" || c.Request.URL.String() == "/signup" || c.Request.URL.String() == "/" || strings.HasPrefix(c.Request.URL.Path, "/swagger") {
c.Next()
return
}
token := c.GetHeader("token")
isValid, err := utils.ValidateToken(token)
if err != nil && !isValid {
c.AbortWithStatusJSON(http.StatusUnauthorized, "Try again with new token")
}
c.Next()
}
return checksum
}
func main() {
r := gin.Default()
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
r.Use(AuthMiddleware())
_, err := db.GetPostgresDBConnection()
if err != nil {
log.Fatal("Unable to establish DB connection", err)
} else {
log.Println("DB connection establsihed")
}
router.PostRouter(r)
router.UserRouter(r)
r.Run(fmt.Sprintf(":%s", port))
}