Self-Hosting Your Project πŸš€

This section explains how to deploy and run your project in a self-hosted environment. You have three main options:

  1. Using Docker directly with the provided Dockerfile 🐳
  2. Using the Makefile for an automated build and run process βš™οΈ
  3. Executing the Go code directly with command-line flags πŸ‘¨β€πŸ’»

Option 1: Docker Deployment 🐳

The Docker deployment is based on the production Dockerfile located at docker/Dockerfile.prod. Follow these steps:

1. Prepare the Environment File πŸ“„

Copy the example.env file to .env and edit the file to fill in your parameters:

HOST="localhost"
PORT=8080
EMAIL_ADDR="test@test.com"
EMAIL_PASS="smtp_server_password"
EMAIL_HOST="smtp.example.com"
EMAIL_PORT=587
SECRET="my_backend_secret"

2. Build the Docker Image πŸ—οΈ

Run the following command in the root of your project to build the image:

docker build -f docker/Dockerfile.prod -t simpleauthlink .

3. Run the Docker Container 🚒

Once the image is built, start a container using the environment file:

docker run --name simpleauthlink --env-file .env -p 8080:80 simpleauthlink

This command maps the container’s port 80 to port 8080 on your host.


Option 2: Using the Makefile βš™οΈ

The Makefile in the root of the project simplifies the build and run process. It contains two targets: api (for building and running the API container) and clean-api (for cleaning up previous containers and images).

1. Set Up the Environment File πŸ“‹

As with the Docker approach, copy and edit example.env to .env with your configuration:

HOST="localhost"
PORT=8080
EMAIL_ADDR="test@test.com"
EMAIL_PASS="smtp_server_password"
EMAIL_HOST="smtp.example.com"
EMAIL_PORT=587
SECRET="my_backend_secret"

2. Build and Run Using Make πŸƒβ€β™‚οΈ

Run the following command from your project root:

make run api

This will:

  • Clean up any previous containers and images.
  • Build the Docker image using the docker/Dockerfile.prod.
  • Run the container with the environment variables from .env and expose the API on the configured port.

Option 3: Running the Go Code Directly πŸ‘¨β€πŸ’»

If you prefer to run the API without Docker, you can execute the Go code directly. The entry point is cmd/authapi/main.go and it accepts several flags for configuration.

The available flags include:

  • --host or environment variable HOST (default: "localhost")
  • --port or environment variable PORT (default: 8080)
  • --email-addr or environment variable EMAIL_ADDR (required)
  • --email-pass or environment variable EMAIL_PASS (required)
  • --email-host or environment variable EMAIL_HOST (required)
  • --email-port or environment variable EMAIL_PORT (default: as defined in code)
  • --secret or environment variable SECRET (required)

Running Directly πŸƒβ€β™€οΈ

Set the required environment variables or pass them as command-line flags. For example:

go run cmd/simpleauthlink/main.go \
  --host="localhost" \
  --port=8080 \
  --email-addr="test@test.com" \
  --email-pass="smtp_server_password" \
  --email-host="smtp.example.com" \
  --email-port=587 \
  --secret="my_backend_secret"

Or set the environment variables first:

export HOST="localhost"
export PORT=8080
export EMAIL_ADDR="test@test.com"
export EMAIL_PASS="smtp_server_password"
export EMAIL_HOST="smtp.example.com"
export EMAIL_PORT=587
export SECRET="my_backend_secret"

go run cmd/simpleauthlink/main.go

Choose the option that best fits your deployment needs. The Docker and Makefile options are ideal for containerized environments, while running the Go code directly might be preferred for development or custom setups.