Backend module
Explore the docs »
Report bug
·
Request feature
Degraded air quality is prejudicial to economy, nature and human lives. Air quality monitoring is an important task to be performed to alleviate the effects of poor air quality, e.g.: it allows governments to take action on events of high level air pollutants in the atmosphere. It also allows to verify if the actions performed were effective. Altough important, air quality monitoring is usually neglected, because it is costly, demanding a lot of resources (material, infrastructural, financial and human) to be performed. Due to its cost, it is necessary to look for cost-effective options.
This repository contains the backend module of a proposed air quality monitoring system. Backend module is responsible to store, process the data gathered by the sensors nodes and generate information about air quality. Backend module is composed by two services:
aqs-api
: A REST API that allows to manage informations about sensor nodes and collected readings;aqs-postgres
: Database service is responsible by storage of all information of sensor nodes and collected readings. PostgreSQL is the database adopted in the system.
Docker Compose is used to create and start both services.
Main technologies, libraries and CLI tools used to built the API:
- Node.js: Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine;
- PostgreSQL: a powerful, open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance;
- Docker: Docker containers wrap up software and its dependencies into a standardized unit for software development that includes everything it needs to run: code, runtime, system tools and libraries. This guarantees that your application will always run the same way and makes collaboration as simple as sharing a container image;
- Docker Compose: Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration;
- Date-fns: Modern JavaScript date utility library;
- Express: Fast, unopinionated, minimalist web framework for node;
- Sequelize: An easy-to-use multi SQL dialect ORM for Node.js
To manage the code style and formatting:
To get a local copy up and running follow these steps.
-
Host machine settings:
-
A host computer running following operational system:
Linux Ubuntu 18.04.5 LTS
or greater.or
-
If deploying services in a Raspberry Pi, use the following operational system:
Raspberry Pi OS (32-bit) Lite, version August 2020 release date 2020-08-20, kernel version 5.4
;
-
-
Git: Git is a free and open source distributed version control system. Check git download page to get instructions on how to install it.
-
Install Node.js: Follow the official tutorial. Node version used in this project:
v12.16.1 LTS
. -
Install a package manager for node:
- Yarn: Check yarn tutorial (Yarn 1.x (classic) was used in this project);
-
Install Docker: Follow the official tutorial. Docker version used in this project:
v19.03.13, build 4484c46d9d
. -
Install Docker Compose: Follow the official tutorial. Docker Compose version used in this project:
v1.27.4, build 40524192
.
Docker Compose will be used to install the project:
-
Clone the repository and navigate to the project directory:
Using ssh: git clone git@github.com:dzvid/aqs-api.git cd aqs-api Or using https: git clone https://github.com/dzvid/aqs-api.git cd aqs-api
-
Create the
.env
file for the environment variables of the application. Run the following command to create.env
file using.env.example
as a template:cp .env.example .env
In the .env file, set a value for the following environment variables:
API_PORT
: port to expose API in host. Default API port is3000
;NODE_ENV
: specifies to Node.js the environment in which the application is running:development
,production
ortesting
. Default isdevelopment
;DB_HOST
: Database server address. This value is overwritten by Docker Compose, but is necessary to be declared to run database migrations in step 3. Default value is127.0.0.1
;DB_PORT
: port to expose database (PostgreSQL) in host. Default database port is5432
;DB_NAME
: Defines the name of database to be created in PostgreSQL. Default name isaqs
;DB_USER
: This optional environment variable is used in conjunction withDB_PASS
to set a user and its password. This variable will create the specified user with superuser power and a database with the same name. If it is not specified, then the default userpostgres
will be used;DB_PASS
: This environment variable is required for you to use the PostgreSQL Docker image. It must not be empty or undefined. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by theDB_USER
environment variable.
⚠️ Make sure values were declared, they are used in thedocker-compose.yml
file to create the containers. -
PostgreSQL database storage: PostgreSQL Docker images provides two options available to store databases used by applications:
3.1 (Adopted in this project) Create a data directory on the host system (outside the container) and mount this to a directory visible from inside the container. This places the database files in a known location on the host system, and makes it easy for tools and applications on the host system to access the files. The downside is that the user needs to make sure that the directory exists, and that e.g. directory permissions and other security mechanisms on the host system are set up correctly. This is the default setting defined in this project, a volume is defined in the directory
./db/data
. In this step it is necessary ro remove the.gitignore
located at./db/data
, run the following command to remove it:rm db/data/.gitignore
3.2 Let Docker manage the storage of your database data by writing the database files to disk on the host system using its own internal volume management. This is the default and is easy and fairly transparent to the user. The downside is that the files may be hard to locate for tools and applications that run directly on the host system, i.e. outside containers. If you want to let Docker manage database storage, edit the file
docker-compose.yml
and remove the wholevolumes
section from the serviceaqs-postgres
. -
Create and start the containers using Docker Compose. Open a terminal window and run the following command:
docker-compose up
-
Run sequelize migrations to create database tables:
4.1 Install project dependencies, run the following command in terminal:
yarn
4.2 After installing dependencies, run sequelize migrations to create the database tables:
yarn sequelize db:migrate
-
Now you can access API service at:
http://localhost:3000
(If using defaultAPI_PORT
).
You are done with configuration! I hope everything is alright and you are ready to use the API service! 🎉
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the dev branch (
git push origin feature/AmazingFeature
) - Open a pull request
Distributed under the MIT License. See LICENSE
for more information.
David Oliveira - oliveiradavid.dev@gmail.com
Project Link: https://github.com/dzvid/aqs-api