lib/ezframe/controller.rb in ezframe-0.1.1 vs lib/ezframe/controller.rb in ezframe-0.2.0

- old
+ new

@@ -1,47 +1,50 @@ # frozen_string_literal: true -require "oj" - module Ezframe class Controller class << self def init - Config.load_files("./config") - Model.init + Config.init + ColumnSets.init + DB.init Message.init - Auth.init_warden if defined?(Warden) + Auth.init if Config[:auth] end def exec(request, response) @request = request - model = Model.get_clone - @request.env["model"] = model + page_instance, method, url_params, class_opts = Route::choose(request) - mylog("exec: path=#{request.path_info} params=#{request.params}") - page_instance, method, url_params = Route::choose(request) - mylog "page: #{page_instance.class}, method=#{method}, url_params=#{url_params}" + Logger.debug("Controller.exec: path=#{request.path_info}, params=#{request.params}, class=#{page_instance.class}, method=#{method}, url_params=#{url_params}, class_opts=#{class_opts}") if !page_instance || page_instance == 404 file_not_found(response) return end @request.env["url_params"] = url_params - warden.authenticate! if page_instance.auth - mylog "rack.session.keys=#{request.env['rack.session'].keys}" + opt_auth = class_opts[:auth] + @session = @request.env['rack.session'] + if !@session[:user] && Config[:auth] && (!opt_auth || opt_auth != "disable") + Logger.debug("authenticate!") + warden.authenticate! + Logger.info "Controller.exec: warden.options = #{@request.env['warden.options']}" + end + # session["in_controller"] = "set in controller" + Logger.debug "rack.session.keys=#{@session.keys}" if @session 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) ] + # Logger.debug("Controller: body = #{body}") + json = JSON.generate(body) + response.body = [ json ] 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 + # Logger.debug("Controller.exec: response.body=#{response.body}") end def file_not_found(response) response.status = 404 response['Content-Type'] = 'text/html; charset=utf-8'