7 | 7 |
add '?/init.server'
|
8 | 8 |
|
9 | 9 |
require 'mmm'
|
|
10 |
|
10 | 11 |
import dir_base, load_tree from require 'build.util'
|
11 | 12 |
import Key from require 'mmm.mmmfs.fileder'
|
12 | 13 |
import SQLStore from require 'mmm.mmmfs.drivers.sql'
|
13 | 14 |
|
|
15 |
lfs = require 'lfs'
|
14 | 16 |
server = require 'http.server'
|
15 | 17 |
headers = require 'http.headers'
|
16 | 18 |
|
|
65 | 67 |
-- no facet given
|
66 | 68 |
facets = [{k.name, k.type} for k,v in pairs fileder.facets]
|
67 | 69 |
children = [f.path for f in *fileder.children]
|
68 | |
print facets
|
69 | |
print children
|
70 | 70 |
contents = tojson :facets, :children
|
71 | 71 |
200, contents
|
72 | 72 |
else
|
|
79 | 79 |
req = stream\get_headers!
|
80 | 80 |
method = req\get ':method'
|
81 | 81 |
path = req\get ':path'
|
|
82 |
|
|
83 |
if path\match '^/%?'
|
|
84 |
-- serve static assets, cheap hack for now
|
|
85 |
|
|
86 |
res = headers.new!
|
|
87 |
if method ~= 'GET' and method ~= 'HEAD'
|
|
88 |
res\append ':status', '405'
|
|
89 |
stream\write_headers res, true
|
|
90 |
return
|
|
91 |
|
|
92 |
static_path = "static/#{path\sub 3}"
|
|
93 |
|
|
94 |
if 'file' == lfs.attributes static_path, 'mode'
|
|
95 |
fd, err, errno = io.open static_path, 'rb'
|
|
96 |
|
|
97 |
if not fd
|
|
98 |
code = switch errno
|
|
99 |
when ce.ENOENT then '404'
|
|
100 |
when ce.EACCES then '403'
|
|
101 |
else '503'
|
|
102 |
|
|
103 |
res\upsert ':status', code
|
|
104 |
res\append 'content-type', 'text/plain'
|
|
105 |
|
|
106 |
assert stream\write_headers res, method == 'HEAD'
|
|
107 |
if method ~= 'HEAD'
|
|
108 |
assert stream\write_body_from_string err
|
|
109 |
|
|
110 |
else
|
|
111 |
suffix = static_path\match '%.([a-z]+)$'
|
|
112 |
type = switch suffix
|
|
113 |
when 'css' then 'text/css'
|
|
114 |
when 'lua' then 'application/lua'
|
|
115 |
when 'js' then 'application/javascript'
|
|
116 |
else 'application/octet-stream'
|
|
117 |
|
|
118 |
res\upsert ':status', '200'
|
|
119 |
res\append 'content-type', type
|
|
120 |
|
|
121 |
assert stream\write_headers res, method == 'HEAD'
|
|
122 |
if method ~= 'HEAD'
|
|
123 |
assert stream\write_body_from_file fd
|
|
124 |
else
|
|
125 |
res\upsert ':status', '404'
|
|
126 |
res\append 'content-type', 'text/plain'
|
|
127 |
|
|
128 |
assert stream\write_headers res, method == 'HEAD'
|
|
129 |
if method ~= 'HEAD'
|
|
130 |
assert stream\write_body_from_string "not found"
|
|
131 |
|
|
132 |
return
|
82 | 133 |
|
83 | 134 |
path, facet = dir_base path
|
84 | 135 |
facet = if #facet > 0
|