-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (99 loc) · 3.16 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
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
package main
import (
//"compress/gzip"
"go-boilerplate/controllers"
"go-boilerplate/db"
"go-boilerplate/forms"
"go-boilerplate/migrations"
"fmt"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/google/uuid"
"github.com/joho/godotenv"
"log"
"os"
)
var auth = new(controllers.AuthController)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
app := gin.Default()
//Custom form validator
binding.Validator = new(forms.DefaultValidator)
app.Use(CORSMiddleware())
app.Use(RequestIDMiddleware())
app.Use(gzip.Gzip(gzip.DefaultCompression))
db.ConnectDb()
migrations.Migrate()
//Start Redis on database 1 - it's used to store the JWT but you can use it for anythig else
//Example: db.GetRedis().Set(KEY, VALUE, at.Sub(now)).Err()
if os.Getenv("REDIS_HOST") != "" {
db.InitRedis(1)
}
setupSentry()
RouteHandler(app)
app.Run() // listen and serve on 0.0.0.0:8080
}
func setupSentry() {
if os.Getenv("SENTRY_DSN") == "" {
return
}
app := gin.Default()
// To initialize Sentry's handler, you need to initialize Sentry itself beforehand
if err := sentry.Init(sentry.ClientOptions{
Dsn: os.Getenv("SENTRY_DSN"),
EnableTracing: true,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
}); err != nil {
fmt.Printf("Sentry initialization failed: %v\n", err)
}
app.Use(sentrygin.New(sentrygin.Options{
Repanic: true,
}))
// sentrygin handler will catch it just fine. Also, because we attached "someRandomTag"
// in the middleware before, it will be sent through as well
//panic("y tho")
}
// CORSMiddleware ...
// CORS (Cross-Origin Resource Sharing)
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, x-access-token")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
fmt.Println("OPTIONS")
c.AbortWithStatus(200)
} else {
c.Next()
}
}
}
// RequestIDMiddleware ...
// Generate a unique ID and attach it to each request for future reference or use
func RequestIDMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
uuidValue := uuid.New()
c.Writer.Header().Set("X-Request-Id", uuidValue.String())
c.Next()
}
}
// TokenAuthMiddleware ...
// JWT Authentication middleware attached to each request that needs to be authenitcated to validate the access_token in the header
func TokenAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
auth.TokenValid(c)
c.Next()
}
}