Coder Social home page Coder Social logo

smartlog-library's Introduction

Usage

export JWT_KEY=<secret_key>
const {permitRole, permitAction} = require('jwt-permit')
// JWT in headers : -H 'authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImFjY291bnRJZCI6IjM1NTIwMGU...'
// Permit by Roles
app.post('/',
  permitRole('admin_location','viewer_location'),
  handler
)
// Permit by Actions
app.post('/',
  permitAction('read_location','update_location'),
  handler
)

simple-kafka-node

An simple interface to work with kafka in nodejs

Usage

npm install simple-kafka-node

Message format

{
    "type" :"anEventType",
    "data" :"A data object"
}

Configuration

Environment variables

MESSAGING_HOST: kafka host, default "locahost:2181"
MESSAGING_CLIENT_ID: client id, default: "bidding"
MESSAGING_GROUP : message group, default: "bidding"

Consumer

const {consumer} = require('simple-kafka-node');

await consumer.subscribe('topic1', 'anEventOnTopic1', (eventData) => {
    logger.info(`New event ${eventData}`);
});
await consumer.connect();

Producer

const {producer} = require('simple-kafka-node'); await producer.sendMessage('topic1', data, key);

Generic consumer handler (a utility that handle event and report error to errorLog if error occurs)

const { genericHandler } = require('simple-kafka-node');

const handleFunction = async (data) => {
    // handle data
}
await consumer.subscribe('topic1', 'anEventOnTopic1', (eventData) => {
    return genericHandler(handleFunction, (err) => { logger.error(`${err}`); }, eventData);
  });

if "handleFunction" has error, a message will be send to "errorLog" topic, else an message is sent to "activityLog" topic

Resopnse json formatter

API response json format

Usage

init formatter

formatter/index.js

const formatter = require('response-json-formatter');
module.exports = formatter;

errorHandler middleware

index.js

const {
    errorHandler,
} = require('../formatter');
const express = require('express');

const app = express()
...
app.use(errorHandler(logOptions));

validate middleware

route.js

const { Router } = require('express');
const router = new Router();
const Joi = require('joi');
const {
    validate
} = require('../formatter');

const pathSchema = Joi.object().keys({
    locationIdClient: Joi.string().required(),
});

router.post('/',
    validate(createSchema, 'body'),
    createController)

response format modules

controller.js

const {
    okData,
    okDataList,
    okCreated,
} = require('../formatter');

const getItem = async (req, res, next) => {
    ...
    okData(res, item);
};

const getList = async (req, res, next) => {
    ...
    okDataList(res, list, { total: list.length });
};

const create = async (req, res, next) => {
    ...
    okCreated(res, data);
};

error instances

const {
    ApiError,
    ApiValidateError,
    ConflictedModificationError,
    ForbiddenAccessError,
    InternalTechnicalError,
    InvalidAuthenticationError,
    NotFoundError
} = require('../formatter');

smartlog-library's People

Contributors

smartlogvn avatar danghung-dev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.