mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Add class method API to Kemal::Base
This commit is contained in:
parent
2e42b3f48c
commit
53fa65f964
4 changed files with 62 additions and 9 deletions
9
samples/app.cr
Normal file
9
samples/app.cr
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
require "kemal/base"
|
||||||
|
|
||||||
|
class MyApp < Kemal::Application
|
||||||
|
get "/" do
|
||||||
|
"Hello Kemal!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
MyApp.run
|
27
spec/application_mode_spec.cr
Normal file
27
spec/application_mode_spec.cr
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
require "./spec_helper"
|
||||||
|
|
||||||
|
private class MyApp < Kemal::Application
|
||||||
|
get "/route1" do |env|
|
||||||
|
"Route 1"
|
||||||
|
end
|
||||||
|
|
||||||
|
get "/route2" do |env|
|
||||||
|
"Route 2"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe MyApp do
|
||||||
|
it "matches the correct route" do
|
||||||
|
request = HTTP::Request.new("GET", "/route2")
|
||||||
|
client_response = call_request_on_app(MyApp.new, request)
|
||||||
|
client_response.body.should eq("Route 2")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't allow a route declaration start without /" do
|
||||||
|
expect_raises Kemal::Exceptions::InvalidPathStartException, "Route declaration get \"route\" needs to start with '/', should be get \"/route\"" do
|
||||||
|
MyApp.new.get "route" do |env|
|
||||||
|
"Route 1"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -13,6 +13,7 @@ class Kemal::Base
|
||||||
include Macros
|
include Macros
|
||||||
include Base::DSL
|
include Base::DSL
|
||||||
include Base::Builder
|
include Base::Builder
|
||||||
|
extend Base::ClassDSL
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
getter route_handler = Kemal::RouteHandler.new
|
getter route_handler = Kemal::RouteHandler.new
|
||||||
|
@ -58,6 +59,20 @@ class Kemal::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.run(port : Int32? = nil)
|
||||||
|
new.tap do |app|
|
||||||
|
Kemal::CLI.new(app.config)
|
||||||
|
|
||||||
|
app.run(port) do
|
||||||
|
yield app
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.run(port : Int32? = nil)
|
||||||
|
run(port) { }
|
||||||
|
end
|
||||||
|
|
||||||
# DEPRECATED: This method should be replaced with `#running?`
|
# DEPRECATED: This method should be replaced with `#running?`
|
||||||
def running
|
def running
|
||||||
running?
|
running?
|
||||||
|
|
|
@ -5,13 +5,13 @@ class Kemal::Base
|
||||||
|
|
||||||
macro included
|
macro included
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
DEFAULT_HANDLERS = [] of {String, String, (HTTP::Server::Context -> Nil)}
|
DEFAULT_HANDLERS = [] of {String, String, (HTTP::Server::Context -> String)}
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
WEBSOCKET_HANDLERS = [] of {String, (HTTP::WebSocket, HTTP::Server::Context -> Void)}
|
WEBSOCKET_HANDLERS = [] of {String, (HTTP::WebSocket, HTTP::Server::Context -> Void)}
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
DEFAULT_ERROR_HANDLERS = [] of {Int32, (HTTP::Server::Context, Exception -> Nil)}
|
DEFAULT_ERROR_HANDLERS = [] of {Int32, (HTTP::Server::Context, Exception -> String)}
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
DEFAULT_FILTERS = [] of {Symbol, String, String, (HTTP::Server::Context -> Nil)}
|
DEFAULT_FILTERS = [] of {Symbol, String, String, (HTTP::Server::Context -> String)}
|
||||||
end
|
end
|
||||||
|
|
||||||
{% for method in HTTP_METHODS %}
|
{% for method in HTTP_METHODS %}
|
||||||
|
@ -62,18 +62,20 @@ class Kemal::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
{% for method in HTTP_METHODS %}
|
module ClassDSL
|
||||||
def self.{{method.id}}(path, &block : HTTP::Server::Context -> _)
|
{% for method in DSL::HTTP_METHODS %}
|
||||||
|
def {{method.id}}(path, &block : HTTP::Server::Context -> _)
|
||||||
DEFAULT_HANDLERS << { {{method}}, path, block }
|
DEFAULT_HANDLERS << { {{method}}, path, block }
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
def self.ws(path, &block : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
def ws(path, &block : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
||||||
WEBSOCKET_HANDLERS << {path, block}
|
WEBSOCKET_HANDLERS << {path, block}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.error(status_code, &block : HTTP::Server::Context, Exception -> _)
|
def error(status_code, &block : HTTP::Server::Context, Exception -> _)
|
||||||
DEFAULT_ERROR_HANDLERS << {status_code, block}
|
DEFAULT_ERROR_HANDLERS << {status_code, block}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -81,8 +83,8 @@ class Kemal::Base
|
||||||
# - before_all, before_get, before_post, before_put, before_patch, before_delete, before_options
|
# - before_all, before_get, before_post, before_put, before_patch, before_delete, before_options
|
||||||
# - after_all, after_get, after_post, after_put, after_patch, after_delete, after_options
|
# - after_all, after_get, after_post, after_put, after_patch, after_delete, after_options
|
||||||
{% for type in [:before, :after] %}
|
{% for type in [:before, :after] %}
|
||||||
{% for method in FILTER_METHODS %}
|
{% for method in DSL::FILTER_METHODS %}
|
||||||
def self.{{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> _)
|
def {{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> _)
|
||||||
DEFAULT_FILTERS << { {{type}}, {{method}}, path, block }
|
DEFAULT_FILTERS << { {{type}}, {{method}}, path, block }
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue