Skip to content

Makefile Usage

Seakee edited this page Jul 11, 2024 · 1 revision

This document explains how to use the Makefile provided in the go-api project. The Makefile contains various targets to help you manage the build, run, and deployment processes of the application.

Variables

  • APP_NAME: The name of the application. Default is go-api.
  • TZ: The timezone for the Docker container. Default is Asia/Shanghai.
  • IMAGE_NAME: The name of the Docker image. Default is $(APP_NAME):latest.
  • CONFIG_DIR: The directory for configuration files. Default is $(shell pwd)/bin/configs.
  • GO_FLAGS: Flags for the go build command. Default is -ldflags="-s -w".
  • RUN_ENV: The environment in which the application will run. Default is local.

Targets

all

The default target that includes formatting, linting, testing, and building the application.

make all

fmt

Formats the source code using gofmt and organizes imports using goimports.

make fmt

build

Formats the source code and builds the Go binary.

make build

run

Runs the application binary.

make run

docker-build

Builds the Docker image for the application.

make docker-build

docker-run

Stops and removes any existing Docker container with the same name, then runs a new Docker container for the application.

make docker-run

docker-clean

Stops and removes any existing Docker container with the same name.

make docker-clean

clean

Cleans up build artifacts.

make clean

Example Usage

Default Usage

  1. Format the source code:

    make fmt
  2. Build the application:

    make build
  3. Run the application:

    make run
  4. Build the Docker image:

     make docker-build
  5. Run the Docker container:

    make docker-run
  6. Clean up build artifacts:

    make clean

Using Custom Parameters

You can override the default variables by passing them as arguments when invoking make. Here are some examples:

  1. Change the application name:

    make build APP_NAME=myapp
  2. Set a different timezone for the Docker container:

    make docker-build TZ=America/New_York
  3. Specify a custom Docker image name:

    make docker-build IMAGE_NAME=myapp:1.0.0
  4. Change the configuration directory:

    make docker-run CONFIG_DIR=/path/to/configs
  5. Set a different run environment:

    make docker-run RUN_ENV=production

Notes

  • Ensure that Docker is installed and running on your system before using the Docker-related targets.
  • Customize the variables in the Makefile as needed for your specific environment and requirements.

Clone this wiki locally