-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
87 lines (76 loc) · 2.17 KB
/
server.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var express = require('express')
var app = express()
var router = express.Router()
var Factstore = require('./elasticfactstore')
var suggestionStore = require('./suggestionstore')
var port = process.env.PORT || 8080
var bodyParser = require('body-parser')
var path = require('path')
var middleware = require('swagger-express-middleware')
var factStore = new Factstore()
router.get('/date/:date', function (req, res) {
factStore.getByDate(req.params.date, req.query.page)
.then(function (facts) {
res.json(facts)
})
.catch(function (err) {
res.send(err)
})
})
router.get('/item/:item', function (req, res) {
factStore.getByItem(req.params.item, req.query.page)
.then(function (facts) {
res.json(facts)
})
.then(function (err) {
res.send(err)
})
})
router.get('/2item/:item1/:item2', function (req, res) {
factStore.getBy2Item(req.params.item1, req.params.item2)
.then(function (facts) {
res.json(facts)
})
.then(function (err) {
res.send(err)
})
})
router.get('/wddg', function (req, res) {
suggestionStore.action(req.query)
.then(function (suggestions) {
res.jsonp(suggestions)
})
.catch(function (err) {
res.status(500)
res.json({ error: err.message })
})
})
function errorHandler (err, req, res, next) {
res.status(500)
res.send(err)
}
middleware('dist/swagger.yaml', app, function (err, middleware) {
if (err) throw err
app.use(middleware.metadata())
app.use(middleware.parseRequest())
app.use(middleware.validateRequest())
app.use(bodyParser.json())
app.get('/wikifactmine-api/', function (req, res) {
res.sendFile(path.join(__dirname, '/dist/index.html'))
})
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '/dist/index.html'))
})
// enable cors
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.use('/api', router)
app.use('/wikifactmine-api/api', router)
app.use('/wikifactmine-api/', express.static('dist'))
app.use('/', express.static('dist'))
app.use(errorHandler)
app.listen(port)
})