From 1e038e36279f097bea9847a0df493991e03b8098 Mon Sep 17 00:00:00 2001 From: Franerial Date: Mon, 19 Jul 2021 23:09:13 +0300 Subject: [PATCH 1/2] add logger --- app/controllers/tests_controller.rb | 1 + config.ru | 4 ++- lib/simpler/middleware/logger.rb | 47 +++++++++++++++++++++++++++++ lib/simpler/router/route.rb | 6 ++-- log/app.log | 24 +++++++++++++++ 5 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 lib/simpler/middleware/logger.rb create mode 100644 log/app.log diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 900a415d..beaf84ea 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,6 +1,7 @@ class TestsController < Simpler::Controller def index @time = Time.now + render "tests/index" end def create diff --git a/config.ru b/config.ru index 3060cc20..6beefaa0 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,5 @@ -require_relative 'config/environment' +require_relative "config/environment" +require_relative "lib/simpler/middleware/logger" +use Simpler::AppLogger, logdev: File.expand_path("log/app.log", Simpler.root) run Simpler.application diff --git a/lib/simpler/middleware/logger.rb b/lib/simpler/middleware/logger.rb new file mode 100644 index 00000000..f63ee925 --- /dev/null +++ b/lib/simpler/middleware/logger.rb @@ -0,0 +1,47 @@ +require "logger" + +module Simpler + class AppLogger + def initialize(app, **options) + @logger = Logger.new(options[:logdev] || STDOUT) + @app = app + end + + def call(env) + @status, @header, @body = @app.call(env) + @request = Rack::Request.new(env) + + @logger.info(create_log) + + Rack::Response.new(@body, @status, @header).finish + end + + private + + def create_log + [@status, @header, @body] + %Q( + Request: #{@request.request_method} #{@request.fullpath} + Handler: #{controller}##{action} + Parameters: #{@request.params} + Response: #{full_status} \[#{@header["Content-Type"]}\] #{view} + ) + end + + def controller + @request.env["simpler.controller"].class.name + end + + def action + @request.env["simpler.action"] + end + + def full_status + "#{@status}" + " #{Rack::Utils::HTTP_STATUS_CODES[@status]}" + end + + def view + "#{@request.env["simpler.template"] || [controller, action].join("/")}.html.erb" unless @request.env["simpler.text_plain"] + end + end +end diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 157667c7..2cab9415 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -35,12 +35,12 @@ def define_params(path) end def define_edited_path(path) - str = path + edited_path = path @params.each do |param_name, param_value| - str = str.gsub param_value, ":#{param_name.to_s}" + edited_path = edited_path.gsub param_value, ":#{param_name.to_s}" end - str + edited_path end end end diff --git a/log/app.log b/log/app.log new file mode 100644 index 00000000..eaa7d491 --- /dev/null +++ b/log/app.log @@ -0,0 +1,24 @@ +I, [2021-07-19T23:06:31.330593 #268476] INFO -- : + Request: GET /tests/plain + Handler: TestsController#plain + Parameters: {} + Response: 201 Created [text/plain] + +I, [2021-07-19T23:06:40.146013 #268476] INFO -- : + Request: GET /tests/5 + Handler: TestsController#show + Parameters: {:id=>"5"} + Response: 200 OK [text/plain] + +I, [2021-07-19T23:06:51.112230 #268476] INFO -- : + Request: GET /tests + Handler: TestsController#index + Parameters: {} + Response: 200 OK [text/html] tests/index.html.erb + +I, [2021-07-19T23:06:58.187223 #268476] INFO -- : + Request: GET /tests/487/question/25?one=1 + Handler: TestsController#question + Parameters: {"one"=>"1", :id=>"487", :question_id=>"25"} + Response: 200 OK [text/plain] + From c4bd1153fc7bb8bd4b3617dbe61616df10461ae0 Mon Sep 17 00:00:00 2001 From: Franerial Date: Tue, 20 Jul 2021 21:37:14 +0300 Subject: [PATCH 2/2] logger and routing fix --- .gitignore | 2 ++ app/controllers/tests_controller.rb | 7 +++--- app/views/tests/show.html.erb | 10 +++++++++ lib/simpler/application.rb | 14 +++++++++++- lib/simpler/middleware/logger.rb | 14 +++++++++--- lib/simpler/router.rb | 10 ++++----- lib/simpler/router/route.rb | 16 ++++++++----- log/app.log | 35 +++++++++-------------------- 8 files changed, 65 insertions(+), 43 deletions(-) create mode 100644 .gitignore create mode 100644 app/views/tests/show.html.erb diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7ccc842a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Ignore all logfiles +/log/* diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index beaf84ea..e8a132a2 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -5,16 +5,17 @@ def index end def create + status 201 + render plain: "Create method in action!" end def plain - render plain: "plain_option_example" - status 201 + render plain: "Plain text responce example" + status 200 end def show @id = params[:id] - render plain: "This is an object (show tests method) with id #{@id}" end def question diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb new file mode 100644 index 00000000..694ab011 --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1,10 @@ + + + + + Index | Simpler application + + +

This is the test object (show tests method) with id <%= @id %>

+ + diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index fe8f2480..bba58164 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "yaml" require "singleton" require "sequel" @@ -27,7 +29,10 @@ def routes(&block) def call(env) route = @router.route_for(env) + return not_found unless route + return not_found unless route.params.empty? || db_route_params_exists?(route.params) + env["simpler.route_params"] = route.params controller = route.controller.new(env) action = route.action @@ -38,7 +43,7 @@ def call(env) private def require_app - Dir["#{Simpler.root}/app/**/*.rb"].each { |file| require file } + Dir["#{Simpler.root}/app/**/*.rb"].sort.each { |file| require file } end def require_routes @@ -58,5 +63,12 @@ def make_response(controller, action) def not_found [404, { "Content-Type" => "text/plain" }, ["Not found"]] end + + def db_route_params_exists?(params) + corresponding_record = nil + params.each { |_param_name, param_value| corresponding_record = @db[:tests].first(id: param_value) } + + !corresponding_record.nil? + end end end diff --git a/lib/simpler/middleware/logger.rb b/lib/simpler/middleware/logger.rb index f63ee925..dcf995e3 100644 --- a/lib/simpler/middleware/logger.rb +++ b/lib/simpler/middleware/logger.rb @@ -19,13 +19,21 @@ def call(env) private def create_log - [@status, @header, @body] - %Q( + if @status == 404 + %Q( + Request: #{@request.request_method} #{@request.fullpath} + Handler: Nil + Parameters: #{@request.params} + Response: #{full_status} + ).delete!("\n") + else + %Q( Request: #{@request.request_method} #{@request.fullpath} Handler: #{controller}##{action} Parameters: #{@request.params} Response: #{full_status} \[#{@header["Content-Type"]}\] #{view} - ) + ).delete!("\n") + end end def controller diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..77b7fdf6 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -1,8 +1,7 @@ -require_relative 'router/route' +require_relative "router/route" module Simpler class Router - def initialize @routes = [] end @@ -16,8 +15,8 @@ def post(path, route_point) end def route_for(env) - method = env['REQUEST_METHOD'].downcase.to_sym - path = env['PATH_INFO'] + method = env["REQUEST_METHOD"].downcase.to_sym + path = env["PATH_INFO"] @routes.find { |route| route.match?(method, path) } end @@ -25,7 +24,7 @@ def route_for(env) private def add_route(method, path, route_point) - route_point = route_point.split('#') + route_point = route_point.split("#") controller = controller_from_string(route_point[0]) action = route_point[1] route = Route.new(method, path, controller, action) @@ -36,6 +35,5 @@ def add_route(method, path, route_point) def controller_from_string(controller_name) Object.const_get("#{controller_name.capitalize}Controller") end - end end diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 2cab9415..a3763dbd 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -16,8 +16,13 @@ def initialize(method, path, controller, action) end def match?(method, path) - define_params(path) - edited_path = define_edited_path(path) || path + @params = define_params(path) + + edited_path = if @params.values.all? && (!@params.values.empty?) + define_edited_path(path) + else + path + end @method == method && edited_path == @path end @@ -31,14 +36,13 @@ def define_params(path) path.scan(ROUTE_PARAM_VALUE_REGEXP).each { |param| route_params_values << (param.delete "/") } @path.scan(ROUTE_PARAM_NAME_REGEXP).each { |param| route_params_names << param.delete(":").delete("/").to_sym } - @params = Hash[route_params_names.zip route_params_values] + Hash[route_params_names.zip route_params_values] end def define_edited_path(path) edited_path = path - @params.each do |param_name, param_value| - edited_path = edited_path.gsub param_value, ":#{param_name.to_s}" - end + + @params.each { |param_name, param_value| edited_path = edited_path.gsub param_value, ":#{param_name.to_s}" } edited_path end diff --git a/log/app.log b/log/app.log index eaa7d491..aa4618e0 100644 --- a/log/app.log +++ b/log/app.log @@ -1,24 +1,11 @@ -I, [2021-07-19T23:06:31.330593 #268476] INFO -- : - Request: GET /tests/plain - Handler: TestsController#plain - Parameters: {} - Response: 201 Created [text/plain] - -I, [2021-07-19T23:06:40.146013 #268476] INFO -- : - Request: GET /tests/5 - Handler: TestsController#show - Parameters: {:id=>"5"} - Response: 200 OK [text/plain] - -I, [2021-07-19T23:06:51.112230 #268476] INFO -- : - Request: GET /tests - Handler: TestsController#index - Parameters: {} - Response: 200 OK [text/html] tests/index.html.erb - -I, [2021-07-19T23:06:58.187223 #268476] INFO -- : - Request: GET /tests/487/question/25?one=1 - Handler: TestsController#question - Parameters: {"one"=>"1", :id=>"487", :question_id=>"25"} - Response: 200 OK [text/plain] - +I, [2021-07-20T21:29:48.157677 #288091] INFO -- : Request: GET /tests/2 Handler: TestsController#show Parameters: {:id=>"2"} Response: 200 OK [text/html] TestsController/show.html.erb +I, [2021-07-20T21:30:01.670387 #288091] INFO -- : Request: GET /tests/6 Handler: Nil Parameters: {} Response: 404 Not Found +I, [2021-07-20T21:30:14.369278 #288091] INFO -- : Request: GET /tests/3/question/25?one=1 Handler: Nil Parameters: {"one"=>"1"} Response: 404 Not Found +I, [2021-07-20T21:30:24.195654 #288091] INFO -- : Request: GET /tests Handler: TestsController#index Parameters: {} Response: 200 OK [text/html] tests/index.html.erb +I, [2021-07-20T21:32:27.196093 #288333] INFO -- : Request: POST /tests Handler: TestsController#create Parameters: {} Response: 201 Created [text/plain] +I, [2021-07-20T21:35:00.585388 #288333] INFO -- : Request: POST /tests Handler: TestsController#create Parameters: {} Response: 201 Created [text/plain] +I, [2021-07-20T21:35:02.896432 #288333] INFO -- : Request: GET /tests Handler: TestsController#index Parameters: {} Response: 200 OK [text/html] tests/index.html.erb +I, [2021-07-20T21:35:07.109826 #288333] INFO -- : Request: GET /tests/3/question/25?one=1 Handler: Nil Parameters: {"one"=>"1"} Response: 404 Not Found +I, [2021-07-20T21:35:10.289813 #288333] INFO -- : Request: GET /tests/6 Handler: Nil Parameters: {} Response: 404 Not Found +I, [2021-07-20T21:35:13.920833 #288333] INFO -- : Request: GET /tests/2 Handler: TestsController#show Parameters: {:id=>"2"} Response: 200 OK [text/html] TestsController/show.html.erb +I, [2021-07-20T21:35:20.181146 #288333] INFO -- : Request: GET /tests/plain Handler: TestsController#plain Parameters: {} Response: 200 OK [text/plain]