forked from creationix/weblit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.lua
52 lines (42 loc) · 1.35 KB
/
server.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
44
45
46
47
48
49
50
51
52
local ffi = require('ffi')
local hd = require('hoedown')
local markdown = [[
# Luvit Blob
This is a blog for luvit related stuffs.
]]
local function markdownToHtml(markdown)
local flags = hd.HOEDOWN_HTML_ESCAPE
local renderer = hd.hoedown_html_renderer_new(flags, 0)
local extensions = bit.bor(
hd.HOEDOWN_EXT_BLOCK,
hd.HOEDOWN_EXT_SPAN
)
local document = hd.hoedown_document_new(renderer, extensions, 16);
local html = hd.hoedown_buffer_new(16)
hd.hoedown_document_render(document, html, markdown, #markdown);
local string = ffi.string(html.data, html.size)
hd.hoedown_buffer_free(html)
hd.hoedown_document_free(document)
hd.hoedown_html_renderer_free(renderer)
return string
end
local function renderIndex(req, res)
res.code = 200
res.body = markdownToHtml(markdown)
res["Content-Type"] = "text/html"
end
require('weblit-app')
.bind {host = "0.0.0.0", port = 8080 }
-- Set an outer middleware for logging requests and responses
.use(require('weblit-logger'))
-- This adds missing headers, and tries to do automatic cleanup.
.use(require('weblit-auto-headers'))
-- A caching proxy layer for backends supporting Etags
.use(require('weblit-etag-cache'))
--
.route({
method = "GET",
path = "/",
}, renderIndex)
-- Bind the ports, start the server and begin listening for and accepting connections.
.start()