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-boilerplatein the project and replace it with<your-github-id/project-name>. -
Run
go install github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-psql@latestto install sqlboiler for generating models. -
Change the
models/sqlboiler.tomlfile to match your database configuration. -
Under models folder, run
sqlboiler psqlto generate models from database tables, this will create amodelsfolder. -
Copy the contents of
models/modelsfolder tomodelsfolder in the root of the project. -
Run
go mod tidyto install all the dependencies. -
Copy
.env.exampleto.envand change the values as per your configuration. -
Run
go build -o ./build/main && ./build/mainto build and run the app.
-
Run
go get github.com/cosmtrek/airto install air for hot reloading. -
Run
airto start the app with hot reloading.
-
SuccessHandler for successful requests. -
BuildErrorHandler for build errors. -
Start new PGX trx from
controllersonly to ensure proper transaction handling. -
/api/v1is the base path for all routes except/for health check. -
/modelsconsider 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
productsanduserstable 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
);