Hello there!
In this blog, I will share how I write my API using Golang with a simple boilerplate that I create. This guide will help you set up a robust starting point for your Golang API projects.
What you need is:
Reflect
Reflex is a small tool to watch a directory and rerun a command when certain files change. If you are familiar with the JavaScript ecosystem, youβll find this package similar tonpm watch
.Fiber
Fiber is an Express-inspired web framework built on top of Fasthttp, the fastest HTTP engine for Go. We'll use Fiber for our API framework.Dotenv
For managing configuration files, we'll use dotenv. This package loads environment variables from a.env
file intoos.Getenv()
.JWT
For authorization, we will use JWT (JSON Web Tokens).GORM
GORM is an ORM library for Golang. Weβll use GORM to handle database operations.Dbmate
Dbmate is a database migration tool. Weβll use Dbmate for handling database migrations.
Now we're ready to create the boilerplate, let's get started π
Setting Up the Boilerplate
Let's get started by setting up the boilerplate structure. Here's what our project structure will look like:
βββ Makefile
βββ README.md
βββ config
βΒ Β βββ config.go
βββ db
βΒ Β βββ connect.go
βΒ Β βββ migrations
βΒ Β βΒ Β βββ 20240722151143_users.sql
βΒ Β βββ schema.sql
βΒ Β βββ seeder.go
βββ go.mod
βββ go.sum
βββ main.go
βββ pkg
βΒ Β βββ controllers
βΒ Β βΒ Β βββ login-controller.go
βΒ Β βββ dto
βΒ Β βΒ Β βββ user-dto.go
βΒ Β βββ models
βΒ Β βΒ Β βββ user.go
βΒ Β βββ utils
βΒ Β βββ pagination.go
βΒ Β βββ validator.go
βββ router
βΒ Β βββ router.go
βββ server
File and Directory Descriptions
- Makefile: Automates common tasks like running tests, building the project, and generating files.
- README.md: Documentation for the project.
- config/config.go: Handles configuration loading and setup.
-
db/:
- connect.go: Manages database connections.
- migrations/: Contains SQL migration files.
- schema.sql: Defines the database schema.
- seeder.go: Seeds the database with initial data.
- go.mod and go.sum: Dependency management files.
- main.go: The entry point of the application.
-
pkg/:
- controllers/: Contains the controller logic.
- dto/: Data transfer objects.
- models/: Defines the data models.
- utils/: Utility functions.
- router/router.go: Sets up the routing for the application.
- server/: Manages server startup and configuration.
Creating the Makefile
Next, weβll create a Makefile to automate simple tasks such as generating migration files and models.
Here is a basic Makefile
example:
.PHONY: migrate
migrate:
dbmate up
.PHONY: rollback
rollback:
dbmate down
.PHONY: create-migration
create-migration:
dbmate new $(name)
build:
go build -o server main.go
run: build
./server
watch:
reflex -s -r '\.go$$' make run
Next Steps
In the next part of this series, we'll start coding and set up our project by creating the necessary configuration files, setting up the Fiber framework, and initializing the database.
Stay tuned for Part 2, where we dive into the actual coding!
Top comments (0)