aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-10-08 12:35:41 +0000
committers-ol <s-ol@users.noreply.github.com>2019-10-08 12:35:41 +0000
commit906f68559479253f7035f42c953bf81dec5331b6 (patch)
tree20acb2cfcb966f3c1c3cf3af486c2e94008fc5ce
parentserve static assets from as ?... (diff)
downloadmmm-906f68559479253f7035f42c953bf81dec5331b6.tar.gz
mmm-906f68559479253f7035f42c953bf81dec5331b6.zip
change server path mapping
-rw-r--r--build/server.moon114
-rw-r--r--mmm/mmmfs/layout.moon8
-rw-r--r--mmm/mmmfs/util.moon4
3 files changed, 64 insertions, 62 deletions
diff --git a/build/server.moon b/build/server.moon
index a78f922..4749736 100644
--- a/build/server.moon
+++ b/build/server.moon
@@ -63,7 +63,7 @@ class Server
if val
200, val
else
- 406, 'cant convert facet'
+ 406, "cant convert facet '#{facet.name}' to '#{facet.type}'"
elseif fileder
-- no facet given
facets = [{k.name, k.type} for k,v in pairs fileder.facets]
@@ -74,71 +74,75 @@ class Server
-- fileder not found
404, "fileder '#{path}' not found"
else
- 501, 'not implemented'
+ 501, "not implemented"
- stream: (sv, stream) =>
- req = stream\get_headers!
- method = req\get ':method'
- path = req\get ':path'
-
- if path\match '^/%?'
- -- serve static assets, cheap hack for now
+ handle_static: (method, path, stream) =>
+ path = path\match '^/%?static/(.*)'
+ return unless path
+ respond = (code, type, body) ->
res = headers.new!
- if method ~= 'GET' and method ~= 'HEAD'
- res\append ':status', '405'
- stream\write_headers res, true
- return
+ res\upsert ':status', code
+ res\append 'content-type', type
- static_path = "static/#{path\sub 3}"
+ assert stream\write_headers res, method == 'HEAD'
+ if body and method ~= 'HEAD'
+ assert stream\write_body_from_string body
- if 'file' == lfs.attributes static_path, 'mode'
- fd, err, errno = io.open static_path, 'rb'
+ if method ~= 'GET' and method ~= 'HEAD'
+ respond '405', 'text/plain', "can only GET/HEAD static resources"
+ return true
- if not fd
- code = switch errno
- when ce.ENOENT then '404'
- when ce.EACCES then '403'
- else '503'
+ if path\match '^%.' or path\match '^%~'
+ respond '404', 'text/plain', "not found"
+ return
- res\upsert ':status', code
- res\append 'content-type', 'text/plain'
+ file_path = "static/#{path}"
+ if 'file' == lfs.attributes file_path, 'mode'
+ fd, err, errno = io.open file_path, 'rb'
- assert stream\write_headers res, method == 'HEAD'
- if method ~= 'HEAD'
- assert stream\write_body_from_string err
+ if not fd
+ code = switch errno
+ when ce.ENOENT then '404'
+ when ce.EACCES then '403'
+ else '503'
- 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'
+ respond code, 'text/plain', err or ''
- assert stream\write_headers res, method == 'HEAD'
+ else
+ suffix = file_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'
+
+ respond '200', type, nil
if method ~= 'HEAD'
- assert stream\write_body_from_string "not found"
+ assert stream\write_body_from_file fd
+ else
+ respond '404', 'text/plain', "not found"
- return
+ true
- path, facet = dir_base path
- facet = if #facet > 0
- facet = '' if facet == ':'
- accept = req\get 'mmm-accept'
- Key facet, accept or 'text/html'
+ stream: (sv, stream) =>
+ req = stream\get_headers!
+ method = req\get ':method'
+ path = req\get ':path'
+
+ -- serve static assets, cheap hack for now
+ return if @handle_static method, path, stream
+
+ path_facet, type = path\match '(.*):(.*)'
+ path_facet or= path
+ path, facet = path_facet\match '(.*)/([^/]*)'
+
+ if facet ~= '?index'
+ type or= 'text/html'
+ type = type\match '%s*(.*)'
+ facet = Key facet, type
else
- nil
+ facet = nil
status, body = @handle method, path, facet
@@ -149,10 +153,8 @@ class Server
res\append ':status', tostring status
res\append 'content-type', response_type
- if method == 'HEAD'
- stream\write_headers res, true
- else
- stream\write_headers res, false
+ stream\write_headers res, method == 'HEAD'
+ if method ~= 'HEAD'
stream\write_chunk body, true
error: (sv, ctx, op, err, errno) =>
diff --git a/mmm/mmmfs/layout.moon b/mmm/mmmfs/layout.moon
index 6eead6d..7312cb5 100644
--- a/mmm/mmmfs/layout.moon
+++ b/mmm/mmmfs/layout.moon
@@ -129,7 +129,7 @@ render = (content, fileder, opts={}) ->
<!DOCTYPE html>
<html>
<head>
- <link rel="stylesheet" type="text/css" href="/?main.css" />
+ <link rel="stylesheet" type="text/css" href="/?static/main.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,400" />
]]
buf ..= "
@@ -143,12 +143,12 @@ render = (content, fileder, opts={}) ->
#{footer}
"
buf ..= [[
- <script type="application/javascript" src="/?highlight.pack.js"></script>
+ <script type="application/javascript" src="/?static/highlight.pack.js"></script>
<script type="application/javascript" src="//cdnjs.cloudflare.com/ajax/libs/marked/0.5.1/marked.min.js"></script>
<script type="application/javascript" src="//cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.6/svg.min.js"></script>
<script type="application/javascript" src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
- <script type="application/javascript" src="/?fengari-web.js"></script>
- <script type="application/lua" src="/?mmm.bundle.lua"></script>
+ <script type="application/javascript" src="/?static/fengari-web.js"></script>
+ <script type="application/lua" src="/?static/mmm.bundle.lua"></script>
<script type="application/lua">require 'mmm'</script>
]]
diff --git a/mmm/mmmfs/util.moon b/mmm/mmmfs/util.moon
index db0c432..9e0a0b0 100644
--- a/mmm/mmmfs/util.moon
+++ b/mmm/mmmfs/util.moon
@@ -5,9 +5,9 @@ merge = (orig={}, extra) ->
tourl = (path) ->
if STATIC
- path
+ path .. '/'
else
- "#{path}/:"
+ path .. '/'
(elements) ->
import a, div, pre from elements