Gin API Starter for Go

A Gin 1.12 REST API starter for Go 1.25, for developers who want Gin's router and handler style with only the middleware they choose. It includes / and /health routes, slog request logging, gin.Recovery, a GIN_MODE switch, graceful shutdown and table-driven route tests.

by Light CloudREST APIs

Features

  • Router made with gin.New() instead of gin.Default(), so only requestLogger and gin.Recovery are registered
  • GET / returns { "message": "Hello from Gin on Light Cloud" } and GET /health returns { "status": "ok" }
  • A small middleware writes one log/slog JSON line per request in place of Gin's text logger
  • gin.Recovery turns a handler panic into a 500 response instead of a dropped connection
  • GIN_MODE picks release (the default), debug or test; .env.example sets debug for local work
  • The Gin engine runs inside a standard http.Server with a 10 second drain on SIGTERM
  • Table-driven tests send requests to the engine with httptest in Gin's test mode
  • make build writes a static binary to bin/server; listens on PORT, default 8080

Tech stack

What you need

  • Free plan: it runs as a server app, and the Free plan includes 3 server apps
  • To run it locally: Go 1.25 or newer; make runs the run, test and build shortcuts
  • No API keys: it starts with nothing to configure

About

server.New in internal/server returns the Gin engine, and main.go sets the Gin mode and hands that engine to a plain http.Server, which gives you a header timeout and a proper shutdown that Gin's own Run does not. Starting from gin.New() keeps the logging and recovery chain visible in one line. The usual next steps are route groups such as r.Group("/api/v1") and handlers that read c.Param and answer with c.JSON. There is no Dockerfile: on Light Cloud the Go module is built and run as a container on the platform's PORT, and every push to your copy redeploys.

FAQ

How do I add a new route in Gin?

Write a handler func(c *gin.Context) in internal/server/handlers.go and register it in server.New, for example r.GET("/items/:id", getItem). Read the value with c.Param("id") and answer with c.JSON.

How do I group Gin routes under /api/v1?

In server.New, create v1 := r.Group("/api/v1") and register routes on it, such as v1.GET("/items", listItems). Middleware passed to r.Group runs only for that group.

What does GIN_MODE do?

In debug Gin prints every registered route and warnings at start; in release it stays quiet. The app defaults to release when GIN_MODE is unset.

Can I host this Gin app without Light Cloud?

Yes. It is a plain Go module with no Light Cloud code: build it with make build and run ./bin/server with PORT set on any server or container platform.

How do I deploy a Gin API with PostgreSQL?

Use the gin-postgresql-crud-api template, which connects with pgx. On a paid Light Cloud plan, deploying it creates the database and sets the DB_HOST, DB_NAME, DB_USER and DB_PASSWORD variables.

Price
Free
Framework
Gin · Go
Plan
Free
Updated

Related templates

All rest api starters