Class: Usmu::Ui::RackServer
- Inherits:
-
Object
- Object
- Usmu::Ui::RackServer
- Defined in:
- lib/usmu/ui/rack_server.rb
Instance Method Summary collapse
- #call(env) ⇒ void
-
#initialize(configuration) ⇒ RackServer
constructor
A new instance of RackServer.
- #shutdown ⇒ void
Constructor Details
#initialize(configuration) ⇒ RackServer
Returns a new instance of RackServer.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/usmu/ui/rack_server.rb', line 7 def initialize(configuration) @log = Logging.logger[self] @log.debug 'Loading environment variables.' ::Dotenv.load @configuration = configuration @index = configuration['serve', 'index', default: 'index.html'] config_filename = File.basename(@configuration.config_file) config_listen = ::Listen.to(@configuration.config_dir, only: %r{#{::Regexp.escape config_filename}$}) do |modified, added, removed| @log.info 'Usmu configuration changed, updating...' @configuration = Usmu::Configuration.from_file(@configuration.config_file) end config_listen.start end |
Instance Method Details
#call(env) ⇒ void
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/usmu/ui/rack_server.rb', line 22 def call(env) generator = @configuration.generator path = env['PATH_INFO'][1...env['PATH_INFO'].length] @log.info "Received a request for: #{path}" path = File.join(path, @index) if File.directory? File.join(@configuration.source_path, path) path = path[1...path.length] if path[0] == '/' @log.info "Serving: #{path}" valid = generator.renderables.select {|r| r.output_filename == path } if valid.length > 0 generator.refresh page = valid[0] type = case page.output_filename[page.output_filename.rindex('.')...page.output_filename.length] when '.html', '.php' 'text/html' when '.css' 'text/css' when '.js' 'text/javascript' else 'text/plain' end ['200', {'Content-Type' => type}, [page.render]] else ['404', {'Content-Type' => 'text/html'}, ['<!DOCTYPE html><h1>File not found.</h1>']] end end |
#shutdown ⇒ void
53 54 |
# File 'lib/usmu/ui/rack_server.rb', line 53 def shutdown end |