Class: Ringleader::Server
- Inherits:
-
Reel::Server
- Object
- Reel::Server
- Ringleader::Server
- Includes:
- Celluloid::Logger
- Defined in:
- lib/ringleader/server.rb
Constant Summary collapse
- ASSET_PATH =
Pathname.new(File.("../../../assets", __FILE__))
- ACTIONS =
%w(enable disable stop start restart).freeze
Instance Method Summary collapse
- #app_as_json(app) ⇒ Object
- #app_index(request) ⇒ Object
- #content_type_for(extname) ⇒ Object
-
#initialize(controller, host, port) ⇒ Server
constructor
A new instance of Server.
- #on_connection(connection) ⇒ Object
-
#route(request) ⇒ Object
thanks to dcell explorer for this code.
- #show_app(uri, request) ⇒ Object
- #static_file(path, request) ⇒ Object
- #update_app(uri, request) ⇒ Object
Constructor Details
#initialize(controller, host, port) ⇒ Server
Returns a new instance of Server.
8 9 10 11 12 13 |
# File 'lib/ringleader/server.rb', line 8 def initialize(controller, host, port) debug "starting webserver on #{host}:#{port}" super host, port, &method(:on_connection) @controller = controller info "web control panel started on http://#{host}:#{port}" end |
Instance Method Details
#app_as_json(app) ⇒ Object
95 96 97 |
# File 'lib/ringleader/server.rb', line 95 def app_as_json(app) AppSerializer.new(app) end |
#app_index(request) ⇒ Object
50 51 52 53 54 |
# File 'lib/ringleader/server.rb', line 50 def app_index(request) json = @controller.apps.map { |app| app_as_json(app) }.to_json request.respond :ok, json debug "GET /apps: 200" end |
#content_type_for(extname) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ringleader/server.rb', line 99 def content_type_for(extname) case extname when ".html" "text/html" when ".js" "application/json" when ".css" "text/css" when ".ico" "image/x-icon" when ".png" "image/png" else "text/plain" end end |
#on_connection(connection) ⇒ Object
15 16 17 18 |
# File 'lib/ringleader/server.rb', line 15 def on_connection(connection) request = connection.request route request if request end |
#route(request) ⇒ Object
thanks to dcell explorer for this code
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 |
# File 'lib/ringleader/server.rb', line 21 def route(request) if request.url == "/" path = "index.html" else path = request.url[%r{^/([a-z0-9\.\-_]+(/[a-z0-9\.\-_]+)*)$}, 1] end if !path or path[".."] request.respond :not_found, "Not found" debug "404 #{path}" return end case request.method when "GET" if path == "apps" app_index request elsif path =~ %r(^apps/\w+) show_app path, request else static_file path, request end when "POST" update_app path, request else error "unknown #{request.method} request to #{request.url}" end end |
#show_app(uri, request) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ringleader/server.rb', line 70 def show_app(uri, request) _, name, _ = uri.split("/") app = @controller.app name if app request.respond :ok, app_as_json(app).to_json debug "GET #{uri}: 200" else request.respond :not_found, "Not found" debug "GET #{uri}: 404" end end |
#static_file(path, request) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ringleader/server.rb', line 56 def static_file(path, request) filename = ASSET_PATH + path if filename.exist? mime_type = content_type_for filename.extname filename.open("r") do |file| request.respond :ok, {"Content-type" => mime_type}, file end debug "GET #{path}: 200" else request.respond :not_found, "Not found" debug "GET #{path}: 404" end end |
#update_app(uri, request) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ringleader/server.rb', line 82 def update_app(uri, request) _, name, action = uri.split("/") app = @controller.app name if app && ACTIONS.include?(action) app.send action request.respond :ok, app_as_json(app).to_json debug "POST #{uri}: 200" else request.respond :not_found, "Not found" debug "POST #{uri}: 404" end end |