Class: MagLove::Server

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Server

Returns a new instance of Server.



18
19
20
21
22
23
24
# File 'lib/maglove/server.rb', line 18

def initialize(options)
  @options = options
  Maglove::Engine.configure do |config|
    config.asset_uri = "."
    config.block_path = "src/themes/#{options[:theme]}/blocks"
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/maglove/server.rb', line 6

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/maglove/server.rb', line 6

def options
  @options
end

Class Method Details

.start(port, theme, templates) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/maglove/server.rb', line 8

def self.start(port, theme, templates)
  app = Rack::Builder.new do
    use MagLove::Middleware::LiveReload, mount: "/maglove", theme: theme, templates: templates
    use Rack::Static, urls: ["/fonts", "/themes"], root: "dist"
    use Rack::Static, urls: ["/images"], root: "dist/themes/#{theme}"
    run MagLove::Server.new(theme: theme, templates: templates, port: port)
  end
  Rack::Server.start(app: app, Port: port, server: :puma)
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/maglove/server.rb', line 26

def call(env)
  template = env["PATH_INFO"].sub("/", "")
  if env["PATH_INFO"] == "/maglove.js"
    [200, { 'Content-Type' => 'text/javascript' }, [Maglove.assets_dir.file("maglove.js").read]]
  elsif env["PATH_INFO"] == "/maglove-widgets.js"
    widgets_file = Workspace.dir(Dir.pwd).file("node_modules/maglove-widgets/dist/widgets.js")
    if widgets_file.exists?
      [200, { 'Content-Type' => 'text/javascript' }, [widgets_file.read]]
    else
      [200, { 'Content-Type' => 'text/javascript' }, ["export default { ready: function() { alert('Module \\\"maglove-widgets\\\" not installed. Run \\\"npm install maglove-widgets\\\" to install.'); return this; }, then: function() {} }"]]
    end
  elsif env["PATH_INFO"] == "/maglove.css"
    [200, { 'Content-Type' => 'text/css' }, [Maglove.assets_dir.file("maglove.css").read]]
  elsif @options[:templates].include?(template)
    [200, { 'Content-Type' => 'text/html' }, [process(template)]]
  else
    [200, { 'Content-Type' => 'text/html' }, [Maglove::Engine.render(Maglove.assets_dir.file("index.haml").read, @options)]]
  end
end

#process(template) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/maglove/server.rb', line 46

def process(template)
  css_contents = Workspace::Dir.new(Dir.pwd, "dist/themes/#{options[:theme]}").file("theme.css").read
  js_contents = Workspace::Dir.new(Dir.pwd, "dist/themes/#{options[:theme]}").file("theme.js").read
  template_asset = MagLove::Asset::Theme.new("templates/#{template}.haml", { base: false })
  Maglove::Engine.render(Maglove.assets_dir.file("maglove.haml").read, {
    theme: @options[:theme],
    contents: template_asset.contents,
    css_contents: css_contents,
    js_contents: js_contents,
    template: template,
    port: @options[:port] || 3000
  })
end

#setup(theme) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/maglove/server.rb', line 60

def setup(theme)
  @widget_stamps = {}
  Dir["widgets/*.rb"].each { |file| @widget_stamps[file] = File.mtime(file).to_i }
  @options[:templates].each do |template|
    mount("/#{template}") do |req, res|
      Dir["widgets/*.rb"].each do |file|
        stamp = File.mtime(file).to_i
        next unless @widget_stamps[file] and @widget_stamps[file] < stamp
        @widget_stamps[file] = stamp
        debug("▸ reloading widget: #{file}")
        load(file)
      end
    end
  end
end