Go
Go HTTP servers generally work with Hatch out of the box.
Map your domain
hatch add api.test 8080 --https
Listener address
Make sure you're listening on all interfaces. Use :8080 (not 127.0.0.1:8080or localhost:8080):
// Standard library
http.ListenAndServe(":8080", handler)
// Gin
r.Run(":8080")
// Echo
e.Start(":8080")
// Fiber
app.Listen(":8080")Forwarded headers
Hatch sets standard proxy headers. If your app needs the client's real IP or protocol:
func handler(w http.ResponseWriter, r *http.Request) {
clientIP := r.Header.Get("X-Forwarded-For")
proto := r.Header.Get("X-Forwarded-Proto")
if clientIP == "" {
clientIP = r.RemoteAddr
}
}CORS
If a frontend on a different Hatch domain calls your Go API:
// Using rs/cors
import "github.com/rs/cors"
c := cors.New(cors.Options{
AllowedOrigins: []string{"https://app.test"},
AllowCredentials: true,
})
handler := c.Handler(mux)Project config
# .hatch.yaml
domains:
- domain: api.test
port: 8080
https: true