Last active
February 22, 2019 09:55
-
-
Save Integralist/8341704 to your computer and use it in GitHub Desktop.
Rack Server Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run lambda { |env| [200, {"Content-Type"=>"text/html"}, ["Hello World"]] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@root = File.expand_path(File.dirname(__FILE__)) | |
run lambda { |env| | |
path = Rack::Utils.unescape(env['PATH_INFO']) | |
index_file = @root + "#{path}index.html" | |
[200, { 'Content-Type' => 'text/html' }, [File.exists?(index_file) ? File.read(index_file) : 'Hello World']] | |
} | |
# run with: rackup config.ru |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$: << File.dirname(__FILE__) | |
module TestComponent | |
class Application | |
def initialize | |
puts "Application initialized" | |
end | |
# Rack requires object being run to have a `call` method which returns | |
# an Array that includes the status code, http headers and a content response | |
def call(env) | |
[200, { 'Content-Type' => 'text/html' }, ['Hello World!']] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$: << File.dirname(__FILE__) | |
require 'app/app' | |
run TestComponent::Application.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run it with
rackup
and the followingconfig.ru
: