# distraught

> Web Server

Latest version **5.0.0** (published 2019-09-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install distraught
pnpm add distraught
yarn add distraught
bun add distraught
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2019-09-27 |
| First published | 2016-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 8.1.4 |
| Dependencies | 41 |
| Unpacked size | 387.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | jwdotjs |
| Maintainers | esvinson, jwdotjs, mattlorey |

## Links

- npm: https://www.npmjs.com/package/distraught
- Repository: https://github.com/knledg/distraught
- Homepage: https://github.com/knledg/distraught#readme
- Issues: https://github.com/knledg/distraught/issues
- npm.io page: https://npm.io/package/distraught

## Dependencies (41)

- [pg](https://npm.io/package/pg.md) ^6.4.2
- [pug](https://npm.io/package/pug.md) ^2.0.0-rc.4
- [cron](https://npm.io/package/cron.md) ^1.7.2
- [glob](https://npm.io/package/glob.md) ^7.1.4
- [knex](https://npm.io/package/knex.md) ^0.19.4
- [axios](https://npm.io/package/axios.md) >= 0.18.1
- [chalk](https://npm.io/package/chalk.md) ^2.4.2
- [lusca](https://npm.io/package/lusca.md) ^1.6.1
- [raven](https://npm.io/package/raven.md) ^2.4.2
- [redis](https://npm.io/package/redis.md) ^2.6.3
- [helmet](https://npm.io/package/helmet.md) ^3.21.1
- [lodash](https://npm.io/package/lodash.md) >= 4.17.15
- [moment](https://npm.io/package/moment.md) >= 2.12.0
- [morgan](https://npm.io/package/morgan.md) ^1.9.1
- [twilio](https://npm.io/package/twilio.md) ^3.11.3
- [yamljs](https://npm.io/package/yamljs.md) ^0.3.0
- [express](https://npm.io/package/express.md) ^4.17.1
- [numeral](https://npm.io/package/numeral.md) ^2.0.6
- [flow-bin](https://npm.io/package/flow-bin.md) ^0.66.0
- [passport](https://npm.io/package/passport.md) ^0.3.2
- [raven-js](https://npm.io/package/raven-js.md) ^3.15.0
- [sendgrid](https://npm.io/package/sendgrid.md) ^5.2.3
- [socket.io](https://npm.io/package/socket.io.md) ^2.0.3
- [body-parser](https://npm.io/package/body-parser.md) ^1.19.0
- [compression](https://npm.io/package/compression.md) ^1.7.4
- [pretty-error](https://npm.io/package/pretty-error.md) ^2.1.1
- [connect-redis](https://npm.io/package/connect-redis.md) ^3.3.0
- [express-flash](https://npm.io/package/express-flash.md) 0.0.2
- [cookie-session](https://npm.io/package/cookie-session.md) ^2.0.0-beta.2
- [passport-local](https://npm.io/package/passport-local.md) ^1.0.0
- [passport-oauth](https://npm.io/package/passport-oauth.md) ^1.0.0
- [express-session](https://npm.io/package/express-session.md) ^1.15.3
- [passport-github](https://npm.io/package/passport-github.md) ^1.1.0
- [socket.io-redis](https://npm.io/package/socket.io-redis.md) ^5.2.0
- [passport-facebook](https://npm.io/package/passport-facebook.md) ^2.1.1
- [@smartrent/heretic](https://npm.io/package/@smartrent/heretic.md) ^0.5.2
- [swagger-ui-express](https://npm.io/package/swagger-ui-express.md) ^2.0.7
- [@google-cloud/storage](https://npm.io/package/@google-cloud/storage.md) ^1.7.0
- [passport-google-oauth](https://npm.io/package/passport-google-oauth.md) ^1.0.0
- [swagger-express-middleware](https://npm.io/package/swagger-express-middleware.md) ^1.0.0-alpha.12
- [@smartrent/express-status-monitor](https://npm.io/package/@smartrent/express-status-monitor.md) ^1.2.6

## Recent versions

- 5.0.0 (latest) — 2019-09-27
- 4.6.0 — 2019-03-13
- 4.5.1 — 2018-10-24
- 4.5.0 — 2018-10-24
- 4.4.0 — 2018-10-09
- 4.3.0 — 2018-09-13
- 4.2.1 — 2018-08-28
- 4.2.0 — 2018-08-15
- 4.1.0 — 2018-06-23
- 4.0.1 — 2018-05-22
- 4.0.0 — 2018-05-21
- 3.0.0 — 2018-04-23
- 2.1.1 — 2018-03-16
- 2.1.0 — 2018-03-16
- 2.0.1 — 2018-03-08
- … 52 more at https://npm.io/package/distraught/versions

## README

# Distraught Web Server

> I was distraught!!

Distraught is a wrapper around a Node.js Express server that exposes an HTTPServer for web requests, a CronServer for functions that need to be called at set intervals, and a WorkerServer to handle long requests. 

## Assumptions

- You have a webserver running Node.js
- Postgresql as the datastore
- Redis as the caching layer and sockets
- RabbitMQ for worker jobs

## Libraries

* [ExpressJS](https://expressjs.com)
* [Mariner - Vanilla SQL Database Migrations](https://www.npmjs.com/package/mariner)
* [Heretic - Queueing / Dequeueing Jobs](https://www.npmjs.com/package/heretic)
* [Google Cloud Storage - File Storage](https://www.npmjs.com/package/@google-cloud/storage)
* [Sentry - Error Handling](https://www.npmjs.com/package/raven)
* [Cron - For Timed Jobs](https://www.npmjs.com/package/cron)
* [Axios - For API Requests](https://www.npmjs.com/package/axios)
* [Sendgrid - Sending Emails](https://www.npmjs.com/package/sendgrid) 
* [Twilio - Text Messaging](https://www.npmjs.com/package/twilio)
* [Socket.IO - Websockets](https://www.npmjs.com/package/socket.io)
* [Swagger - API Documentation](https://swagger.io/swagger-ui/)

## Expected Migrations / Tables In Order To Use WorkerServer

This does require some migrations to be ran, however this server does -not- run the migrations on startup. If you are using Distraught for the first time, please run the following migration:

```sql
CREATE TABLE heretic_jobs (
  id SERIAL NOT NULL PRIMARY KEY,
  queue_name text NOT NULL,
  status text DEFAULT 'pending',
  payload jsonb,
  attempt_logs jsonb[] DEFAULT '{}',
  max_attempts int NOT NULL DEFAULT 1,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now(),
  last_attempted_at timestamptz
);

CREATE INDEX ON heretic_jobs (queue_name);
CREATE INDEX ON heretic_jobs (status);

CREATE FUNCTION heretic_updated_at_timestamp() RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = now();
  RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';

CREATE TRIGGER update_heretic_jobs_updated_at
  BEFORE UPDATE ON heretic_jobs
  FOR EACH ROW EXECUTE PROCEDURE heretic_updated_at_timestamp();
```

## Processes

The framework is setup to run three processes: web, crons, and workers.

- Web
  - Will boot up the Express server
- Crons
  - These are processes that run in the background at set intervals
- Workers
  - Workers are background processes that may take quite a bit of time to complete, so they are enqueued to a RabbitMQ server via [Heretic, a simple RabbitMQ interface](https://github.com/bjyoungblood/heretic)

## Logging

- Supports sending all server logs to [Logentries](https://logentries.com/) if a `LOGENTRIES_TOKEN` is present.
- Supports sending uncaught/unhandled errors to [Sentry](https://sentry.io) if a `SENTRY_DSN` is present.

### Logging Errors

```javascript
const {logErr} = require('distraught');

logErr(new Error('Whoa..That\'s not good'), {
  var1: val1,
  var2: val2,
});
```

### Server Monitoring

In `httpserver`, if you specify `enableStatusMonitor: true`, a `/status` page will be accessible on the same port that the Express server is running that memory usage, CPU usage, response time, requests per second, and number of responses by status code using [express-status-monitor](https://www.npmjs.com/package/express-status-monitor)

## Database

- Utilizes [Knex](http://knexjs.org/) to handle your queries

### Setting Up Your Connections

```javascript

const d = require('distraught');

d.init({
  db: {
    r: { connection: process.env.READONLY_DATABASE_URL },
    rw: { connection: process.env.READWRITE_DATABASE_URL },
  },
  heretic: {
    default: { db: "r", connection: process.env.AMQP_URL },
  },
  cache: {
    default: { connection: process.env.REDIS_URL },
  },
  captureUncaught: true,
  captureUnhandled: true,
});
```

### Querying The Database

```javascript

const {db, toCamelCase, createOne} = require('distraught');

function fetchUsers() {
  return db.r('users')
    .column(['*'])
    .limit(1000)
    .then(toCamelCase);
}

function addUser(user) {
  return createOne(db.rw, 'users', user)
    .then(toCamelCase);
}
```

## HTTPServer

```javascript
const {httpServer, init} = require('distraught');

init({ 
  cache: {
    default: { connection: process.env.REDIS_URL },
  },
});

const homeController = require('./controllers/home');

const server = httpServer({
  publicPath: path.join(__dirname, 'public'),
  viewPath: path.join(__dirname, 'views'),
  findUserById(id: number) {
    return cache.default.getOrSet(`user-${id}`, fetchUserById.bind(null, id)); // Needed for passport middleware
  },
});

server.app.use((req, res, next) => {
  // ...some middleware/plugin logic
  next();
});

/* WEB ROUTES */
server.app.get('/', homeController.get);

authController.setAuthRoutes(server.app, server.passport);

server.start();
```

### Templates

By default, HTTPServer will render pug templates, but you can change the view engine to whatever you want during instantiation. 

#### Adding/Changing View Engines

```javascript
const {httpServer} = require('distraught');

const server = httpServer({
  viewEngine: 'jsx',
});

server.app.engine('jsx', (filePath, options, callback) => { 
  const html = templateRenderFn(filePath, options);
  callback(null, html);
});
```

### Swagger

To enable Swagger:

```javascript
const server = httpServer({
  swaggerConfig: {
    appRoot: __dirname,
    yamlPath: path.join(__dirname, 'api/swagger/swagger.yaml'),
  },
});
```

[Example of Creating API/Config Folders and Using the Swagger Editor](https://github.com/swagger-api/swagger-node)
[Swagger - Getting Started](https://github.com/swagger-api/swagger-node/blob/master/docs/README.md)


## WorkerServer

```javascript
// Make sure the Heretic database migration has run

const {init, workerServer, MINUTE, heretic, chalk, log} = require('distraught');

init({
  db: {
    r: { connection: process.env.READONLY_DATABASE_URL },
    rw: { connection: process.env.READWRITE_DATABASE_URL },
  },
  heretic: {
    default: { db: "r", connection: process.env.AMQP_URL },
  },
});


function testDequeue(job, message, done) {
  log(chalk.yellow('Dequeueing job: Test queue'));
  return Promise.resolve()
    .then(done);
}

function queueJob() {
  heretic.default.enqueue('test.dequeue', {});
  setTimeout(() => {
    queueJob();
  }, 5000);
}

function startWorkerServer() {
  const debug = process.env.WORKER_DEBUG;
  const workers = workerServer({
    heretic: heretic.default,
    requiredEnv: [],
    queues: [
      {name: 'test.dequeue', concurrency: 3, handler: testDequeue, isEnabled: process.env.NODE_ENV === 'development', alertAt: MINUTE, killAt: MINUTE * 2, debug},
    ],
  });
  workers.start();
};

queueJob();
startWorkerServer();
```

### Enqueueing jobs

```javascript
import {heretic} from 'distraught';

heretic.default.enqueue('test.dequeue', {});
```

## Crons

```javascript
const {cronServer, log, chalk} = require('distraught');

exports.startCronServer = () => {
  cronServer({
    crons: [
      {
        name: 'Ping',
        cronTime: '* * * * * *', // Every second
        onTick() {
          log(chalk.green('Pong'));
        },
      },
    ],
  });
};
```

## Caching

### Caching Individual Functions

Getting value from cache by key, or setting it via a function

```javascript
  const {init, cache, MINUTE} = require('distraught');

  init({
    cache: {
      default: { connection: process.env.REDIS_URL },
    },
  });

  const getValueFn = () => {
    return someFuncReturningData();
  }; // Can be a scalar, function returning a scalar, or function returning a Promise
  const ttl = MINUTE * 3;

  function getUsers() {
    return cache.default.getOrSet('all-users', getValueFn, ttl)
      .then((users) => console.log(users));
  }

  await getUsers(); // Cache missed
  await getUsers(); // Cache hit
```

### Invalidating Keys

The below example will remove `all-users` from the cache

```javascript
  const {cache} = require('distraught');
  cache.default.invalidate('all-users');
```

### Thanks

Thanks to [Hackathon Starter](https://github.com/sahat/hackathon-starter) for a lot of inspiration

---
_Source: https://npm.io/package/distraught · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
