aboutsummaryrefslogtreecommitdiff
path: root/vnext/server/index.js
blob: 16abc8db088a6b3c96d777cbdd47391afb442a6f (plain) (blame)
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
import express from 'express'
import { raw } from 'body-parser'
import cors from 'cors'
import config from 'config'
import debug from 'debug'
const log = debug('http')

import serverRenderer from './middleware/renderer'
import event from './middleware/event'
import oembed from './middleware/oembed'
import urlExpand from './middleware/urlexpand'

const PORT = process.env.LISTEN_PORT || 8081
import path from 'path'
import { webhook, webhookPath } from './durov'

// initialize the application and create the routes
const app = express()
app.use(raw())
app.use(cors())
const router = express.Router()

router.post('/api/v2/sender', event)
router.get('/api/v2/oembed', oembed)
router.get('/api/v2/urlexpand', urlExpand)

const durov_webhook = webhookPath()
if (durov_webhook) {
    router.post(`/api/v2/${durov_webhook}`, webhook)
}
router.use('^/$', serverRenderer)

const STATIC_ROOT = config.get('service.static_root') || path.resolve(__dirname, 'public')

// other static resources should just be served as they are
router.use(express.static(
    STATIC_ROOT,
    { maxAge: '30d' },
))

router.use('*', serverRenderer)

app.use(router)


// start the app
app.listen(PORT, (error) => {
    if (error) {
        return console.log('something bad happened', error)
    }

    log('listening on ' + PORT + '...')
})