Class: Staticz::Server

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Server

Returns a new instance of Server.



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/server.rb', line 11

def initialize(port)
  @port = port

  Thin::Logging.silent = true

  app = Rack::Builder.new do
    map "/reload.js" do
      run lambda { |env|
        [
          200,
          {"Content-Type" => "text/plain"},
          Staticz::Modules::Reload.build_reload_js
        ]
      }
    end
    map "/api/hash" do
      run lambda { |env|
        [
          200,
          {"Content-Type" => "text/plain"},
          Staticz::Modules::Reload.hash
        ]
      }
    end

    map "/" do
      if Staticz::Manifest.instance.index_missing?
        use Rack::Static,
          urls: {"/" => "get_started.html"},
          root: File.join(File.dirname(__FILE__), "templates", "files")

        use Rack::Static,
          urls: {"/logo.png" => "logo.png"},
          root: File.join(File.dirname(__FILE__), "templates", "files")

        use Rack::Static,
          urls: {"/bolt.png" => "bolt.png"},
          root: File.join(File.dirname(__FILE__), "templates", "files")
      else
        use Rack::Static, urls: {"/" => "build/index.html"}
      end

      Staticz::Server.all_templates(Staticz::Manifest.instance.elements).each do |e|
        use Rack::Static, urls: {"/#{e.name}" => e.build_path}
      end

      run Rack::Directory.new("build")
    end
  end

  thin_server = Thin::Server.new '127.0.0.1', port
  thin_server.app = app

  build_manifest

  Thread.new { listen_to_file_changes }
  Thread.new { listen_to_manifest_changes }
  thin_server.start
end

Class Method Details

.all_templates(elements) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/server.rb', line 71

def self.all_templates(elements)
  elements.map do |e|
    if e.is_a? Staticz::Compilable::Haml
      e
    elsif e.is_a? Staticz::Compilable::Slim
      e
    elsif e.is_a? Staticz::Sub
      all_templates(e.elements)
    end
  end.flatten.select do |e|
    e
  end
end