Skip to content

Repository files navigation

Template: go-http-server

Template Go Report Card

This is a GitHub template. Click "Use this template" at the top of the repo (or the badge above) to spin up a fresh service. See Use this template below for the one-command rename.

Minimal Go HTTP server template. See docs at gultekinmakif.github.io/go-http-server.

Standard library net/http with:

  • Go 1.22+ method-prefix routing
  • middleware: request ID, request logger, panic recoverer
  • DB: Postgres + GORM
  • Docker: multi-stage, distroless
  • env: typed config, validated at startup
  • logs: slog — tint in dev, JSON in prod
  • graceful shutdown on SIGINT/SIGTERM

Use this template

  1. Create a new repo from this template (or click "Use this template" in the UI):

    gh repo create my-go-server --template gultekinmakif/go-http-server --private --clone
    cd my-go-server
  2. Rename the module path + DB name + Docker tag:

    ./scripts/init-template.sh github.com/<you>/my-go-server
    go mod tidy
  3. Replace @gultekinmakif in .github/CODEOWNERS with your handle

  4. Import the branch-protection ruleset (one-time, per repo):

    sed -i '' 's/@gultekinmakif/@<your-handle>/' .github/CODEOWNERS
    gh api --method POST -H "Accept: application/vnd.github+json" \
      "repos/<you>/<repo>/rulesets" \
      --input <(jq 'del(.id, .source, .source_type)' .github/rulesets/pr-protection.json)

After that, make docker-up and you're running.

Quick start

docker compose up --build        # app + postgres on :3000
curl localhost:3000/health

Or against your own Postgres:

cp configs/.env.example .env     
make run                         
make dev                         # hot-reload 
make help

Endpoints

Method Path Notes
GET /health Liveness -> {"status":"ok"}
POST /posts Create. Body: {title, body, slug}. Returns 201 + record.
GET /posts List all posts
GET /posts/{slug} Read by slug. 404 on miss.

See docs at gultekinmakif.github.io/go-http-server.

Configuration

Var Default Notes
PORT 3000
ENV dev dev → tint pretty logs, prod → JSON
LOG_LEVEL debug debug / info / warn / error
SHUTDOWN_TIMEOUT 10s Graceful drain timeout
DATABASE_URL (required) Postgres DSN, e.g. postgres://postgres:postgres@localhost:5432/http_server?sslmode=disable

License

see LICENSE.

About

Minimal Go HTTP server template.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Contributors

Languages