1
|
|
|
import express from 'express'; |
2
|
|
|
import bodyParser from 'body-parser'; |
3
|
|
|
import chronicle, { middlewares } from '../../src'; |
4
|
|
|
import { citiesList, cityShow, cityUpdate, cityCreate, forecastsList, forecastUpdate } from './routes'; |
5
|
|
|
|
6
|
|
|
const port = process.env.PORT || 3005; |
7
|
|
|
const chr = middlewares.express(chronicle, { |
8
|
|
|
save : { |
9
|
|
|
uniqueFilter : [ 'context.title', 'request.method', 'response.status.code', 'url.path' ], |
10
|
|
|
files : [ { reporter: 'lite', path: './documentation/api.md' } ] |
11
|
|
|
} |
12
|
|
|
}); |
13
|
|
|
|
14
|
|
|
chronicle.setConfig({ |
15
|
|
|
headers : { |
16
|
|
|
request : { |
17
|
|
|
include : [ 'authorization' ], |
18
|
|
|
sanitize : { |
19
|
|
|
authorization : () => "'API KEY'" |
20
|
|
|
} |
21
|
|
|
}, |
22
|
|
|
response : { |
23
|
|
|
include : [] |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
}); |
27
|
|
|
|
28
|
|
|
const app = express(); |
29
|
|
|
const router = express.Router(); |
30
|
|
|
|
31
|
|
|
router.use(bodyParser.json()); |
32
|
|
|
|
33
|
|
|
router.get('/cities', chr('Cities', 'Get list of cities'), citiesList); |
34
|
|
|
router.get('/cities/:id', chr('Cities', 'Get one city'), cityShow); |
35
|
|
|
router.patch('/cities/:id', chr('Cities', 'Update Existing City'), cityUpdate); |
36
|
|
|
router.post('/cities', chr('Cities', 'Create new City'), cityCreate); |
37
|
|
|
router.patch('/forecasts/:id', chr('Forecasts', 'Update Forecast'), forecastUpdate); |
38
|
|
|
|
39
|
|
|
// Use function in middleware for full controll |
40
|
|
|
function customTitle(req) { |
41
|
|
|
return { |
42
|
|
|
group : 'Forecasts', |
43
|
|
|
title : req.query.cityId |
44
|
|
|
? 'Forecasts for specific city' |
45
|
|
|
: 'Get list of forecasts' |
46
|
|
|
}; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
router.get('/forecasts', chr(customTitle), forecastsList); |
50
|
|
|
|
51
|
|
|
app.use('/api', router); |
52
|
|
|
|
53
|
|
|
export default app; |
54
|
|
|
|
55
|
|
|
if (!process.env.TEST) { |
56
|
|
|
app.listen(port, () => { |
57
|
|
|
console.log(`Weather server is running on ${port}`); |
|
|
|
|
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|