-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
43 lines (38 loc) · 1.09 KB
/
init.lua
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
local path = require('path')
local Utopia = require('utopia')
local favicon = require('favicon')
local logger = require('logger')
local static = require('static')
local requestQuery = require('request-query')
local directory = require('directory')
local cors = require('cors')
local jsonResponse = require('json-response')
local _ = require('utopia-route')
local __dirname = module.dir
local app = Utopia:new()
local port = process.env.PORT or 8080
local publicDir = path.join(__dirname, 'public')
function customMiddleware (req, res, nxt)
if not req.query.static then
res:finish('Hello from Utopia and Docker on Zeit!')
else
nxt()
end
end
function pathMiddleware (req, res)
res:finish('Bar')
end
app:use(logger('short'))
app:use(cors())
app:use(jsonResponse({indent = 2}))
app:use(favicon())
app:use(requestQuery)
app:use(_.get('/json/:id', function (req, res)
res:json({foo = 'bar', id = req.params.id})
end))
app:use('/foo', pathMiddleware)
app:use(customMiddleware)
app:use(directory(publicDir))
app:use(static(publicDir))
app:listen(port)
print('Server started at localhost:' .. port)