Skip to main content

Go REST API with Chi Router & Clean Architecture

Build a production Go REST API on chi with clean architecture, middleware, DB migrations, JWT auth, validation, and full test coverage.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt scaffolds a production Go REST API on the chi router with a clean layered structure rather than a single monolithic handler file. You set the [domain] and [architecture], and the AI lays out the project (cmd/, internal/, pkg/), a chi middleware stack covering logging, recovery, CORS, rate limiting, and request ID, and a strict Handler to Service to Repository separation. That layering is what keeps a Go codebase maintainable as features pile up, instead of collapsing into one bloated package that nobody wants to touch.

The variables select the concrete tooling for the stack. [db_driver] and [migration_tool] set the database and schema-migration approach, [auth_features] shapes the JWT middleware with access plus refresh tokens and role-based checks, and [validation_approach], [logger], and [config_lib] fill in request validation, structured logging, and configuration from env vars and a config file. [test_db] drives the integration test strategy, and the prompt insists on unit tests with mocks plus integration tests against a real test database, since mocks alone hide the bugs that actually bite in production.

When to use it

  • Starting a new Go backend that needs to stay maintainable as the team grows
  • Standing up a layered Handler/Service/Repository structure from the very start
  • Wiring a complete chi middleware stack instead of assembling it piecemeal later
  • Adding JWT auth with access and refresh tokens and role-based middleware
  • Setting up database migrations and integration tests against a real test database
  • Generating a Makefile, Dockerfile, and docker-compose for a smooth local setup

Example output

You get a full project layout with cmd/, internal/, and pkg/ directories, a chi router with the configured middleware stack, and cleanly separated handler, service, and repository layers. It includes database integration with migrations, JWT auth middleware, request validation, structured logging, graceful shutdown with context propagation, and a test suite spanning unit, integration, and API levels, plus a Makefile, Dockerfile, and docker-compose.yml for running everything locally.

Pro tips

  • Set [domain] to your real business area so the generated handlers and models are useful rather than placeholder code
  • Keep [db_driver] as pgx for PostgreSQL; it is the performant, idiomatic choice for this clean-architecture layout
  • Use a real test database via [test_db] like testcontainers; mocks alone miss the integration bugs that matter
  • Match [logger] to your platform; slog is a solid stdlib default that avoids pulling in an extra dependency
  • Confirm the [auth_features] cover refresh-token rotation if your app needs long-lived sessions
  • Keep the [validation_approach] at the handler boundary so invalid requests are rejected before they reach your service layer
  • Review the middleware ordering carefully, since rate limiting and request ID placement changes runtime behavior

Frequently Asked Questions

Why use chi instead of a larger framework like Gin?
Chi is a lightweight, idiomatic router built on the standard net/http interfaces, which keeps middleware and handlers composable and easy to test. The prompt leans on that simplicity for the clean Handler/Service/Repository layering, but you can adapt the structure to other routers if needed.
Does it set up real database migrations?
Yes. The `[migration_tool]` variable wires migrations, defaulting to golang-migrate, alongside the `[db_driver]` for database access. This gives you versioned schema changes from the start rather than ad-hoc SQL applied by hand and forgotten later.
Will the integration tests need Docker to run?
With the default `[test_db]` of testcontainers-go, yes; it spins up a real PostgreSQL container during tests, so Docker must be available. This catches integration bugs that mocks miss, though it makes the test suite heavier and slower than pure unit tests.
Can I change the architecture pattern?
Yes. The `[architecture]` variable accepts clean architecture, hexagonal, or other layered patterns. The Handler/Service/Repository separation is the core idea, and the directory layout adjusts to match whichever pattern you specify in the variable.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Rust & Go Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support