-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (42 loc) · 1.74 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
package main
import (
"fmt"
uRepo "github.com/uninus-opensource/uninus-go-gin-boilerplate/feature/user/repository"
uSvc "github.com/uninus-opensource/uninus-go-gin-boilerplate/feature/user/service"
"github.com/uninus-opensource/uninus-go-gin-boilerplate/helper/hashing"
jwt2 "github.com/uninus-opensource/uninus-go-gin-boilerplate/helper/jwt"
"github.com/uninus-opensource/uninus-go-gin-boilerplate/routes"
"github.com/gin-gonic/gin"
"github.com/uninus-opensource/uninus-go-gin-boilerplate/config"
aHandler "github.com/uninus-opensource/uninus-go-gin-boilerplate/feature/auth/handler"
aSvc "github.com/uninus-opensource/uninus-go-gin-boilerplate/feature/auth/service"
"github.com/uninus-opensource/uninus-go-gin-boilerplate/helper/database"
)
func main() {
app := gin.Default()
var loadConfig = config.RunConfig()
db := database.NewConnectionDB(*loadConfig)
database.Migrate(db)
jwt := jwt2.NewJWT(loadConfig.Secret)
hash := hashing.NewHash()
userRepo := uRepo.NewUserRepository(db)
userSvc := uSvc.NewUserService(userRepo)
authSvc := aSvc.NewAuthService(userRepo, userSvc, hash, jwt)
authHand := aHandler.NewAuthHandler(authSvc)
app.Use(corsMiddleware())
routes.AuthRoute(app, authHand)
addr := fmt.Sprintf(":%d", loadConfig.AppPort)
app.Run(addr)
}
func corsMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Content-Length, Accept-Language, Accept-Encoding, Connection, Authorization")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE, PATCH, OPTIONS")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(200)
return
}
c.Next()
}
}