@@ -12,22 +12,20 @@ module Syntropy
1212 class App
1313 attr_reader :route_cache
1414
15- def initialize ( machine , src_path , mount_path , env = { } )
15+ def initialize ( machine , src_path , mount_path , opts = { } )
1616 @machine = machine
17- @src_path = src_path
17+ @src_path = File . expand_path ( src_path )
1818 @mount_path = mount_path
1919 @route_cache = { }
20- @env = env
20+ @opts = opts
2121
2222 @relative_path_re = calculate_relative_path_re ( mount_path )
23- if ( wf = env [ :watch_files ] )
24- period = wf . is_a? ( Numeric ) ? wf : 0.1
25- machine . spin do
26- Syntropy . file_watch ( @machine , src_path , period : period ) { invalidate_cache ( it ) }
27- rescue Exception => e
28- p e
29- p e . backtrace
30- end
23+ @machine . spin do
24+ # we do startup stuff asynchronously, in order to first let TP2 do its
25+ # setup tasks
26+ @machine . sleep 0.25
27+ @opts [ :logger ] &.call ( "Serving from #{ File . expand_path ( @src_path ) } " )
28+ start_file_watcher if opts [ :watch_files ]
3129 end
3230 end
3331
@@ -42,15 +40,6 @@ def find_route(path, cache: true)
4240 entry
4341 end
4442
45- def invalidate_cache ( fn )
46- invalidated_keys = [ ]
47- @route_cache . each do |k , v |
48- invalidated_keys << k if v [ :fn ] == fn
49- end
50-
51- invalidated_keys . each { @route_cache . delete ( it ) }
52- end
53-
5443 def call ( req )
5544 entry = find_route ( req . path )
5645 render_entry ( req , entry )
@@ -62,6 +51,32 @@ def call(req)
6251
6352 private
6453
54+ def start_file_watcher
55+ @opts [ :logger ] &.call ( 'Watching for module file changes...' , nil )
56+ wf = @opts [ :watch_files ]
57+ period = wf . is_a? ( Numeric ) ? wf : 0.1
58+ @machine . spin do
59+ Syntropy . file_watch ( @machine , @src_path , period : period ) do
60+ @opts [ :logger ] &.call ( "Detected changed file: #{ it } " )
61+ invalidate_cache ( it )
62+ rescue Exception => e
63+ p e
64+ p e . backtrace
65+ exit!
66+ end
67+ end
68+ end
69+
70+ def invalidate_cache ( fn )
71+ invalidated_keys = [ ]
72+ @route_cache . each do |k , v |
73+ @opts [ :logger ] &.call ( "Invalidate cache for #{ k } " , nil )
74+ invalidated_keys << k if v [ :fn ] == fn
75+ end
76+
77+ invalidated_keys . each { @route_cache . delete ( it ) }
78+ end
79+
6580 def calculate_relative_path_re ( mount_path )
6681 mount_path = '' if mount_path == '/'
6782 /^#{ mount_path } (?:\/ (.*))?$/
@@ -95,7 +110,7 @@ def calculate_route(path)
95110 end
96111
97112 def file_entry ( fn )
98- { fn : fn , kind : FILE_KINDS [ File . extname ( fn ) ] || :static }
113+ { fn : File . expand_path ( fn ) , kind : FILE_KINDS [ File . extname ( fn ) ] || :static }
99114 end
100115
101116 def find_index_entry ( dir )
@@ -138,19 +153,23 @@ def render_entry(req, entry)
138153 when :not_found
139154 req . respond ( 'Not found' , ':status' => Qeweney ::Status ::NOT_FOUND )
140155 when :static
141- entry [ :mime_type ] ||= Qeweney ::MimeTypes [ File . extname ( entry [ :fn ] ) ]
142- req . respond ( IO . read ( entry [ :fn ] ) , 'Content-Type' => entry [ :mime_type ] )
156+ respond_static ( req , entry )
143157 when :markdown
144158 body = render_markdown ( IO . read ( entry [ :fn ] ) )
145159 req . respond ( body , 'Content-Type' => 'text/html' )
146160 when :module
147- call_module ( entry , req )
161+ call_module ( req , entry )
148162 else
149- raise " Invalid entry kind"
163+ raise ' Invalid entry kind'
150164 end
151165 end
152166
153- def call_module ( entry , req )
167+ def respond_static ( req , entry )
168+ entry [ :mime_type ] ||= Qeweney ::MimeTypes [ File . extname ( entry [ :fn ] ) ]
169+ req . respond ( IO . read ( entry [ :fn ] ) , 'Content-Type' => entry [ :mime_type ] )
170+ end
171+
172+ def call_module ( req , entry )
154173 entry [ :code ] ||= load_module ( entry )
155174 if entry [ :code ] == :invalid
156175 req . respond ( nil , ':status' => Qeweney ::Status ::INTERNAL_SERVER_ERROR )
@@ -165,17 +184,13 @@ def call_module(entry, req)
165184 end
166185
167186 def load_module ( entry )
168- loader = Syntropy ::ModuleLoader . new ( @src_path , @env )
187+ loader = Syntropy ::ModuleLoader . new ( @src_path , @opts )
169188 ref = entry [ :fn ] . gsub ( %r{^#{ @src_path } \/ } , '' ) . gsub ( /\. rb$/ , '' )
170189 o = loader . load ( ref )
171190 # klass = Class.new
172191 # o = klass.instance_eval(body, entry[:fn], 1)
173192
174- if o . is_a? ( Papercraft ::HTML )
175- return wrap_template ( o )
176- else
177- return o
178- end
193+ o . is_a? ( Papercraft ::HTML ) ? wrap_template ( o ) : o
179194 end
180195
181196 def wrap_template ( templ )
0 commit comments