diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a6abc75 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source :rubygems + +gem "rack" +gem "rack-mount" +gem "rack-contrib" +gem "thin" +gem "activesupport" diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..d861790 --- /dev/null +++ b/app.rb @@ -0,0 +1,13 @@ +require "rack" + +class MyApp + def call(env) + [ + 200, + {"Content-Type" => "text/plain"}, + ["Hello Rackers!"] + ] + end +end + +Rack::Handler::Thin.run MyApp.new, :Port => 1234 diff --git a/builder.rb b/builder.rb new file mode 100644 index 0000000..1a2cae8 --- /dev/null +++ b/builder.rb @@ -0,0 +1,25 @@ +require "rack" + +class MyApp + def call(env) + [ + 200, + {"Content-Type" => "text/plain"}, + ["Hello Rackers!"] + ] + end +end + +app = Rack::Builder.app do + use Rack::ContentLength + + map "/codeplane" do + run MyApp.new + end + + map "/howto" do + run MyApp.new + end +end + +Rack::Handler::Thin.run app, :Port => 1234 diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..50e9cf1 --- /dev/null +++ b/config.ru @@ -0,0 +1,14 @@ +class MyApp + def call(env) + [ + 200, + {"Content-Type" => "text/plain"}, + ["Hello Rackers!"] + ] + end +end + +use Rack::ContentLength +use Rack::Lint + +run MyApp.new diff --git a/fu.rb b/fu.rb new file mode 100644 index 0000000..35a0ba6 --- /dev/null +++ b/fu.rb @@ -0,0 +1,136 @@ +require "rack" +require "rack/mount" +require "rack/contrib" +require "active_support/hash_with_indifferent_access" + +class FU + class App + attr_reader :env + attr_accessor :callback + + def initialize(callback) + @callback = callback + end + + def request + @request ||= Rack::Request.new(env) + end + + def params + @params ||= begin + params = request.params || {} + params.merge!(request.env["rack.request.form_hash"] || {}) + params.merge!(request.env["rack.routing_args"] || {}) + ActiveSupport::HashWithIndifferentAccess.new(params) + end + end + + def call(env) + @env = env + instance_eval(&callback) + end + end + + def self.app(&block) + new(&block).to_app + end + + def initialize(&block) + instance_eval(&block) + end + + def middlewares + @middlewares ||= [] + end + + def get(path, &block) + match(path, "GET", &block) + end + + def post(path, &block) + match(path, "POST", &block) + end + + def put(path, &block) + match(path, "PUT", &block) + end + + def delete(path, &block) + match(path, "DELETE", &block) + end + + def head(path, &block) + match(path, "HEAD", &block) + end + + def match(path, method = nil, &block) + conditions = {path_info: compile_path(path)} + conditions[:request_method] = method if method + + builder = Rack::Builder.new + builder.use Rack::ETag + builder.use Rack::ContentLength + builder.use Rack::MethodOverride + builder.use Rack::NestedParams + + middlewares.each do |middleware, *args| + builder.use middleware, *args + end + + app = App.new(block) + builder.run(app) + + route_set.add_route(builder, conditions) + end + + def use(middleware, *args) + middlewares << [middleware, args] + end + + def to_app + route_set.freeze + end + + private + def route_set + @route_set ||= Rack::Mount::RouteSet.new + end + + def compile_path(path) + Rack::Mount::Strexp.compile Rack::Mount::Utils.normalize_path(path), {}, %w[/.?] + end +end + +app = FU.app do + use Rack::Runtime + + get "/" do + name = params.fetch(:name, "Rackers") + + [ + 200, + {"Content-Type" => "text/html"}, + ["Hello #{name}!"] + ] + end + + get "/:name" do + [ + 200, + {"Content-Type" => "text/html"}, + ["Hello #{params[:name]}!"] + ] + end + + post "/" do + name = params.fetch(:name, "Rackers") + + [ + 200, + {"Content-Type" => "text/html"}, + ["Hello #{name}! Post data sent!"] + ] + end +end + +Rack::Handler::Thin.run app, :Port => 1234 diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..89271ee --- /dev/null +++ b/public/index.html @@ -0,0 +1,13 @@ + + + + + + + + + +

Hello Rackers!

+ + + diff --git a/public/script.js b/public/script.js new file mode 100644 index 0000000..32931ca --- /dev/null +++ b/public/script.js @@ -0,0 +1 @@ +document.write("

#fuckyeah!

"); \ No newline at end of file diff --git a/public/style.css b/public/style.css new file mode 100644 index 0000000..3287223 --- /dev/null +++ b/public/style.css @@ -0,0 +1,7 @@ +body { + background: blue; + color: yellow; + font-family: sans-serif; + font-size: 36px; + text-align: center; +} \ No newline at end of file diff --git a/static.rb b/static.rb new file mode 100644 index 0000000..0fe6629 --- /dev/null +++ b/static.rb @@ -0,0 +1,13 @@ +require "rack" + +app = Rack::Builder.new do + use Rack::Static, + urls: {"/" => "index.html"}, + root: "public" + + run Rack::URLMap.new({ + "/" => Rack::Directory.new("public") + }) +end + +Rack::Handler::Thin.run app, :Port => 1234