DEV Community

Cover image for Setup The Structure of Your Boilerplate (Part 1)
Sheena Zien
Sheena Zien

Posted on • Edited on

Setup The Structure of Your Boilerplate (Part 1)

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:

  1. 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 to npm watch.

  2. 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.

  3. Dotenv
    For managing configuration files, we'll use dotenv. This package loads environment variables from a .env file into os.Getenv().

  4. JWT
    For authorization, we will use JWT (JSON Web Tokens).

  5. GORM
    GORM is an ORM library for Golang. We’ll use GORM to handle database operations.

  6. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)