Class: StaticMatic::Server

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(staticmatic, default = nil) ⇒ Server

Returns a new instance of Server.



3
4
5
6
# File 'lib/staticmatic/server.rb', line 3

def initialize(staticmatic, default = nil)
  @files = default || Rack::File.new(staticmatic.src_dir)
  @staticmatic = staticmatic
end

Class Method Details

.start(staticmatic) ⇒ Object

Starts the StaticMatic preview server



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/staticmatic/server.rb', line 46

def self.start(staticmatic)
  [ 'INT', 'TERM' ].each do |signal|
    Signal.trap(signal) do
      puts 
      puts "Exiting"
      exit!(0)
    end
  end
  port = staticmatic.configuration.preview_server_port || 3000

  host = staticmatic.configuration.preview_server_host || ""

  app = Rack::Builder.new do
    use Rack::ShowExceptions
    run StaticMatic::Server.new(staticmatic)
  end 

  ssl_enable = staticmatic.configuration.ssl_enable || false

  if not ssl_enable
    staticmatic.configuration.preview_server.run(app, :Port => port, :Host => host)
  else
    ssl_verify_client = OpenSSL::SSL::VERIFY_NONE
    ssl_private_key = OpenSSL::PKey::RSA.new(File.open(staticmatic.configuration.ssl_private_key_path).read)
    ssl_certificate = OpenSSL::X509::Certificate.new(File.open(staticmatic.configuration.ssl_certificate_path).read)
    ssl_cert_name = [["CN", WEBrick::Utils::getservername]]
    staticmatic.configuration.preview_server.run(
      app, 
      :Port => port, 
      :Host => host,
      :SSLEnable => true,
      :SSLPrivateKey => ssl_private_key,
      :SSLCertificate => ssl_certificate,
      :SSLCertName => ssl_cert_name
    )

  end
  
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/staticmatic/server.rb', line 8

def call(env)
  @staticmatic.load_helpers
  path_info = env["PATH_INFO"]

  file_dir, file_name, file_ext = @staticmatic.expand_path(path_info)
  
  file_dir = CGI::unescape(file_dir)
  file_name = CGI::unescape(file_name)

  unless file_ext && ["html", "css", "js"].include?(file_ext) &&
      File.basename(file_name) !~ /^\_/ &&
      (template_path = @staticmatic.determine_template_path file_name, file_ext, file_dir)
    return @files.call(env)
  end

  res = Rack::Response.new

  begin
    @staticmatic.clear_template_variables!

    if file_ext == "css"
      res.header["Content-Type"] = "text/css"
      res.write @staticmatic.render_template(template_path)
    elsif file_ext == "js"
      res.header["Content-Type"] = "text/javascript"
      res.write @staticmatic.render_template(template_path)
    else
      res.header["Content-Type"] = "text/html"
      res.write @staticmatic.render_template_with_layout(template_path)
    end
  rescue StaticMatic::Error => e
    res.write e.message
  end

  res.finish
end