Goland CORS, logs and healtcheck

In this guide, we’ll walk through how to create CORS (Cross-Origin Resource Sharing) and a health check endpoint in a Golang application using the Chi router. Setting Up Chi and CORS Middleware We’ll use the go-chi/chi and go-chi/cors packages to handle routing and CORS in our application. Step 1: Import Required Packages First, make sure to import the necessary packages: import ( "fmt" "net/http" "log" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/go-chi/cors" AdminMiddleware "github.com/project/api/middleware" "github.com/project/api/services" "gorm.io/gorm" ) Step 2: Define the CORS AllowOriginFunc We’ll define a function to specify the allowed origins for CORS. This function also logs disallowed origins: ...

2024-06-01