Renamed all occurrences to Kemal

This commit is contained in:
Sdogruyol 2015-10-23 21:33:26 +03:00
parent 289ad42d4e
commit 5ca84fea4c
13 changed files with 38 additions and 38 deletions

View File

@ -1,4 +1,4 @@
# frank # Kemal
This is a proof-of-concept [Sinatra](http://www.sinatrarb.com/) clone for [Crystal](http://www.crystal-lang.org). This is a proof-of-concept [Sinatra](http://www.sinatrarb.com/) clone for [Crystal](http://www.crystal-lang.org).

View File

@ -1,4 +1,4 @@
require "../src/frank" require "../libs/kemal"
get "/" do get "/" do
"Hello World!" "Hello World!"

View File

@ -1,43 +1,43 @@
require "./spec_helper" require "./spec_helper"
describe "Frank::Handler" do describe "Kemal::Handler" do
it "routes" do it "routes" do
frank = Frank::Handler.new kemal = Kemal::Handler.new
frank.add_route "GET", "/" do kemal.add_route "GET", "/" do
"hello" "hello"
end end
request = HTTP::Request.new("GET", "/") request = HTTP::Request.new("GET", "/")
response = frank.call(request) response = kemal.call(request)
response.body.should eq("hello") response.body.should eq("hello")
end end
it "routes request with query string" do it "routes request with query string" do
frank = Frank::Handler.new kemal = Kemal::Handler.new
frank.add_route "GET", "/" do |ctx| kemal.add_route "GET", "/" do |ctx|
"hello #{ctx.params["message"]}" "hello #{ctx.params["message"]}"
end end
request = HTTP::Request.new("GET", "/?message=world") request = HTTP::Request.new("GET", "/?message=world")
response = frank.call(request) response = kemal.call(request)
response.body.should eq("hello world") response.body.should eq("hello world")
end end
it "route parameter has more precedence than query string arguments" do it "route parameter has more precedence than query string arguments" do
frank = Frank::Handler.new kemal = Kemal::Handler.new
frank.add_route "GET", "/:message" do |ctx| kemal.add_route "GET", "/:message" do |ctx|
"hello #{ctx.params["message"]}" "hello #{ctx.params["message"]}"
end end
request = HTTP::Request.new("GET", "/world?message=coco") request = HTTP::Request.new("GET", "/world?message=coco")
response = frank.call(request) response = kemal.call(request)
response.body.should eq("hello world") response.body.should eq("hello world")
end end
it "sets content type" do it "sets content type" do
frank = Frank::Handler.new kemal = Kemal::Handler.new
frank.add_route "GET", "/" do |env| kemal.add_route "GET", "/" do |env|
env.response.content_type = "application/json" env.response.content_type = "application/json"
end end
request = HTTP::Request.new("GET", "/") request = HTTP::Request.new("GET", "/")
response = frank.call(request) response = kemal.call(request)
response.headers["Content-Type"].should eq("application/json") response.headers["Content-Type"].should eq("application/json")
end end
end end

View File

@ -1,4 +1,4 @@
require "spec" require "spec"
require "../src/frank/*" require "../src/kemal/*"
include Frank include Kemal

View File

@ -1,7 +0,0 @@
def get(path, &block : Frank::Context -> _)
Frank::Handler::INSTANCE.add_route("GET", path, &block)
end
def post(path, &block : Frank::Context -> _)
Frank::Handler::INSTANCE.add_route("POST", path, &block)
end

View File

@ -1,17 +1,17 @@
require "option_parser" require "option_parser"
require "./frank/*" require "./Kemal/*"
at_exit do at_exit do
OptionParser.parse! do |opts| OptionParser.parse! do |opts|
opts.on("-p ", "--port ", "port") do |opt_port| opts.on("-p ", "--port ", "port") do |opt_port|
Frank.config.port = opt_port.to_i Kemal.config.port = opt_port.to_i
end end
end end
config = Frank.config config = Kemal.config
handlers = [] of HTTP::Handler handlers = [] of HTTP::Handler
handlers << HTTP::LogHandler.new handlers << HTTP::LogHandler.new
handlers << Frank::Handler::INSTANCE handlers << Kemal::Handler::INSTANCE
handlers << HTTP::StaticFileHandler.new("./public") handlers << HTTP::StaticFileHandler.new("./public")
server = HTTP::Server.new(config.port, handlers) server = HTTP::Server.new(config.port, handlers)

View File

@ -1,4 +1,4 @@
module Frank module Kemal
class Config class Config
INSTANCE = Config.new INSTANCE = Config.new
property ssl property ssl

View File

@ -1,4 +1,4 @@
class Frank::Context class Kemal::Context
getter request getter request
def initialize(@request) def initialize(@request)

7
src/kemal/dsl.cr Normal file
View File

@ -0,0 +1,7 @@
def get(path, &block : Kemal::Context -> _)
Kemal::Handler::INSTANCE.add_route("GET", path, &block)
end
def post(path, &block : Kemal::Context -> _)
Kemal::Handler::INSTANCE.add_route("POST", path, &block)
end

View File

@ -1,7 +1,7 @@
require "http/server" require "http/server"
require "uri" require "uri"
class Frank::Handler < HTTP::Handler class Kemal::Handler < HTTP::Handler
INSTANCE = new INSTANCE = new
def initialize def initialize
@ -13,7 +13,7 @@ class Frank::Handler < HTTP::Handler
response || call_next(request) response || call_next(request)
end end
def add_route(method, path, &handler : Frank::Context -> _) def add_route(method, path, &handler : Kemal::Context -> _)
@routes << Route.new(method, path, &handler) @routes << Route.new(method, path, &handler)
end end
@ -30,8 +30,8 @@ class Frank::Handler < HTTP::Handler
params[key] ||= value params[key] ||= value
end end
frank_request = Request.new(request, params) kemal_request = Request.new(request, params)
context = Context.new(frank_request) context = Context.new(kemal_request)
begin begin
body = route.handler.call(context).to_s body = route.handler.call(context).to_s
content_type = context.response?.try(&.content_type) || "text/plain" content_type = context.response?.try(&.content_type) || "text/plain"

View File

@ -1,4 +1,4 @@
class Frank::Request class Kemal::Request
getter params getter params
def initialize(@request, @params) def initialize(@request, @params)

View File

@ -1,3 +1,3 @@
class Frank::Response class Kemal::Response
property content_type property content_type
end end

View File

@ -1,7 +1,7 @@
class Frank::Route class Kemal::Route
getter handler getter handler
def initialize(@method, path, &@handler : Frank::Context -> _) def initialize(@method, path, &@handler : Kemal::Context -> _)
@components = path.split "/" @components = path.split "/"
end end