814 815def only_one!(list) 816 if list.size == 1 817 list[0] 818 else 819 raise DbException.new("did not find one, size=#{list.size}") 820 end 821end
18 yield MyDb.create 19end 20 21def query(*args) 22 start = Time.local 23 out = yield MyDb.create.query *args # this auto closes I think 24 puts "query #{args} took #{Time.local - start}" 25 out 26end 27 28require "json_mapping" # punt on Serializable for now
296 setter title : String 297 getter title 298end 299 300def get_url_from_url_id(env) 301 out = Url.get_only_by_id(env.params.url["url_id"]) 302 env.response.title = out.name_with_episode 303 out 304end 305 306post "/save_tag/:url_id" do |env|
387 url = get_url_from_url_id(env) 388 render "views/add_new_tag.ecr", "views/layout_nik.ecr" 389end 390 391get "/show_tag_details/:url_id" do |env| # the view "full tags" confusing page... 392 url = get_url_from_url_id(env) 393 show_tag_details = env.params.query["show_tag_details"]? 394 env.response.title = url.name_with_episode + " Edited" 395 render "views/show_tag_details.ecr", "views/layout_nik.ecr" 396end 397
261 # add = ->(x : Int32, y : Int32) { x + y } 262 # add.call(1, 2) # => 3 263 # ``` 264 @[Primitive(:proc_call)] 265 @[Raises] 266 def call(*args : *T) : R 267 end 268end 269 270# All `Number` methods are defined on concrete structs (for example `Int32`, `UInt8`, etc.), 271# never on `Number`, `Int` or `Float` because we don't want to handle a primitive for
261 # add = ->(x : Int32, y : Int32) { x + y } 262 # add.call(1, 2) # => 3 263 # ``` 264 @[Primitive(:proc_call)] 265 @[Raises] 266 def call(*args : *T) : R 267 end 268end 269 270# All `Number` methods are defined on concrete structs (for example `Int32`, `UInt8`, etc.), 271# never on `Number`, `Int` or `Float` because we don't want to handle a primitive for
12 @routes = Radix::Tree(Route).new 13 @cached_routes = Hash(String, Radix::Result(Route)).new 14 end 15 16 def call(context : HTTP::Server::Context) 17 process_request(context) 18 end 19 20 # Adds a given route to routing tree. As an exception each `GET` route additionaly defines 21 # a corresponding `HEAD` route. 22 def add_route(method : String, path : String, &handler : HTTP::Server::Context -> _)
23 24 abstract def call(context : HTTP::Server::Context) 25 26 def call_next(context : HTTP::Server::Context) 27 if next_handler = @next 28 next_handler.call(context) 29 else 30 context.response.respond_with_status(:not_found) 31 end 32 end 33
8 def initialize 9 @routes = Radix::Tree(WebSocket).new 10 end 11 12 def call(context : HTTP::Server::Context) 13 return call_next(context) unless context.ws_route_found? && websocket_upgrade_request?(context) 14 context.websocket.call(context) 15 end 16 17 def lookup_ws_route(path : String) 18 @routes.find "/ws" + path
23 24 abstract def call(context : HTTP::Server::Context) 25 26 def call_next(context : HTTP::Server::Context) 27 if next_handler = @next 28 next_handler.call(context) 29 else 30 context.response.respond_with_status(:not_found) 31 end 32 end 33
17 call_block_for_path_type("ALL", context.request.path, :before, context) 18 call_block_for_path_type(context.request.method, context.request.path, :before, context) 19 if Kemal.config.error_handlers.has_key?(context.response.status_code) 20 raise Kemal::Exceptions::CustomException.new(context) 21 end 22 call_next(context) 23 call_block_for_path_type(context.request.method, context.request.path, :after, context) 24 call_block_for_path_type("ALL", context.request.path, :after, context) 25 context 26 end 27
23 24 abstract def call(context : HTTP::Server::Context) 25 26 def call_next(context : HTTP::Server::Context) 27 if next_handler = @next 28 next_handler.call(context) 29 else 30 context.response.respond_with_status(:not_found) 31 end 32 end 33
45 env.redirect "https://playitmyway.org#{env.request.path}#{"?" + query if query}" 46 elsif env.request.hostname == "playitmyway.inet2.org" 47 env.redirect "https://playitmyway.org#{env.request.path}#{"?" + query if query}" 48 else 49 # success or no login required 50 call_next env 51 end 52 end 53end 54 55add_handler CustomHandler.new
23 24 abstract def call(context : HTTP::Server::Context) 25 26 def call_next(context : HTTP::Server::Context) 27 if next_handler = @next 28 next_handler.call(context) 29 else 30 context.response.respond_with_status(:not_found) 31 end 32 end 33
70 context.response.status_code = 304 71 return 72 end 73 send_file(context, file_path) 74 else 75 call_next(context) 76 end 77 end 78 79 private def modification_time(file_path) 80 File.info(file_path).modification_time
23 24 abstract def call(context : HTTP::Server::Context) 25 26 def call_next(context : HTTP::Server::Context) 27 if next_handler = @next 28 next_handler.call(context) 29 else 30 context.response.respond_with_status(:not_found) 31 end 32 end 33
3 class ExceptionHandler 4 include HTTP::Handler 5 INSTANCE = new 6 7 def call(context : HTTP::Server::Context) 8 call_next(context) 9 rescue ex : Kemal::Exceptions::RouteNotFound 10 call_exception_with_status_code(context, ex, 404) 11 rescue ex : Kemal::Exceptions::CustomException 12 call_exception_with_status_code(context, ex, context.response.status_code) 13 rescue ex : Exception
23 24 abstract def call(context : HTTP::Server::Context) 25 26 def call_next(context : HTTP::Server::Context) 27 if next_handler = @next 28 next_handler.call(context) 29 else 30 context.response.respond_with_status(:not_found) 31 end 32 end 33
3 class LogHandler < Kemal::BaseLogHandler 4 def initialize(@io : IO = STDOUT) 5 end 6 7 def call(context : HTTP::Server::Context) 8 elapsed_time = Time.measure { call_next(context) } 9 elapsed_text = elapsed_text(elapsed_time) 10 @io << Time.utc << ' ' << context.response.status_code << ' ' << context.request.method << ' ' << context.request.resource << ' ' << elapsed_text << '\n' 11 @io.flush 12 context 13 end
23 24 abstract def call(context : HTTP::Server::Context) 25 26 def call_next(context : HTTP::Server::Context) 27 if next_handler = @next 28 next_handler.call(context) 29 else 30 context.response.respond_with_status(:not_found) 31 end 32 end 33
7 INSTANCE = new 8 9 def call(context : HTTP::Server::Context) 10 context.response.headers.add "X-Powered-By", "Kemal" if Kemal.config.powered_by_header 11 context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type") 12 call_next context 13 end 14 end 15end
46 response.version = request.version 47 response.headers["Connection"] = "keep-alive" if request.keep_alive? 48 context = Context.new(request, response) 49 50 Log.with_context do 51 @handler.call(context) 52 rescue ex : ClientError 53 Log.debug(exception: ex.cause) { ex.message } 54 rescue ex 55 Log.error(exception: ex) { "Unhandled exception on HTTP::Handler" } 56 unless response.closed?
510 return 511 end 512 end 513 {% end %} 514 515 @processor.process(io, io) 516 ensure 517 {% begin %} 518 begin 519 io.close 520 rescue IO::Error{% unless flag?(:without_openssl) %} | OpenSSL::SSL::Error{% end %}
463 end 464 465 if io 466 # a non nillable version of the closured io 467 _io = io 468 spawn handle_client(_io) 469 else 470 break 471 end 472 end 473 ensure
261 # add = ->(x : Int32, y : Int32) { x + y } 262 # add.call(1, 2) # => 3 263 # ``` 264 @[Primitive(:proc_call)] 265 @[Raises] 266 def call(*args : *T) : R 267 end 268end 269 270# All `Number` methods are defined on concrete structs (for example `Int32`, `UInt8`, etc.), 271# never on `Number`, `Int` or `Float` because we don't want to handle a primitive for
93 {Pointer(Void).null, Pointer(Void).null} 94 {% else %} 95 Fiber.stack_pool.checkout 96 {% end %} 97 98 fiber_main = ->(f : Fiber) { f.run } 99 100 # FIXME: This line shouldn't be necessary (#7975) 101 stack_ptr = nil 102 {% if flag?(:win32) %} 103 # align stack bottom to 16 bytes