From b5d66c8d5ce941a6ee290128807f1f9e50aee4e9 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 8 Mar 2016 16:59:30 -0500 Subject: mv src/guff/*-handler.cr src/guff/handler/ --- src/guff/api-handler.cr | 149 --------------------------------- src/guff/blog-handler.cr | 106 ----------------------- src/guff/handlers.cr | 6 +- src/guff/handlers/api-handler.cr | 149 +++++++++++++++++++++++++++++++++ src/guff/handlers/blog-handler.cr | 106 +++++++++++++++++++++++ src/guff/handlers/not-found-handler.cr | 10 +++ src/guff/handlers/test-handler.cr | 14 ++++ src/guff/not-found-handler.cr | 10 --- src/guff/test-handler.cr | 14 ---- 9 files changed, 280 insertions(+), 284 deletions(-) delete mode 100644 src/guff/api-handler.cr delete mode 100644 src/guff/blog-handler.cr create mode 100644 src/guff/handlers/api-handler.cr create mode 100644 src/guff/handlers/blog-handler.cr create mode 100644 src/guff/handlers/not-found-handler.cr create mode 100644 src/guff/handlers/test-handler.cr delete mode 100644 src/guff/not-found-handler.cr delete mode 100644 src/guff/test-handler.cr diff --git a/src/guff/api-handler.cr b/src/guff/api-handler.cr deleted file mode 100644 index 274e137..0000000 --- a/src/guff/api-handler.cr +++ /dev/null @@ -1,149 +0,0 @@ -require "json" -require "./handler" -require "./api/*" - -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 - -module Guff - class APIHandler < 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) - APIDocsHTMLView.run(context) - end - end -end diff --git a/src/guff/blog-handler.cr b/src/guff/blog-handler.cr deleted file mode 100644 index 988eab5..0000000 --- a/src/guff/blog-handler.cr +++ /dev/null @@ -1,106 +0,0 @@ -require "./handler" - -module Guff - class BlogHandler < 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 -end diff --git a/src/guff/handlers.cr b/src/guff/handlers.cr index 6eeb389..ba48bbd 100644 --- a/src/guff/handlers.cr +++ b/src/guff/handlers.cr @@ -1,7 +1,7 @@ require "http/server" require "./config" require "./model" -require "./blog-handler" +require "./handlers/*" module Guff module Handlers @@ -32,10 +32,6 @@ module Guff init: ->(models : Models) { BlogHandler.new(models) as HTTP::Handler }, - }, { - init: ->(models : Models) { - SlugHandler.new(models) as HTTP::Handler - }, }, { init: ->(models : Models) { HTTP::StaticFileHandler.new(models.config["public"]) as HTTP::Handler diff --git a/src/guff/handlers/api-handler.cr b/src/guff/handlers/api-handler.cr new file mode 100644 index 0000000..274e137 --- /dev/null +++ b/src/guff/handlers/api-handler.cr @@ -0,0 +1,149 @@ +require "json" +require "./handler" +require "./api/*" + +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 + +module Guff + class APIHandler < 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) + APIDocsHTMLView.run(context) + end + end +end diff --git a/src/guff/handlers/blog-handler.cr b/src/guff/handlers/blog-handler.cr new file mode 100644 index 0000000..988eab5 --- /dev/null +++ b/src/guff/handlers/blog-handler.cr @@ -0,0 +1,106 @@ +require "./handler" + +module Guff + class BlogHandler < 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 +end diff --git a/src/guff/handlers/not-found-handler.cr b/src/guff/handlers/not-found-handler.cr new file mode 100644 index 0000000..b15b1dc --- /dev/null +++ b/src/guff/handlers/not-found-handler.cr @@ -0,0 +1,10 @@ +require "./handler" + +module Guff + class NotFoundHandler < Handler + def call(context : HTTP::Server::Context) + context.response.status_code = 404 + context.response.puts "not found" + end + end +end diff --git a/src/guff/handlers/test-handler.cr b/src/guff/handlers/test-handler.cr new file mode 100644 index 0000000..3856400 --- /dev/null +++ b/src/guff/handlers/test-handler.cr @@ -0,0 +1,14 @@ +require "./handler" + +module Guff + class TestHandler < 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 +end diff --git a/src/guff/not-found-handler.cr b/src/guff/not-found-handler.cr deleted file mode 100644 index b15b1dc..0000000 --- a/src/guff/not-found-handler.cr +++ /dev/null @@ -1,10 +0,0 @@ -require "./handler" - -module Guff - class NotFoundHandler < Handler - def call(context : HTTP::Server::Context) - context.response.status_code = 404 - context.response.puts "not found" - end - end -end diff --git a/src/guff/test-handler.cr b/src/guff/test-handler.cr deleted file mode 100644 index 3856400..0000000 --- a/src/guff/test-handler.cr +++ /dev/null @@ -1,14 +0,0 @@ -require "./handler" - -module Guff - class TestHandler < 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 -end -- cgit v1.2.3