Skip to content

Cleanup router setup #33

@steeevin88

Description

@steeevin88

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)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions