Skip to content

How to sappio

Bruno Peixoto edited this page Feb 26, 2023 · 30 revisions

Table of contents

  1. Description
  2. How to
  3. Features
  4. Recommended practices
  5. Backlog

Description

A simple logging app.

How to

Setup

There are below environment variables for the node app to work properly. We list them in the table below:

Name Reference description Default Optional
APP_HOST 0-65535 localhost Yes
APP_PORT 0-65535 3000 No
NUM_PORTS Number of listen ports 5 Yes
JWT_KEY string 1234 Yes
NODE_ENV development/production development No
LOGTAIL_TOKEN string No

Build

We must first install the required libraries to run and host, locally or remotely, the app. Therefore, we run the command below.

npm run build

Run and deploy

You may utilize some cloud service to host the app, like AWS EC2, Azure, or GCloud, but also we might run it locally.

We use Docker for build and run the application with npm run command below:

npm run docker:clean --tag=IMAGE-NAME
npm run docker:deploy --tag=IMAGE-NAME --from=3000 --to=8080

Stop and remove

You must follow the instructions below to stop and remove the service on the docker daemon:

npm run docker:clean --tag=IMAGE-NAME

After the command run, we will not see the deployed app by command run docker ps -a anymore.

Define routes

The available routes are on file /core/routers/root.js. You may either define new routes on this file or create another file in the same folder with the extension .js. For the latter, we require to use the defined router on an available app on file /core/app.js

In both cases, we utilize the library swagger-jsdoc to interpret the comments near routes. However, the documentation on the library swagger-autogen is useful for route extension. We define the swagger route on $host/swagger

Features

Available endpoint routes

We may go on the browser and type in the URL field the text $APP_HOST:$APP_PORT followed by available routes. The available routes are below to consult:

  1. /: The home or Sappio landing page;
  2. /all: It lists all available routes;
  3. /token: It provides a JWT token;
  4. /healthcheck: It provides a health object;
  5. /health: It provides other health object;
  6. /swagger: It renders a page with available gathered routes;
  7. /status: It displays a rudimentary machine usage and app availability UI;

TAKE NOTE: Both variables APP_HOST and APP_PORT are those available on file .env at project root folder.

Logging

At first, we direct logs to the console. The full logging experience happens by sign-up to LogTail and adequate environment variable .env fill up with row LOGTAIL_TOKEN=XXXXX. The string XXXXX refers to the respective provided token string.

Recommended practices

Express engine itself suggests several good practices here: https://expressjs.com/en/advanced/best-practice-performance.html. Good practices for express performance improvement include:

  • Synchronous and asynchronous functions: do it performatively;
  • Handle exceptions properly: by try-catch or promises.
  • Run your app in a cluster: we increase Node.js app performance greatly by launching a processes cluster (a cluster runs multiple instances of the app, distributing the load and tasks among the instances).
  • Cache request results: your app may not repeat the operation to serve the same request repeatedly. A performative caching uses some unique id: choose it properly.
  • Use a load balancer: run multiple instances and distribute the traffic, like Nginx or HAProxy.
  • Use a reverse proxy: it performs supporting operations on the requests. It can handle error pages, compression, caching, serving files, and load balancing among other things.

Backlog

  1. Add some other routes for educational purposes;
  2. Authenticate services from users and collaborative services;
  3. Create and seed an initial (choosable?) database;
  4. Provide different setups for development and production environments (for debugging and performance);