From 6faf41630a5bb8eb23f3a2800ab01a8121fa1b09 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 8 Mar 2016 17:23:40 -0500 Subject: rename handlers --- src/guff/handlers/api-handler.cr | 148 --------------------------------- src/guff/handlers/api.cr | 148 +++++++++++++++++++++++++++++++++ src/guff/handlers/blog-handler.cr | 104 ----------------------- src/guff/handlers/blog.cr | 104 +++++++++++++++++++++++ src/guff/handlers/not-found-handler.cr | 8 -- src/guff/handlers/not-found.cr | 8 ++ src/guff/handlers/test-handler.cr | 12 --- src/guff/handlers/test.cr | 12 +++ 8 files changed, 272 insertions(+), 272 deletions(-) delete mode 100644 src/guff/handlers/api-handler.cr create mode 100644 src/guff/handlers/api.cr delete mode 100644 src/guff/handlers/blog-handler.cr create mode 100644 src/guff/handlers/blog.cr delete mode 100644 src/guff/handlers/not-found-handler.cr create mode 100644 src/guff/handlers/not-found.cr delete mode 100644 src/guff/handlers/test-handler.cr create mode 100644 src/guff/handlers/test.cr diff --git a/src/guff/handlers/api-handler.cr b/src/guff/handlers/api-handler.cr deleted file mode 100644 index 77606ae..0000000 --- a/src/guff/handlers/api-handler.cr +++ /dev/null @@ -1,148 +0,0 @@ -require "json" -require "../handler" -require "../api/*" -require "../views/html/api-docs" - -private macro define_method_calls(hash) - case namespace - {% for namespace, methods in hash %} - when "{{ namespace.id }}" - case method - {% for method in methods %} - when "{{ method }}" - do_{{ namespace.id }}_{{ method }}(context, get_method_args( - context.request.query_params, - "{{ namespace.id }}", - "{{ method.id }}" - )).to_json - {% end %} - else - raise "unknown method" - end - {% end %} - else - raise "unknown namespace" - end -end - -class Guff::Handlers::APIHandler < Guff::Handler - include API::Methods - include API::ContentType - include API::Util - include API::PostAPI - include API::DirAPI - include API::FileAPI - include API::TagAPI - include API::SiteAPI - include API::TestAPI - - PATH_RE = %r{ - ^/api - - (?: - # method call - (?: - / - (?[a-z0-9_-]+) - / - (?[a-z0-9_]+) - ) - - | - - # index.html - /(?:index(?:\.html|)|) - - | - - # implicit index (no trailing slash) - ) - - $ - }mx - - def call(context : HTTP::Server::Context) - if md = (context.request.path || "").match(PATH_RE) - if md["namespace"]? - # method call - do_call(context, md["namespace"], md["method"]) - else - # api index - do_docs(context) - end - else - call_next(context) - end - end - - private def do_call( - context : HTTP::Server::Context, - namespace : String, - method : String - ) - # set response type - context.response.content_type = get_content_type - - # method call - json = begin - # - # macro expands to equivalent of the following for each - # namespace and method: - # - # args = get_method_args(context, namespace, method) - # send("do_#{namespace}_#{method}".intern, context, args) - # - define_method_calls({ - post: [ - get_posts, - add_post, - update_post, - remove_posts, - set_tags, - ], - - dir: [ - add, - remove, - ], - - file: [ - add, - remove, - ], - - tag: [ - get_tags, - remove_tags, - ], - - site: [ - add_site, - remove_sites, - set_default, - set_domains, - ], - - test: [ - version, - get_posts, - error, - ], - }) - rescue e - # log backtrace - # FIXME - puts e.inspect_with_backtrace - - # return error to user - { "error": e.to_s }.to_json - end - - # send body - context.response.puts json - end - - private def do_docs(context : HTTP::Server::Context) - Guff::APIDocsHTMLView.run(context) - end -end diff --git a/src/guff/handlers/api.cr b/src/guff/handlers/api.cr new file mode 100644 index 0000000..77606ae --- /dev/null +++ b/src/guff/handlers/api.cr @@ -0,0 +1,148 @@ +require "json" +require "../handler" +require "../api/*" +require "../views/html/api-docs" + +private macro define_method_calls(hash) + case namespace + {% for namespace, methods in hash %} + when "{{ namespace.id }}" + case method + {% for method in methods %} + when "{{ method }}" + do_{{ namespace.id }}_{{ method }}(context, get_method_args( + context.request.query_params, + "{{ namespace.id }}", + "{{ method.id }}" + )).to_json + {% end %} + else + raise "unknown method" + end + {% end %} + else + raise "unknown namespace" + end +end + +class Guff::Handlers::APIHandler < Guff::Handler + include API::Methods + include API::ContentType + include API::Util + include API::PostAPI + include API::DirAPI + include API::FileAPI + include API::TagAPI + include API::SiteAPI + include API::TestAPI + + PATH_RE = %r{ + ^/api + + (?: + # method call + (?: + / + (?[a-z0-9_-]+) + / + (?[a-z0-9_]+) + ) + + | + + # index.html + /(?:index(?:\.html|)|) + + | + + # implicit index (no trailing slash) + ) + + $ + }mx + + def call(context : HTTP::Server::Context) + if md = (context.request.path || "").match(PATH_RE) + if md["namespace"]? + # method call + do_call(context, md["namespace"], md["method"]) + else + # api index + do_docs(context) + end + else + call_next(context) + end + end + + private def do_call( + context : HTTP::Server::Context, + namespace : String, + method : String + ) + # set response type + context.response.content_type = get_content_type + + # method call + json = begin + # + # macro expands to equivalent of the following for each + # namespace and method: + # + # args = get_method_args(context, namespace, method) + # send("do_#{namespace}_#{method}".intern, context, args) + # + define_method_calls({ + post: [ + get_posts, + add_post, + update_post, + remove_posts, + set_tags, + ], + + dir: [ + add, + remove, + ], + + file: [ + add, + remove, + ], + + tag: [ + get_tags, + remove_tags, + ], + + site: [ + add_site, + remove_sites, + set_default, + set_domains, + ], + + test: [ + version, + get_posts, + error, + ], + }) + rescue e + # log backtrace + # FIXME + puts e.inspect_with_backtrace + + # return error to user + { "error": e.to_s }.to_json + end + + # send body + context.response.puts json + end + + private def do_docs(context : HTTP::Server::Context) + Guff::APIDocsHTMLView.run(context) + end +end diff --git a/src/guff/handlers/blog-handler.cr b/src/guff/handlers/blog-handler.cr deleted file mode 100644 index c95938c..0000000 --- a/src/guff/handlers/blog-handler.cr +++ /dev/null @@ -1,104 +0,0 @@ -require "../handler" - -class Guff::Handlers::BlogHandler < Guff::Handler - ROUTES = [{ - list: false, - blog: true, - re: %r{ - ^/ - - # match YYYY/MM/DD/SLUG.html - (?\d{4}) - / - (?\d{2}) - / - (?\d{2}) - / - (?[a-z0-9._-]+) - \.html - - $ - }x, - }, { - list: true, - blog: true, - re: %r{ - ^/ - - # match YYYY/MM/DD - (?\d{4}) - / - (?\d{2}) - / - (?\d{2}) - /? - - $ - }x, - }, { - list: true, - blog: true, - re: %r{ - ^/ - - # match YYYY/MM - (?\d{4}) - / - (?\d{2}) - /? - - $ - }x, - }, { - list: true, - blog: true, - re: %r{ - ^/ - - # match YYYY - (?\d{4}) - /? - - $ - }x, - }, { - list: false, - blog: false, - re: %r{ - ^/ - - # match slug - (?[a-z0-9._-]+) - \.html - - $ - }x, - }, { - list: true, - blog: true, - re: %r{ - # match index - ^/$ - }x, - }] - - def call(context : HTTP::Server::Context) - path = context.request.path || "" - - call_next(context) unless ROUTES.reduce(false) do |matched, route| - unless matched - if md = (route[:re] as Regex).match(path) - # matched route - matched = true - - context.response.puts "blog: route = %s, md = %s" % [ - route.to_s, - md.to_s - ] - end - end - - matched - end - end -end diff --git a/src/guff/handlers/blog.cr b/src/guff/handlers/blog.cr new file mode 100644 index 0000000..c95938c --- /dev/null +++ b/src/guff/handlers/blog.cr @@ -0,0 +1,104 @@ +require "../handler" + +class Guff::Handlers::BlogHandler < Guff::Handler + ROUTES = [{ + list: false, + blog: true, + re: %r{ + ^/ + + # match YYYY/MM/DD/SLUG.html + (?\d{4}) + / + (?\d{2}) + / + (?\d{2}) + / + (?[a-z0-9._-]+) + \.html + + $ + }x, + }, { + list: true, + blog: true, + re: %r{ + ^/ + + # match YYYY/MM/DD + (?\d{4}) + / + (?\d{2}) + / + (?\d{2}) + /? + + $ + }x, + }, { + list: true, + blog: true, + re: %r{ + ^/ + + # match YYYY/MM + (?\d{4}) + / + (?\d{2}) + /? + + $ + }x, + }, { + list: true, + blog: true, + re: %r{ + ^/ + + # match YYYY + (?\d{4}) + /? + + $ + }x, + }, { + list: false, + blog: false, + re: %r{ + ^/ + + # match slug + (?[a-z0-9._-]+) + \.html + + $ + }x, + }, { + list: true, + blog: true, + re: %r{ + # match index + ^/$ + }x, + }] + + def call(context : HTTP::Server::Context) + path = context.request.path || "" + + call_next(context) unless ROUTES.reduce(false) do |matched, route| + unless matched + if md = (route[:re] as Regex).match(path) + # matched route + matched = true + + context.response.puts "blog: route = %s, md = %s" % [ + route.to_s, + md.to_s + ] + end + end + + matched + end + end +end diff --git a/src/guff/handlers/not-found-handler.cr b/src/guff/handlers/not-found-handler.cr deleted file mode 100644 index 4cd564f..0000000 --- a/src/guff/handlers/not-found-handler.cr +++ /dev/null @@ -1,8 +0,0 @@ -require "../handler" - -class Guff::Handlers::NotFoundHandler < Guff::Handler - def call(context : HTTP::Server::Context) - context.response.status_code = 404 - context.response.puts "not found" - end -end diff --git a/src/guff/handlers/not-found.cr b/src/guff/handlers/not-found.cr new file mode 100644 index 0000000..4cd564f --- /dev/null +++ b/src/guff/handlers/not-found.cr @@ -0,0 +1,8 @@ +require "../handler" + +class Guff::Handlers::NotFoundHandler < Guff::Handler + def call(context : HTTP::Server::Context) + context.response.status_code = 404 + context.response.puts "not found" + end +end diff --git a/src/guff/handlers/test-handler.cr b/src/guff/handlers/test-handler.cr deleted file mode 100644 index f373630..0000000 --- a/src/guff/handlers/test-handler.cr +++ /dev/null @@ -1,12 +0,0 @@ -require "../handler" - -class Guff::Handlers::TestHandler < Guff::Handler - def call(context : HTTP::Server::Context) - if ((context.request.path || "").match(/^\/test\//)) - context.response.content_type = "text/html" - context.response.puts "test" - else - call_next(context) - end - end -end diff --git a/src/guff/handlers/test.cr b/src/guff/handlers/test.cr new file mode 100644 index 0000000..f373630 --- /dev/null +++ b/src/guff/handlers/test.cr @@ -0,0 +1,12 @@ +require "../handler" + +class Guff::Handlers::TestHandler < Guff::Handler + def call(context : HTTP::Server::Context) + if ((context.request.path || "").match(/^\/test\//)) + context.response.content_type = "text/html" + context.response.puts "test" + else + call_next(context) + end + end +end -- cgit v1.2.3