Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
use ioredis instead of redis
Browse files Browse the repository at this point in the history
  • Loading branch information
August committed Sep 2, 2020
1 parent eb38f50 commit 868cb1a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ node_modules/
.vscode/
.idea/

package-*.json
*.lock
*.log
.env
81 changes: 65 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"actual-urlsafe-base64": "^1.0.0",
"argon2": "^0.26.2",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"ioredis": "^4.17.3",
"loggaby": "^1.0.1",
"mongodb": "^3.5.9",
"redis": "^3.0.2",
"snowflakey": "^0.2.0"
},
"devDependencies": {
Expand Down
30 changes: 24 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@ const express = require('express');
const Loggaby = require('loggaby');
const cookieParser = require('cookie-parser');
const snowflakey = require('snowflakey');
const redis = require('redis');
const RedisClient = require('ioredis');
const { MongoClient } = require('mongodb');
const { constants } = require('../lib/util');
const logger = new Loggaby();
const cors = require('cors');

require('dotenv').config();
const port = parseInt(process.env.API_PORT);

const port = parseInt(process.env.API_PORT || 3307);
const app = express();
app.use(express.json());
app.use(cookieParser());
app.use(cors());

MongoClient.connect(process.env.MONGODB_URI, { useUnifiedTopology: true, useNewUrlParser: true }, (err, client) => {
MongoClient.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017', { useUnifiedTopology: true, useNewUrlParser: true }, (err, client) => {
if (err) {
logger.error(`mongo connection failed: ${err}`);
process.exit();
}
logger.log('connected to database');
app.locals.db = client.db('profileplace');
});

app.locals.snowflakeWorker = new snowflakey.Worker({
epoch: constants.epoch,
workerId: 28,
Expand All @@ -29,9 +33,16 @@ app.locals.snowflakeWorker = new snowflakey.Worker({
processBits: 0,
incrementBits: 14
});
app.locals.redis = redis.createClient();

app.locals.redis.on('ready', () => logger.log('connected to redis'));
app.locals.redis = new RedisClient({
host: process.env.REDIS_HOST || 'localhost',
port: Number(process.env.REDIS_PORT) || 6379,
password: process.env.REDIS_PASSWORD,
db: Number(process.env.REDIS_DATABASE) || 9,
lazyConnect: true
});

app.locals.redis.once('ready', () => logger.log('connected to redis'));

const { readdir } = require('fs').promises;
const { join } = require('path').posix;
Expand All @@ -53,7 +64,14 @@ readdir(join(__dirname, 'routers')).then(async versions => {
console.error(`500 at ${req.path}: ${err}`);
});

app.listen(port, () => {
app.listen(port, async () => {
try {
await app.locals.redis.connect();
} catch (ex) {
logger.error(`unable to connect to redis:\n${ex}`);
process.exit();
}

logger.log(`API listening on port ${port}`);
logger.log(`Versions loaded: ${versions.join(', ')}`);
});
Expand Down

0 comments on commit 868cb1a

Please sign in to comment.