lib/ezframe/controller.rb in ezframe-0.0.4 vs lib/ezframe/controller.rb in ezframe-0.1.0
- old
+ new
@@ -1,61 +1,61 @@
# frozen_string_literal: true
+require "oj"
module Ezframe
- class Boot
+ class Controller
class << self
- def exec(request, response)
- @request = request
+ def init
Config.load_files("./config")
Model.init
- model = Model.get_clone
+ Message.init
Auth.init_warden if defined?(Warden)
+ end
+
+ def exec(request, response)
+ @request = request
+ model = Model.get_clone
@request.env["model"] = model
mylog("exec: path=#{request.path_info} params=#{request.params}")
- klass, method = PageBase::decide_route(request.path_info)
- unless klass
- response.status = 404
- response['Content-Type'] = 'text/html; charset=utf-8'
- response.body = [ Html.convert(Ht.p("file not found")) ]
+ page_instance, method, url_params = Route::choose(request)
+ mylog "page: #{page_instance.class}, method=#{method}, url_params=#{url_params}"
+ if !page_instance || page_instance == 404
+ file_not_found(response)
return
end
- method = "default" if !method || method=="undefined"
- page = klass.new(request, model)
- if request.post?
- method_full_name = "public_#{method}_post"
- else
- method_full_name = "public_#{method}_page"
- end
- if page.auth
- warden.authenticate!
- end
- # request.env["rack.session"]["kamatest"]="usable"
- # mylog "method: #{klass}.#{method_full_name}"
- #mylog "rack.session.id=#{request.env['rack.session'].id}"
+ @request.env["url_params"] = url_params
+ warden.authenticate! if page_instance.auth
mylog "rack.session.keys=#{request.env['rack.session'].keys}"
- #mylog "warden=#{request.env['warden'].inspect}"
- mylog "klass=#{klass}, method=#{method_full_name}"
- body = if page.respond_to?(method_full_name)
- page.send(method_full_name)
- else
- mylog "no such method: #{method_full_name}"
- page.public_default_page
- end
+ page_instance.set_request(@request)
+ body = page_instance.send(method)
+
+ # 戻り値によるレスポンス生成
if body.is_a?(Hash) || body.is_a?(Array)
+ # puts "Controller: body = #{body}"
response.body = [ JSON.generate(body) ]
+ # response.body = [ Oj.dump(body) ]
response['Content-Type'] = 'application/json; charset=utf-8'
else
response.body = [ body ]
response['Content-Type'] = 'text/html; charset=utf-8'
end
response.status = 200
+ # puts response.body
end
-# def file_not_found(response)
-# response.body = ['path not found']
-# response.status = 404
-# end
+ def file_not_found(response)
+ response.status = 404
+ response['Content-Type'] = 'text/html; charset=utf-8'
+ template_file = ("#{Config[:template_dir]}/404.html")
+ # puts template_file
+ if File.exist?(template_file)
+ body = File.read(template_file)
+ else
+ body = Html.convert(Ht.p("file not found"))
+ end
+ response.body = [ body ]
+ end
def warden
@request.env["warden"]
end