From 53fa65f96443ae2a72622389e72407921c9283a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sun, 15 Oct 2017 22:32:11 +0200 Subject: [PATCH] Add class method API to Kemal::Base --- samples/app.cr | 9 +++++++++ spec/application_mode_spec.cr | 27 +++++++++++++++++++++++++++ src/kemal/base.cr | 15 +++++++++++++++ src/kemal/base/dsl.cr | 20 +++++++++++--------- 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 samples/app.cr create mode 100644 spec/application_mode_spec.cr diff --git a/samples/app.cr b/samples/app.cr new file mode 100644 index 0000000..2ce65ab --- /dev/null +++ b/samples/app.cr @@ -0,0 +1,9 @@ +require "kemal/base" + +class MyApp < Kemal::Application + get "/" do + "Hello Kemal!" + end +end + +MyApp.run diff --git a/spec/application_mode_spec.cr b/spec/application_mode_spec.cr new file mode 100644 index 0000000..49e1f68 --- /dev/null +++ b/spec/application_mode_spec.cr @@ -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 diff --git a/src/kemal/base.cr b/src/kemal/base.cr index 649d6c1..b6eadd2 100644 --- a/src/kemal/base.cr +++ b/src/kemal/base.cr @@ -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? diff --git a/src/kemal/base/dsl.cr b/src/kemal/base/dsl.cr index a7473ac..fe2cc91 100644 --- a/src/kemal/base/dsl.cr +++ b/src/kemal/base/dsl.cr @@ -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 %}