diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-05-22 13:29:06 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-05-22 13:29:06 -0400 |
commit | 66d2f67e35efb06c38deaacc9bfdd27aca4b2714 (patch) | |
tree | 7e2eb7e6d508c02fadbbfff61cf964e6e8a587f2 | |
parent | 2a2d6ed0b48276d8bfe82bbd1ad2c2f5e11290b6 (diff) | |
download | guff-66d2f67e35efb06c38deaacc9bfdd27aca4b2714.tar.bz2 guff-66d2f67e35efb06c38deaacc9bfdd27aca4b2714.zip |
add api handler and stub post and user apis
-rw-r--r-- | src/guff.cr | 127 |
1 files changed, 126 insertions, 1 deletions
diff --git a/src/guff.cr b/src/guff.cr index cbedd49..4bbcba3 100644 --- a/src/guff.cr +++ b/src/guff.cr @@ -13,6 +13,31 @@ private macro define_model_set_getters(hash) {% end %} end +private macro include_api_modules(modules) + {% for mod in modules.resolve %} + include {{ mod.id }} + {% end %} +end + +private macro api_method_dispatch(modules) + case namespace + {% for mod in modules.resolve %} + {% mod_name = mod.resolve.name.gsub(/^.*:(.*)API$/, "\\1").downcase %} + when {{ mod_name.stringify }} + case method + {% for mod_method in mod.resolve.methods %} + {% method_name = mod_method.name.gsub(/^do_([^_]+)_/, "") %} + when {{ method_name.stringify }} + {{ mod_method.name.id }}(params) + {% end %} + else + raise "unknown method: #{namespace}/#{method}" + end + {% end %} + else + raise "unknown namespace: #{namespace}" + end +end module Guff class Config @@ -202,6 +227,42 @@ module Guff false end end + + def add_user( + name : String? = nil, + email : String? = nil, + password : String? = nil, + groups : Array(String)? = nil, + active : Bool? = nil, + ) : Int64 + # TODO create user + user_id = 0_i64 + + # set user attributes + set_user( + user_id: user_id, + email: email, + password: password, + groups: groups, + active: active, + ) + + # return user id + user_id + end + + def set_user( + user_id : Int64, + name : String? = nil, + email : String? = nil, + password : String? = nil, + groups : Array(String)? = nil, + active : Bool? = nil, + ) + end + + def get_users + end end class SessionModel < Model @@ -402,6 +463,28 @@ module Guff end end + module APIs + module PostAPI + def do_post_get_posts(params : HTTP::Params) + { "asdf": "foo" } + end + end + + module UserAPI + def do_user_add_user(params : HTTP::Params) + user_id = @context.models.user.add_user( + name: params["name"]?, + email: params["email"], + password: params["password"]?, + active: params["active"]? ? (params["active"] == "t") : nil, + # groups: params["groups"]? ? JSON.parse(params["groups"]) : nil, + ) + + { "user_id": user_id } + end + end + end + module Views abstract class View def initialize(@context : Context) @@ -552,7 +635,7 @@ module Guff abstract def authenticated_call(context : HTTP::Server::Context) end - class SessionHandler < Guff::Handlers::Handler + class SessionHandler < Handler def call(context : HTTP::Server::Context) # clear session @context.session.clear @@ -566,6 +649,43 @@ module Guff end end + class APIHandler < Handler + PATH_RE = %r{^/guff/api/(?<namespace>[\w_-]+)/(?<method>[\w_-]+)$} + + API_MODULES = [ + APIs::PostAPI, + APIs::UserAPI, + ] + + include_api_modules(API_MODULES) + + def call(context : HTTP::Server::Context) + if context.request.method == "POST" || + (@context.development? && context.request.method == "GET") + if md = PATH_RE.match(context.request.path.not_nil!) + namespace, method = %w{namespace method}.map { |k| md[k] } + params = HTTP::Params.parse(context.request.body || "") + + code, data = begin + { 200, api_method_dispatch(API_MODULES) } + rescue err + STDERR.puts "ERROR: #{err}" + { 400, { "error": err.to_s } } + end + + # send json response + context.response.status_code = code + context.response.content_type = "application/json; charset=utf-8" + data.to_json(context.response) + else + call_next(context) + end + else + call_next(context) + end + end + end + class AssetsHandler < Handler def initialize(context : Context) super(context) @@ -798,6 +918,9 @@ module Guff id: :session, }, { dev: false, + id: :api, + }, { + dev: false, id: :assets, }, { dev: false, @@ -831,6 +954,8 @@ module Guff HTTP::DeflateHandler.new when :session SessionHandler.new(context) + when :api + APIHandler.new(context) when :assets AssetsHandler.new(context) when :admin |