aboutsummaryrefslogtreecommitdiff
path: root/src/guff/handlers/api-handler.cr
diff options
context:
space:
mode:
Diffstat (limited to 'src/guff/handlers/api-handler.cr')
-rw-r--r--src/guff/handlers/api-handler.cr220
1 files changed, 109 insertions, 111 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