diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2019-10-08 11:24:24 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2019-10-08 11:24:24 +0000 |
| commit | d63e8f54a87076cfbd3cb458c3e2128f46a9038d (patch) | |
| tree | 499c377a9e367b3f3e6746e1354be12bc14156ef | |
| parent | layout fixes: (diff) | |
| download | mmm-d63e8f54a87076cfbd3cb458c3e2128f46a9038d.tar.gz mmm-d63e8f54a87076cfbd3cb458c3e2128f46a9038d.zip | |
serve static assets from as ?...
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | Tupfile | 16 | ||||
| -rw-r--r-- | build/import.moon | 3 | ||||
| -rw-r--r-- | build/render_all.moon | 3 | ||||
| -rw-r--r-- | build/server.moon | 55 |
5 files changed, 67 insertions, 14 deletions
@@ -1,8 +1,8 @@ -dist -out db.sqlite3 ##### TUP GITIGNORE ##### ##### Lines below automatically generated by Tup. ##### Do not edit. .tup /.gitignore +/out +/static @@ -3,13 +3,15 @@ include_rules !download = |> ^ DOWNLOAD %O^ curl -L https://github.com/fengari-lua/fengari-web/releases/download/@(FENGARI_VERSION)/`basename %o` > %o |> !sassc = |> ^ SCSS %b^ sassc @(SASS_ARGS) %f %o |> | %o.map -# download fengari dependencies -: |> !download |> out/fengari-web.js -: |> !download |> out/fengari-web.js.map -: static/* |> cp %f out/ |> out/%b - # render stylesheet -: scss/main.scss |> !sassc |> out/main.css +: scss/main.scss |> !sassc |> static/main.css + +# download fengari dependencies +: |> !download |> static/fengari-web.js +: |> !download |> static/fengari-web.js.map # bundle for client loading -: mmm/.bundle.lua | <modules> |> ^ WRAP %d^ moon &(build)/bundle_module.moon %o --wrap %f |> out/mmm.bundle.lua +: mmm/.bundle.lua | <modules> |> ^ WRAP %d^ moon &(build)/bundle_module.moon %o --wrap %f |> static/mmm.bundle.lua + +# copy for static builds +: foreach static/* |> cp %f out/ |> out/%b diff --git a/build/import.moon b/build/import.moon index b5def43..e87f12d 100644 --- a/build/import.moon +++ b/build/import.moon @@ -39,8 +39,7 @@ with SQLStore name: output, verbose: true continue if file == '$order' filepath = "#{dirpath}/#{file}" - attr = lfs.attributes filepath - switch attr.mode + switch lfs.attributes filepath, 'mode' when 'file' key, value = load_facet file, filepath \create_facet fileder, key.name, key.type, value diff --git a/build/render_all.moon b/build/render_all.moon index 4fe827b..7797b7b 100644 --- a/build/render_all.moon +++ b/build/render_all.moon @@ -18,7 +18,8 @@ import load_tree from require 'build.util' -- moon render_all.moon [db.sqlite3] [startpath] { file, startpath } = arg -export BROWSER +export BROWSER, STATIC +STATIC = true tree = load_tree SQLStore :name tree = tree\walk startpath if startpath diff --git a/build/server.moon b/build/server.moon index b178636..a78f922 100644 --- a/build/server.moon +++ b/build/server.moon @@ -8,10 +8,12 @@ add '?/init' add '?/init.server' require 'mmm' + import dir_base, load_tree from require 'build.util' import Key from require 'mmm.mmmfs.fileder' import SQLStore from require 'mmm.mmmfs.drivers.sql' +lfs = require 'lfs' server = require 'http.server' headers = require 'http.headers' @@ -66,8 +68,6 @@ class Server -- no facet given facets = [{k.name, k.type} for k,v in pairs fileder.facets] children = [f.path for f in *fileder.children] - print facets - print children contents = tojson :facets, :children 200, contents else @@ -81,6 +81,57 @@ class Server method = req\get ':method' path = req\get ':path' + if path\match '^/%?' + -- serve static assets, cheap hack for now + + res = headers.new! + if method ~= 'GET' and method ~= 'HEAD' + res\append ':status', '405' + stream\write_headers res, true + return + + static_path = "static/#{path\sub 3}" + + if 'file' == lfs.attributes static_path, 'mode' + fd, err, errno = io.open static_path, 'rb' + + if not fd + code = switch errno + when ce.ENOENT then '404' + when ce.EACCES then '403' + else '503' + + res\upsert ':status', code + res\append 'content-type', 'text/plain' + + assert stream\write_headers res, method == 'HEAD' + if method ~= 'HEAD' + assert stream\write_body_from_string err + + else + suffix = static_path\match '%.([a-z]+)$' + type = switch suffix + when 'css' then 'text/css' + when 'lua' then 'application/lua' + when 'js' then 'application/javascript' + else 'application/octet-stream' + + res\upsert ':status', '200' + res\append 'content-type', type + + assert stream\write_headers res, method == 'HEAD' + if method ~= 'HEAD' + assert stream\write_body_from_file fd + else + res\upsert ':status', '404' + res\append 'content-type', 'text/plain' + + assert stream\write_headers res, method == 'HEAD' + if method ~= 'HEAD' + assert stream\write_body_from_string "not found" + + return + path, facet = dir_base path facet = if #facet > 0 facet = '' if facet == ':' |
