-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
42 lines (31 loc) · 1.03 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import Koa from 'koa';
import pino from 'koa-pino-logger';
import Router from 'koa-router';
const koaBody = require('koa-body');
import { RedisClient } from './redisClient';
import { config } from '../config';
const router = new Router();
const logger = pino();
const redisClient = new RedisClient(
config.get('redis.hostname'),
config.get('redis.cacheExpiryTime'),
config.get('redis.maxCapacity')
);
const app = new Koa();
console.log('Run using config:', config.toString());
app.use(logger);
app.use(koaBody());
router
.get('/:key', async (ctx) => {
const cacheValue = await redisClient.get(ctx.params.key);
ctx.body = cacheValue;
})
.post('/', async (ctx) => {
const { key, value } = (ctx.request as any).body as { key: string, value: any };
const success = await redisClient.atomicSet(key, value);
ctx.body = { success };
});
app.use(router.routes());
const appPort = config.get('port');
app.listen(appPort);
console.info(`Listening to http://localhost:${appPort} 🚀`);