Skip to content

How to create a service?

Abstract

Creating a service from the terminal is a simple process. The following steps will guide you through the process of creating a new service.

Basic setup

Supposing that you are in the root directory of the project, navigate to the service folder

cd services

Create a new service folder. By convention, the service folder name should be in lowercase and separated by a hyphen.

mkdir <service-name>

Navigate to the newly created service folder and create a README.md file. Once created add some information about the service purpose and how to run it. You can refer to some other services README.md files for inspiration.

cd <service-name>
touch README.md

If you need to use some environment variables you will have to create a .env file. Of course this file should not be committed to the repository as specified in the .gitignore file.

touch .env

Python services

To setup a python project you need to create a virtual environment and install poetry for dependency management. To do so, run the following commands:

python3 -m venv .venv
source .venv/bin/activate
pip install poetry

Initialize a new poetry project and follow the instructions to create a new project.

poetry init

The process would create a new pyproject.toml file. Depending on how you would like to create the python file you could either structure is as a python package or not. According to this choice you can specify package mode in the pyproject.toml file. An example of the file is shown below:

[tool.poetry]
name = "metadata"
version = "0.1.0"
description = ""
authors = ["RobertoChiosa <roberto.chiosa@polito.it>"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.11"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

When the project is created, you can add the dependencies to the project by running the following command:

poetry add <dependency>

You should always use this command to add dependencies to the project. This will ensure that the dependencies are added to the pyproject.toml file and that the installed package are compatible with the project.

To ensure code quality and style we use black ans isort tools. Add this into the configuration file:

[tool.isort]
profile = "black"
import_heading_stdlib = "Standard library imports"
import_heading_thirdparty = "Third party imports"
import_heading_firstparty = "Project imports"