diff options
| author | s-ol <s+removethis@s-ol.nu> | 2023-11-23 15:45:26 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2023-11-23 16:13:50 +0000 |
| commit | 8262d3be33d0b38bc9166855c8b7fe833bd84f39 (patch) | |
| tree | 4721600303a5be8b106eba819b556268b6c3ae68 | |
| parent | sort children using _sort facet (diff) | |
| download | mmm-8262d3be33d0b38bc9166855c8b7fe833bd84f39.tar.gz mmm-8262d3be33d0b38bc9166855c8b7fe833bd84f39.zip | |
add http-cache option
| -rw-r--r-- | Dockerfile | 2 | ||||
| -rw-r--r-- | build/server.moon | 39 |
2 files changed, 35 insertions, 6 deletions
@@ -18,4 +18,4 @@ RUN mkdir -p root/static/mmm && \ EXPOSE 8000 ENTRYPOINT ["moon", "build/server.moon"] -CMD ["fs", "0.0.0.0", "8000", "--cache", "--no-rw"] +CMD ["fs", "0.0.0.0", "8000", "--cache", "--http-cache", "--no-rw"] diff --git a/build/server.moon b/build/server.moon index f7bb2d8..6679c83 100644 --- a/build/server.moon +++ b/build/server.moon @@ -40,6 +40,12 @@ class Server @root = Fileder @store init_cache! + if @flags['http-cache'] + root = @root or Fileder @store + @revision = root\get 'revision: git/hash' + @revision or= os.date! + @revision = @revision\gsub '%s+$', '' + -- @TODO: fix UNSAFE! UNSAFE = @flags.unsafe @@ -116,6 +122,17 @@ class Server else 406, "cant convert facet '#{facet.name}' to '#{facet.type}'" + get_etag: (path, facet) => + return unless @revision + + facet = tostring facet + + path = path\gsub '"', '_' + facet = facet\gsub '"', '_' + + etag = "#{@revision}:#{path}: #{facet}" + string.format '%q', etag + err_and_trace = (msg) -> debug.traceback msg, 2 stream: (sv, stream) => req = stream\get_headers! @@ -134,6 +151,18 @@ class Server typ = typ\match '%s*(.*)' Key facet, typ + res = headers.new! + etag = @get_etag path, facet + if etag + res\append 'etag', etag + res\append 'cache-control', "public, max-age=#{365*24*60*60}, immutable" + + if etag == req\get 'if-none-match' + res = headers.new! + res\append ':status', tostring 304 + stream\write_headers res, true + return + value = stream\get_body_as_string! ok, status, body = xpcall @.handle, err_and_trace, @, method, path, facet, value if not ok @@ -141,10 +170,9 @@ class Server body = "Internal Server Error:\n#{status}" status = 500 - res = headers.new! response_type = if status > 299 then 'text/plain' - else if facet and facet.type == 'text/html+interactive' then 'text/html' - else if facet then facet.type + elseif facet and facet.type == 'text/html+interactive' then 'text/html' + elseif facet then facet.type else 'text/plain' res\append ':status', tostring status res\append 'content-type', response_type @@ -156,8 +184,9 @@ class Server -- usage: -- moon server.moon [FLAGS] [STORE] [host] [port] -- * FLAGS - any of the following: --- --[no-]unsafe - enable/disable server-side code execution when writable is on (default: on if local) --- --[no-]cache - cache all fileder contents (default: off) +-- --[no-]unsafe - enable/disable server-side code execution when writable is on (default: on if local) +-- --[no-]cache - cache all fileder contents (default: off) +-- --[no-]http-cache - use ETag and Last-Modified headers (default: off) -- * STORE - see mmm/mmmfs/stores/init.moon:get_store -- * host - interface to bind to (default localhost, set to 0.0.0.0 for public hosting) -- * port - port to serve from, default 8000 |
