-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (63 loc) · 2.21 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
package main
import (
"backend/config"
"backend/controllers"
"backend/models"
"log"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
)
func main() {
config.Init()
log.Println("Starting up...")
// Initialize Validators
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("isTimestamp", controllers.IsTimestamp)
v.RegisterValidation("doesSchoolExist", controllers.DoesSchoolExist)
log.Println("Registered validators...")
}
// Initialize DB
models.GetDB()
log.Println("Initialized Database...")
// Dummy data
if models.IsDBEmpty() {
models.InsertDummyData(5)
log.Println("Dummy data inserted...")
}
// Routes
r := gin.Default()
// CORS
conf := cors.DefaultConfig()
conf.AllowCredentials = true
conf.AllowAllOrigins = true
conf.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
conf.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"}
r.Use(cors.New(conf))
schoolRouter := r.Group("/schools")
schoolRouter.GET("/", controllers.GetAllSchools)
authRouter := r.Group("/auth")
authRouter.POST("/signup", controllers.Singup)
authRouter.POST("/login", controllers.Login)
profileRouter := r.Group("/profile")
profileRouter.Use(controllers.JWTAuthenticator())
profileRouter.GET("/", controllers.GetProfile)
profileRouter.PUT("/", controllers.UpdateProfile)
courseGroupRouter := r.Group("/schools")
courseGroupRouter.Use(controllers.JWTAuthenticator())
courseGroupRouter.GET("/course-groups", controllers.GetAllSchoolCourses)
planRouter := r.Group("/plans")
planRouter.Use(controllers.JWTAuthenticator())
planRouter.GET("/", controllers.GetAllPlans)
planRouter.POST("/", controllers.CreatePlan)
planRouter.DELETE("/:plan_id", controllers.DeletePlan)
planRouter.GET("/:plan_id", controllers.GetPlan)
planRouter.POST("/:plan_id/:course_id", controllers.AddCourseToPlan)
planRouter.DELETE("/:plan_id/:course_id", controllers.DeleteCourseFromPlan)
planRouter.DELETE("/:plan_id/all", controllers.ClearPlan)
err := r.Run()
if err != nil {
panic(err)
} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}