git.s-ol.nu mmm / 0cc0b9d
basic caching for fileder tree and conversion results s-ol 2 months ago
4 changed file(s) with 37 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1717 RUN tup init && tup generate build-static.sh && ./build-static.sh
1818
1919 EXPOSE 8000
20 ENTRYPOINT ["moon", "build/server.moon", "fs", "0.0.0.0", "8000"]
20 ENTRYPOINT ["moon", "build/server.moon"]
21 CMD ["fs", "0.0.0.0", "8000", "--cache", "--no-rw"]
1616 import get_store from require 'mmm.mmmfs.stores'
1717 import render from require 'mmm.mmmfs.layout'
1818 import Browser from require 'mmm.mmmfs.browser'
19 import init_cache from require 'mmm.mmmfs.cache'
1920 import decodeURI from require 'http.util'
2021
2122 lfs = require 'lfs'
4041 if @flags.unsafe == nil
4142 @flags.unsafe = not @flags.rw or opts.host == 'localhost' or opts.host == '127.0.0.1'
4243
44 if @flags.cache
45 assert not @flags.rw, "--rw and --cache are incompatible"
46 @root = Fileder @store
47 init_cache!
48
4349 -- @TODO: fix UNSAFE!
4450 UNSAFE = @flags.unsafe
4551
5460 assert @server\loop!
5561
5662 handle_interactive: (fileder, facet) =>
57 root = Fileder @store
63 root = @root or Fileder @store
5864 browser = Browser root, fileder.path, facet.name
5965
6066 render browser\todom!, fileder, noview: true, scripts: "
9298
9399 switch method
94100 when 'GET', 'HEAD'
95 root = Fileder @store
101 root = @root or Fileder @store
96102 export BROWSER
97103 BROWSER = :path
98104 fileder = root\walk path
216222 -- * FLAGS - any of the following:
217223 -- --[no-]rw - enable/disable POST?PUT/DELETE operations (default: on if local)
218224 -- --[no-]unsafe - enable/disable server-side code execution when writable is on (default: on if local or --no-rw)
225 -- --[no-]cache - cache all fileder contents (default: off)
219226 -- * STORE - see mmm/mmmfs/stores/init.moon:get_store
220227 -- * host - interface to bind to (default localhost, set to 0.0.0.0 for public hosting)
221228 -- * port - port to serve from, default 8000
0 class Cache
1 new: =>
2 @cache = {}
3
4 get: (fileder, key) =>
5 key = tostring key
6 @cache[fileder.path] or= {}
7 @cache[fileder.path][key]
8
9 set: (fileder, key, val) =>
10 key = tostring key
11 @cache[fileder.path] or= {}
12 @cache[fileder.path][key] = val
13
14
15 init_cache = ->
16 export CACHE
17 CACHE = Cache!
18
19 {
20 :init_cache
21 }
275275 if val = @facets[want]
276276 return val, want
277277
278 if val = CACHE and CACHE\get @, want
279 return val, want
280
278281 -- find matching key and shortest conversion path
279282 key, conversions = @find want
280283
281284 if key
282285 value = apply_conversions @, conversions, @facets[key], key
286 CACHE\set @, want, value if CACHE
283287 value, key
284288
285289 -- like @get, throw if no value or conversion path