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 Base::DSL
|
||||
include Base::Builder
|
||||
extend Base::ClassDSL
|
||||
|
||||
# :nodoc:
|
||||
getter route_handler = Kemal::RouteHandler.new
|
||||
|
@ -58,6 +59,20 @@ class Kemal::Base
|
|||
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?`
|
||||
def running
|
||||
running?
|
||||
|
|
|
@ -5,13 +5,13 @@ class Kemal::Base
|
|||
|
||||
macro included
|
||||
# :nodoc:
|
||||
DEFAULT_HANDLERS = [] of {String, String, (HTTP::Server::Context -> Nil)}
|
||||
DEFAULT_HANDLERS = [] of {String, String, (HTTP::Server::Context -> String)}
|
||||
# :nodoc:
|
||||
WEBSOCKET_HANDLERS = [] of {String, (HTTP::WebSocket, HTTP::Server::Context -> Void)}
|
||||
# :nodoc:
|
||||
DEFAULT_ERROR_HANDLERS = [] of {Int32, (HTTP::Server::Context, Exception -> Nil)}
|
||||
DEFAULT_ERROR_HANDLERS = [] of {Int32, (HTTP::Server::Context, Exception -> String)}
|
||||
# :nodoc:
|
||||
DEFAULT_FILTERS = [] of {Symbol, String, String, (HTTP::Server::Context -> Nil)}
|
||||
DEFAULT_FILTERS = [] of {Symbol, String, String, (HTTP::Server::Context -> String)}
|
||||
end
|
||||
|
||||
{% for method in HTTP_METHODS %}
|
||||
|
@ -62,18 +62,20 @@ class Kemal::Base
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
{% for method in HTTP_METHODS %}
|
||||
def self.{{method.id}}(path, &block : HTTP::Server::Context -> _)
|
||||
module ClassDSL
|
||||
{% for method in DSL::HTTP_METHODS %}
|
||||
def {{method.id}}(path, &block : HTTP::Server::Context -> _)
|
||||
DEFAULT_HANDLERS << { {{method}}, path, block }
|
||||
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}
|
||||
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}
|
||||
end
|
||||
|
||||
|
@ -81,8 +83,8 @@ class Kemal::Base
|
|||
# - 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
|
||||
{% for type in [:before, :after] %}
|
||||
{% for method in FILTER_METHODS %}
|
||||
def self.{{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> _)
|
||||
{% for method in DSL::FILTER_METHODS %}
|
||||
def {{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> _)
|
||||
DEFAULT_FILTERS << { {{type}}, {{method}}, path, block }
|
||||
end
|
||||
{% end %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue