Class: Resulang::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/resulang/server.rb

Class Method Summary collapse

Class Method Details

.appObject



19
20
21
# File 'lib/resulang/server.rb', line 19

def self.app
  Resulang::App.new(path: File.expand_path('.'))
end

.call(env) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/resulang/server.rb', line 44

def self.call(env)
  if env['PATH_INFO'] == '/'
    serve_html
  else
    serve_file(env['PATH_INFO'])
  end
end

.escape(html) ⇒ Object



7
8
9
# File 'lib/resulang/server.rb', line 7

def self.escape(html)
  html.gsub(/\</, '&lt;').gsub(/\>/, '&gt;').gsub(/\"/, '&quot;')
end

.mime_type(filename) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/resulang/server.rb', line 11

def self.mime_type(filename)
  if (types = MIME::Types.type_for(filename)).empty?
    'text/plain'
  else
    types.first.content_type
  end
end

.serve_file(path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/resulang/server.rb', line 31

def self.serve_file(path)
  fullpath = File.expand_path("./#{path}")
  if File.file?(fullpath)
    data = File.read(fullpath)
    headers = {
      'Content-Type' => mime_type(File.basename(path))
    }
    [200, headers, [data]]
  else
    [404, { 'Content-Type' => 'text/html' }, ['Not Found']]
  end
end

.serve_htmlObject



23
24
25
26
27
28
29
# File 'lib/resulang/server.rb', line 23

def self.serve_html
  html = app.processor(output: nil, format: :html).process
  headers = {
    'Content-Type'   => 'text/html'
  }
  [200, headers, [html]]
end