git.s-ol.nu mmm / d63e8f5
serve static assets from as ?... s-ol 3 years ago
5 changed file(s) with 67 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
0 dist
1 out
20 db.sqlite3
31 ##### TUP GITIGNORE #####
42 ##### Lines below automatically generated by Tup.
53 ##### Do not edit.
64 .tup
75 /.gitignore
6 /out
7 /static
22 !download = |> ^ DOWNLOAD %O^ curl -L https://github.com/fengari-lua/fengari-web/releases/download/@(FENGARI_VERSION)/`basename %o` > %o |>
33 !sassc = |> ^ SCSS %b^ sassc @(SASS_ARGS) %f %o |> | %o.map
44
5 # render stylesheet
6 : scss/main.scss |> !sassc |> static/main.css
7
58 # download fengari dependencies
6 : |> !download |> out/fengari-web.js
7 : |> !download |> out/fengari-web.js.map
8 : static/* |> cp %f out/ |> out/%b
9
10 # render stylesheet
11 : scss/main.scss |> !sassc |> out/main.css
9 : |> !download |> static/fengari-web.js
10 : |> !download |> static/fengari-web.js.map
1211
1312 # bundle for client loading
14 : mmm/.bundle.lua | <modules> |> ^ WRAP %d^ moon &(build)/bundle_module.moon %o --wrap %f |> out/mmm.bundle.lua
13 : mmm/.bundle.lua | <modules> |> ^ WRAP %d^ moon &(build)/bundle_module.moon %o --wrap %f |> static/mmm.bundle.lua
14
15 # copy for static builds
16 : foreach static/* |> cp %f out/ |> out/%b
3838 continue if file == '$order'
3939
4040 filepath = "#{dirpath}/#{file}"
41 attr = lfs.attributes filepath
42 switch attr.mode
41 switch lfs.attributes filepath, 'mode'
4342 when 'file'
4443 key, value = load_facet file, filepath
4544 \create_facet fileder, key.name, key.type, value
1717 -- moon render_all.moon [db.sqlite3] [startpath]
1818 { file, startpath } = arg
1919
20 export BROWSER
20 export BROWSER, STATIC
21 STATIC = true
2122
2223 tree = load_tree SQLStore :name
2324 tree = tree\walk startpath if startpath
77 add '?/init.server'
88
99 require 'mmm'
10
1011 import dir_base, load_tree from require 'build.util'
1112 import Key from require 'mmm.mmmfs.fileder'
1213 import SQLStore from require 'mmm.mmmfs.drivers.sql'
1314
15 lfs = require 'lfs'
1416 server = require 'http.server'
1517 headers = require 'http.headers'
1618
6567 -- no facet given
6668 facets = [{k.name, k.type} for k,v in pairs fileder.facets]
6769 children = [f.path for f in *fileder.children]
68 print facets
69 print children
7070 contents = tojson :facets, :children
7171 200, contents
7272 else
7979 req = stream\get_headers!
8080 method = req\get ':method'
8181 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
82133
83134 path, facet = dir_base path
84135 facet = if #facet > 0