Building and Starting the slimtoolkit/slim Project

This guide provides a step-by-step process for building and starting the slimtoolkit/slim project.

Prerequisites

  • Go installed
  • Docker installed

Building the Project

There are several ways to build the slim toolkit/slim project:

Using the Makefile:

The Makefile provides several convenient commands for building, testing, and cleaning the project:

1. Building for production:

make build

This command builds the project for production using the go build command and outputs the binary to ./bin/slim.

2. Building for development:

make build_dev

This command builds the project for development, including debugging symbols and other helpful information.

3. Building for M1 Macs:

make build_m1

This command builds the project specifically for Apple M1 Macs.

4. Building in Docker:

make build_in_docker

This command builds the project inside a Docker container using the provided Dockerfile. It is useful for cross-platform compatibility and ensuring consistent build environments.

5. Building for M1 Macs in Docker:

make build_m1_in_docker

This command builds the project specifically for M1 Macs inside a Docker container.

6. Running tests:

make test

This command runs the project’s unit tests.

7. Formatting code:

make fmt

This command formats the Go code using gofmt.

8. Cleaning the project:

make clean

This command removes the build artifacts and temporary files.

9. Inspecting the project:

make inspect

This command inspects the project’s structure and dependencies.

10. Getting help:

make help

This command displays the available Makefile functions.

Using Docker:

  1. Building the image:
docker build -t slim .

This command builds the Docker image using the Dockerfile in the project root directory.

  1. Running the container:
docker run -d -p 0:8080 slim

This command starts the Docker container in the background, exposing port 8080 to the host machine.

Starting the Project

After building the project, you can start the application using the following command:

./bin/slim

This command runs the slim binary generated during the build process.

Example Code Snippets:

Makefile (excerpt):

build:
    @go build -o ./bin/slim -ldflags "-s -w -X 'github.com/slimtoolkit/slim.Version=$(VERSION)'" ./cmd/slim

build_dev:
    @go build -o ./bin/slim -ldflags "-X 'github.com/slimtoolkit/slim.Version=$(VERSION)'" ./cmd/slim

Dockerfile:

FROM golang:1.19-alpine

WORKDIR /app

COPY . .

RUN go mod tidy
RUN go build -o slim ./cmd/slim

EXPOSE 0
CMD ["slim"]

Source: