aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/guff/handlers/api-handler.cr220
-rw-r--r--src/guff/handlers/blog-handler.cr172
-rw-r--r--src/guff/handlers/not-found-handler.cr12
-rw-r--r--src/guff/handlers/test-handler.cr18
4 files changed, 207 insertions, 215 deletions
diff --git a/src/guff/handlers/api-handler.cr b/src/guff/handlers/api-handler.cr
index 274e137..f40a39d 100644
--- a/src/guff/handlers/api-handler.cr
+++ b/src/guff/handlers/api-handler.cr
@@ -1,6 +1,6 @@
require "json"
-require "./handler"
-require "./api/*"
+require "../handler"
+require "../api/*"
private macro define_method_calls(hash)
case namespace
@@ -24,126 +24,124 @@ private macro define_method_calls(hash)
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
-
+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
(?:
- # method call
- (?:
- /
- (?<namespace>[a-z0-9_-]+)
- /
- (?<method>[a-z0-9_]+)
- )
+ /
+ (?<namespace>[a-z0-9_-]+)
+ /
+ (?<method>[a-z0-9_]+)
+ )
- |
+ |
- # index.html
- /(?:index(?:\.html|)|)
+ # index.html
+ /(?:index(?:\.html|)|)
- |
+ |
- # implicit index (no trailing slash)
- )
+ # implicit index (no trailing slash)
+ )
+
+ $
+ }mx
- $
- }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
+ 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
- call_next(context)
+ # 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
+ 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
- private def do_docs(context : HTTP::Server::Context)
- APIDocsHTMLView.run(context)
- end
+ # send body
+ context.response.puts json
+ end
+
+ private def do_docs(context : HTTP::Server::Context)
+ APIDocsHTMLView.run(context)
end
end
diff --git a/src/guff/handlers/blog-handler.cr b/src/guff/handlers/blog-handler.cr
index 988eab5..c95938c 100644
--- a/src/guff/handlers/blog-handler.cr
+++ b/src/guff/handlers/blog-handler.cr
@@ -1,106 +1,104 @@
-require "./handler"
+require "../handler"
-module Guff
- class BlogHandler < Handler
- ROUTES = [{
- list: false,
- blog: true,
- re: %r{
- ^/
+class Guff::Handlers::BlogHandler < Guff::Handler
+ ROUTES = [{
+ list: false,
+ blog: true,
+ re: %r{
+ ^/
- # match YYYY/MM/DD/SLUG.html
- (?<year>\d{4})
- /
- (?<month>\d{2})
- /
- (?<day>\d{2})
- /
- (?<slug>[a-z0-9._-]+)
- \.html
+ # match YYYY/MM/DD/SLUG.html
+ (?<year>\d{4})
+ /
+ (?<month>\d{2})
+ /
+ (?<day>\d{2})
+ /
+ (?<slug>[a-z0-9._-]+)
+ \.html
- $
- }x,
- }, {
- list: true,
- blog: true,
- re: %r{
- ^/
+ $
+ }x,
+ }, {
+ list: true,
+ blog: true,
+ re: %r{
+ ^/
- # match YYYY/MM/DD
- (?<year>\d{4})
- /
- (?<month>\d{2})
- /
- (?<day>\d{2})
- /?
+ # match YYYY/MM/DD
+ (?<year>\d{4})
+ /
+ (?<month>\d{2})
+ /
+ (?<day>\d{2})
+ /?
- $
- }x,
- }, {
- list: true,
- blog: true,
- re: %r{
- ^/
+ $
+ }x,
+ }, {
+ list: true,
+ blog: true,
+ re: %r{
+ ^/
- # match YYYY/MM
- (?<year>\d{4})
- /
- (?<month>\d{2})
- /?
+ # match YYYY/MM
+ (?<year>\d{4})
+ /
+ (?<month>\d{2})
+ /?
- $
- }x,
- }, {
- list: true,
- blog: true,
- re: %r{
- ^/
+ $
+ }x,
+ }, {
+ list: true,
+ blog: true,
+ re: %r{
+ ^/
- # match YYYY
- (?<year>\d{4})
- /?
+ # match YYYY
+ (?<year>\d{4})
+ /?
- $
- }x,
- }, {
- list: false,
- blog: false,
- re: %r{
- ^/
+ $
+ }x,
+ }, {
+ list: false,
+ blog: false,
+ re: %r{
+ ^/
- # match slug
- (?<slug>[a-z0-9._-]+)
- \.html
+ # match slug
+ (?<slug>[a-z0-9._-]+)
+ \.html
- $
- }x,
- }, {
- list: true,
- blog: true,
- re: %r{
- # match index
- ^/$
- }x,
- }]
+ $
+ }x,
+ }, {
+ list: true,
+ blog: true,
+ re: %r{
+ # match index
+ ^/$
+ }x,
+ }]
- def call(context : HTTP::Server::Context)
- path = context.request.path || ""
+ 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
+ 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
+ context.response.puts "blog: route = %s, md = %s" % [
+ route.to_s,
+ md.to_s
+ ]
end
-
- matched
end
+
+ matched
end
end
end
diff --git a/src/guff/handlers/not-found-handler.cr b/src/guff/handlers/not-found-handler.cr
index b15b1dc..4cd564f 100644
--- a/src/guff/handlers/not-found-handler.cr
+++ b/src/guff/handlers/not-found-handler.cr
@@ -1,10 +1,8 @@
-require "./handler"
+require "../handler"
-module Guff
- class NotFoundHandler < Handler
- def call(context : HTTP::Server::Context)
- context.response.status_code = 404
- context.response.puts "not found"
- end
+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
index 3856400..f373630 100644
--- a/src/guff/handlers/test-handler.cr
+++ b/src/guff/handlers/test-handler.cr
@@ -1,14 +1,12 @@
-require "./handler"
+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
+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