-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (45 loc) · 1.18 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 (
"log"
"net/http"
"BACKEND-GO/database"
"BACKEND-GO/initiliazers"
"BACKEND-GO/routes"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func init() {
initiliazers.LoadEnvVariable()
}
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
}
database.InitDatabase()
defer database.CloseDatabase()
router := gin.Default()
// router.Use(cors.Default())
router.Use(setupCORS())
store := cookie.NewStore([]byte("secret"))
router.Use(sessions.Sessions("session", store))
routes.UserRoute(router)
routes.ComplaintRoutes(router)
routes.StudentRoutes(router)
server := &http.Server{
Addr: ":2426",
Handler: router,
}
log.Println("Server is running at :2426...")
server.ListenAndServe()
}
func setupCORS() gin.HandlerFunc {
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://localhost:5173"}
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE"}
config.AllowHeaders = append(config.AllowHeaders, "Authorization")
config.AllowCredentials = true
return cors.New(config)
}