Skip to content

Shasta/metrics-rest

Repository files navigation

Metrics REST API

Overview

Trusted REST api that allows to check if the electric counter metadata is correctly stored in IPFS, and saves a backup to a MongoDB instance, to be able to recreate all the metrics history if IPFS goes down in an hypothecal case.

Getting Started

Clone the repo:

git clone git@github.com:Shasta/metrics-rest.git
cd metrics-rest

Install dependencies:

npm install

Set environment (vars):

cp .env.example .env

Start server:

# Start server
npm start

# Selectively set DEBUG env var to get logs
DEBUG=metrics-rest:* npm start

Refer debug to know how to selectively turn on logs.

Tests:

# Run tests written in ES6 
npm run test

# Run test along with code coverage
npm run test:coverage

# Run tests on file change
npm run test:watch

# Run tests enforcing code coverage (configured via .istanbul.yml)
npm run test:check-coverage

Lint:

# Lint code with ESLint
npm run lint

# Run lint on any file change
npm run lint:watch
Deployment
# compile to ES5
1. npm run build

# upload dist/ to your server
2. scp -rp dist/ user@dest:/path

# install production dependencies only
3. NODE_ENV=production npm run start

# Use any process manager to start your services
4. pm2 start dist/index.js

Logging

Universal logging library winston is used for logging. It has support for multiple transports. A transport is essentially a storage device for your logs. Each instance of a winston logger can have multiple transports configured at different levels. For example, one may want error logs to be stored in a persistent remote location (like a database), but all logs output to the console or a local file. We just log to the console for simplicity, you can configure more transports as per your requirement.

API logging

Logs detailed info about each api request to console during development. Detailed API logging

Error logging

Logs stacktrace of error to console along with other details. You should ideally store all error messages persistently. Error logging

Code Coverage

Get code coverage summary on executing yarn test Code Coverage Text Summary

yarn test also generates HTML code coverage report in coverage/ directory. Open lcov-report/index.html to view it. Code coverage HTML report

Docker

Using Docker Compose for Development

# service restarts on file change
bash bin/development.sh

Building and running without Docker Compose

# To use this option you need to make sure mongodb is listening on port 27017

# Build docker 
docker build -t express-mongoose-es6-rest-api .

# Run docker
docker run -p 4040:4040 express-mongoose-es6-rest-api

About

REST API for relaying electric counter metadata to IPFS and keep backups in MongoDB.

Resources

Stars

Watchers

Forks

Packages

No packages published