This is a boilerplate project for building REST APIs with Go, utilizing the GoFiber framework, PostgreSQL for database management, and SQLBoiler for ORM.
-
/api/v1
/routes
- Contains all API route definitions./controllers
- Handles request validation and delegates to services./services
- Implements business logic, database interactions, and other services./middlewares
- Includes middleware for authentication, logging, rate limiting, etc./types
- Defines custom types used across the application.
-
/build
- Contains the built binary; this directory is ignored by Git. -
/cmd
- Initializes the Fiber application and sets up basic middleware configuration. -
/config
- Manages configuration and environment variables. -
/db
- Manages database connections and related utilities. -
/handlers
- Manages response formatting and database transactions. -
/models
- Auto-generated models from database tables using SQLBoiler. -
/secure
- Stores SSL certificates; this directory is ignored by Git. -
/utils
- Contains utility functions and helpers. -
main.go
- The entry point of the application.
- Enhance your development experience by using the Material Icon Theme in VSCode for a more visually appealing folder structure.
-
The repo contains product API implementation for reference.
-
Clone the repo and rename the folder to your project name.
-
Search for
honestyan/go-fiber-boilerplate
in the project and replace it with<your-github-id/project-name>
. -
Run
go install github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-psql@latest
to install sqlboiler for generating models. -
Change the
models/sqlboiler.toml
file to match your database configuration. -
Under models folder, run
sqlboiler psql
to generate models from database tables, this will create amodels
folder. -
Copy the contents of
models/models
folder tomodels
folder in the root of the project. -
Run
go mod tidy
to install all the dependencies. -
Copy
.env.example
to.env
and change the values as per your configuration. -
Run
go build -o ./build/main && ./build/main
to build and run the app.
-
Run
go get github.com/cosmtrek/air
to install air for hot reloading. -
Run
air
to start the app with hot reloading.
-
Success
Handler for successful requests. -
BuildError
Handler for build errors. -
Start new PGX trx from
controllers
only to ensure proper transaction handling. -
/api/v1
is the base path for all routes except/
for health check. -
/models
consider keeping this in a separate repository and importing it as a Git submodule for better modularity. -
To set up the sample product and user API implementation, create the
products
andusers
table with the following SQL script:
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'gender_enum') THEN
CREATE TYPE gender_enum AS ENUM ('male', 'female');
END IF;
END $$;
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username varchar(50) NOT NULL,
password varchar(255) NOT NULL,
email varchar(50) NOT NULL,
name varchar(50) NOT NULL,
gender gender_enum NOT NULL DEFAULT 'male',
created timestamp NOT NULL,
modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
name varchar(255) NOT NULL,
description text NOT NULL,
price int NOT NULL,
created timestamp NOT NULL,
modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);