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)