-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
87 lines (74 loc) · 1.98 KB
/
index.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
import express from 'express'
import compression from 'compression'
import helmet from 'helmet'
import cors from 'cors'
import {
getJsonAttributes,
getContractMetadata,
getNftMetadata,
renderHtml,
} from './src/serverUtils.js'
import cache from './src/cacheMiddleware.js'
import config from './src/config.js'
const port = parseInt(process.env.PORT, 10) || 3000
const server = express()
// remove powered-by
server.disable('x-powered-by')
// helmet
server.use(helmet({
frameguard: false,
contentSecurityPolicy: false,
dnsPrefetchControl: false,
crossOriginResourcePolicy: false,
crossOriginEmbedderPolicy: false,
}))
// cors
server.use(cors({
origin: '*',
credentials: true,
optionSuccessStatus: 200,
}))
// gzip
server.use(compression())
// ping
server.get('/ping', (req, res) => {
return res.status(200).send('Pong!')
})
// redirect calls to homepage
if (config.externalUri) {
server.get('/', (req, res) => {
return res.redirect(config.externalUri)
})
}
// contract metadata routes
server.get('/token/contract.json', cache(config.metadataCacheTtl), (req, res) => {
return res.type('json').send(JSON.stringify(getContractMetadata(), null, 2))
})
server.get('/token/:id.json', cache(config.metadataCacheTtl), (req, res) => {
const { id } = req.params
if (!getJsonAttributes(id)) {
return res.status(404).send('404 - Not found')
}
return res.type('json').send(JSON.stringify(getNftMetadata(id), null, 2))
})
//
server.get('/token/:id.html', cache(config.contentCacheTtl), (req, res) => {
const { id } = req.params
if (!getJsonAttributes(id)) {
return res.status(404).send('404 - Not found')
}
return res.type('html').send(renderHtml(id))
})
// static files
server.use(express.static('public', {
maxAge: 24 * 60 * 60 * 1000, // 24h
}))
// fallback route
server.all('*', (req, res) => {
res.status(404).send('404 - Not found')
})
// start server
server.listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})