-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
58 lines (44 loc) · 1.21 KB
/
Copy pathmain.go
File metadata and controls
58 lines (44 loc) · 1.21 KB
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
package main
import (
"fmt"
"github.com/ayush00git/cms-web/config"
"github.com/ayush00git/cms-web/handlers"
"github.com/ayush00git/cms-web/helpers"
"github.com/ayush00git/cms-web/routes"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
// Load .env file if it exists, ignore error if missing (e.g. in docker containers)
_ = godotenv.Load()
// db connection
config.ConnectDB()
r := gin.Default()
// CORS policy and config
corsConfig := cors.DefaultConfig()
frontendURL := helpers.GetEnvWithDefault("FRONTEND_URL", "http://localhost:5173")
corsConfig.AllowOrigins = []string{frontendURL}
corsConfig.AllowCredentials = true
r.Use(cors.New(corsConfig))
// health check route
r.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "server running smooth"})
})
// define handlers
authHandler := &handlers.AuthHandler{
DB: config.DB,
}
postHandler := &handlers.PostHandler{
DB: config.DB,
}
adminHandler := &handlers.AdminHandler{
DB: config.DB,
}
// register routes
routes.AuthRoute(r, authHandler)
routes.PostRoute(r, postHandler)
routes.AdminRoutes(r, adminHandler)
r.Run(":8080")
fmt.Println("Sevrer running on port 8080")
}