Setting up Route Not Found in Gin

What you’re looking for is the NoRoute handler. More precisely: r := gin.Default() r.NoRoute(func(c *gin.Context) { c.JSON(404, gin.H{“code”: “PAGE_NOT_FOUND”, “message”: “Page not found”}) })

Go gin framework CORS

FWIW, this is my CORS Middleware that works for my needs. func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set(“Access-Control-Allow-Origin”, “*”) c.Writer.Header().Set(“Access-Control-Allow-Credentials”, “true”) c.Writer.Header().Set(“Access-Control-Allow-Headers”, “Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With”) c.Writer.Header().Set(“Access-Control-Allow-Methods”, “POST, OPTIONS, GET, PUT”) if c.Request.Method == “OPTIONS” { c.AbortWithStatus(204) return } c.Next() } }

How to pass arguments to router handlers in Golang using Gin web framework?

I would avoid stuffing ‘application scoped’ dependencies (e.g. a DB connection pool) into a request context. Your two ‘easiest’ options are: Make it a global. This is OK for smaller projects, and *sql.DB is thread-safe. Pass it explicitly in a closure so that the return type satisfies gin.HandlerFunc e.g. // SomeHandler returns a `func(*gin.Context)` to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)