being nitpicky but i think these lines are a bit messy:
|
r := gin.Default() |
|
|
|
config := cors.DefaultConfig() |
|
config.AllowOrigins = []string{clientURL} |
|
config.AllowCredentials = true |
|
config.AddAllowHeaders("Authorization") |
|
r.Use(cors.New(config)) |
|
|
|
r.GET("/ping", func(c *gin.Context) { |
|
c.JSON(http.StatusOK, gin.H{ |
|
"message": "response", |
|
}) |
|
}) |
|
|
|
events := r.Group("/events") |
|
{ |
|
events.GET("/", handlers.GetEvents) |
|
events.GET("/:id", handlers.GetEventByID) |
|
events.POST("/", handlers.CreateEvent) |
|
events.DELETE("/:id", handlers.DeleteEventByID) |
|
} |
|
|
|
r.Run(":8002") |
as we add more handlers and stuff it might be a lot of code. and main.go is ideally clean as it's the "main entrypoint" for our server... so let's clean this up. ideally it's just a "setupRouter" function that returns the router for us...
[ ] create a new file in the main package --> router.go
[ ] create a new setupRouter() function that takes in the config create in #32 and sets the router up. this includes creating the gin router, setting up cors, and defining all handlers
[ ] update main.go to import this function; it should be a clean initialization now:
r := setupRouter(cfg)
r.Run(":" + cfg.Port)
being nitpicky but i think these lines are a bit messy:
SCEvents/cmd/server/main.go
Lines 39 to 61 in 279d801
as we add more handlers and stuff it might be a lot of code. and
main.gois ideally clean as it's the "main entrypoint" for our server... so let's clean this up. ideally it's just a "setupRouter" function that returns the router for us...[ ] create a new file in the
mainpackage -->router.go[ ] create a new
setupRouter()function that takes in the config create in #32 and sets the router up. this includes creating the gin router, setting up cors, and defining all handlers[ ] update
main.goto import this function; it should be a clean initialization now: