-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
28 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
"debug": "4.3.1", | ||
"dnscache": "^1.0.2", | ||
"express": "^4.17.1", | ||
"pg": "8.5.1" | ||
"pg": "8.5.1", | ||
"redis": "3.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createClient } from 'redis'; | ||
import { promisify } from 'util'; | ||
const client = createClient({ | ||
host: 'localhost', | ||
port: 6379 | ||
}) | ||
client.on('error', (e) => console.log(e)) | ||
export const getAsync = promisify(client.get).bind(client) | ||
export const setAsync = promisify(client.set).bind(client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
import { Router } from 'express' | ||
import createError from 'http-errors' | ||
import controller from '../controller/Stormtrooper.js' | ||
import { getAsync } from '../config/redis.js' | ||
const trooperRoutes = new Router() | ||
|
||
const verifyId = (request, response, next) => { | ||
const id = request.params.id | ||
if (!/^[0-9]+$/.test(id)) { | ||
return next(createError(422, 'invalid id')) | ||
} | ||
next() | ||
} | ||
|
||
const fromCache = (request, response, next) => { | ||
getAsync(`trooper:${request.params.id}`) | ||
.then(result => { | ||
if (!result) return next() | ||
response.send(JSON.parse(result)) | ||
}) | ||
.catch(_ => next()) | ||
} | ||
trooperRoutes.get('/', controller.list) | ||
trooperRoutes.get('/:id', verifyId, controller.byId) | ||
trooperRoutes.get('/:id', verifyId, fromCache, controller.byId) | ||
trooperRoutes.post('/', controller.create) | ||
trooperRoutes.put('/:id', verifyId, controller.updateById) | ||
trooperRoutes.delete('/:id', verifyId, controller.deleteById) | ||
|
||
export default trooperRoutes |