Class: MagLove::Server
- Inherits:
-
Object
- Object
- MagLove::Server
- Defined in:
- lib/maglove/server.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(options) ⇒ Server
constructor
A new instance of Server.
- #process(template) ⇒ Object
- #setup(theme) ⇒ Object
Constructor Details
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
6 7 8 |
# File 'lib/maglove/server.rb', line 6 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/maglove/server.rb', line 6 def @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" = Workspace.dir(Dir.pwd).file("node_modules/maglove-widgets/dist/widgets.js") if .exists? [200, { 'Content-Type' => 'text/javascript' }, [.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/#{[:theme]}").file("theme.css").read js_contents = Workspace::Dir.new(Dir.pwd, "dist/themes/#{[: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 |