Skip to content

Run and configuration

Leonid Kozarin edited this page Apr 12, 2023 · 1 revision

Run

On a local machine (for development)

Docker

The easiest way to run the project on a local machine is to use the configuration described in the docker-compose.yaml file. This builds the application and launch it with overall infrastructure, packaged in Docker containers connected by a common network. Needless to say, you must have Docker and Docker Compose installed for all of these to work. If you prefer to use a terminal, run the following command to launch everything:

docker-compose up --build -d

For GoLand, you have to create a new configuration and specify the configuration file .env to be used (Modify options → Environment variables file).

Also, for development, it would be very nice to have direct network access to the database and Redis on localhost and be able to connect to them with IDE. To achieve this, specify both Docker Compose files:

docker-compose -f docker-compose.yaml -f docker-compose.override.yaml up --build -d

Don't forget to replace hosts in the .env file!

If you have Make installed, just use the following commands:

  • make start to launch everything;
  • make dev-env to replace hosts in the .env file to localhost (sed is required).

Anyway, you have to open the .env file and specify API_TOKEN with a valid token for your bot got from @BotFather.

Build an executable file

However, for debugging, it's a lot more convenient to use a static (almost) binary, to which you can easily attach a debugger. The rest of the infrastructure you can run as containers. To build the application, you may use the usual go build command, but the bot requires a significant number of parameters to be set as environment variables, making it inconvenient to fill them out manually. The GoLand IDE and EnvFile plugin are the rescue. Just use the following configuration as an example:

go build configuration

To run the infrastructure either use the following configuration:

run infrastructure only configuration

Or the command variant with services specified explicitly:

docker-compose -f docker-compose.yaml -f docker-compose.override.yaml up -d postgres redis

Tests

This part is simple. Execute: go test -v ./....

Or make test if Make is installed.

On a server (production mode)

On the server, the application and all infrastructure are deployed as Docker containers via Docker Compose. However, there is a couple of nuances. As the deployment diagram says, the server deployment has two additional components:

  • nginx-proxy is a special image of nginx web server, which creates virtual hosts for all containers with the VIRTUAL_HOST environment variable automatically.
  • acme-companion is a script that, for containers with the LETSENCRYPT_HOST environment variable, issues new TLS certificates and updates the configuration of nginx automatically.

These components are common to all services on the server and allow the admin not to worry about the certificates at the application level. The nginx exposes two standard ports (80 and 443), routing requests either by domain name or by path (which is set via the VIRTUAL_PATH variable, and VIRTUAL_DEST=/ truncates this prefix berfore passing the request further).

Configuration

The application can be configured with environment variables in the .env file.