From c6d6efbe2bd0eec27117fe0ba4c6bd6a50a955c9 Mon Sep 17 00:00:00 2001 From: Denis Toritsyn Date: Mon, 11 Jul 2022 23:40:13 +0500 Subject: [PATCH 1/6] feat: task20, add render capabilities --- app/controllers/tests_controller.rb | 1 + lib/simpler/router.rb | 2 +- lib/simpler/view.rb | 24 ++++++++++++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..f052c1e2 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -2,6 +2,7 @@ class TestsController < Simpler::Controller def index @time = Time.now + render plain: "#{@time} This is text from Plain" end def create diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..7313e730 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -22,7 +22,7 @@ def route_for(env) @routes.find { |route| route.match?(method, path) } end - private + private7 def add_route(method, path, route_point) route_point = route_point.split('#') diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..0beca00a 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -10,12 +10,32 @@ def initialize(env) end def render(binding) - template = File.read(template_path) + extend? ? extending_render : file_render(binding) + end + + private + + def extending_render + send(key_hash) + end + + def key_hesh + @env['simpler.template'].keys[0] + end + + def plain + @env['simpler.template'].values[0] + "\n" + end + def file_render(binding) + template = File.read(template_path) + ERB.new(template).result(binding) end - private + def extend? + @env['simpler.template'].class == Hash + end def controller @env['simpler.controller'] From 30d6f18aace26cead96326ceb1573a8ec0d6517f Mon Sep 17 00:00:00 2001 From: Denis Toritsyn Date: Tue, 12 Jul 2022 19:36:24 +0500 Subject: [PATCH 2/6] feat: task20, add set status, add set content-type --- app/controllers/tests_controller.rb | 3 ++- lib/simpler/controller.rb | 22 +++++++++++++++++----- lib/simpler/router.rb | 2 +- lib/simpler/view.rb | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index f052c1e2..d37dacd5 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -6,7 +6,8 @@ def index end def create - + set_headers('text/html') + render plain: "This is text from Plain create" end end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..d82ffca1 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -15,23 +15,35 @@ def make_response(action) @request.env['simpler.controller'] = self @request.env['simpler.action'] = action - set_default_headers + set_headers + set_status send(action) write_response @response.finish end + def set_status(status = nil) + case @request.request_method + when 'GET' + @response.status = Rack::Utils::SYMBOL_TO_STATUS_CODE[:ok] + when 'POST' + @response.status = Rack::Utils::SYMBOL_TO_STATUS_CODE[:created] + end + + @response.status = status unless status.nil? + end + + def set_headers(type = 'text/html') + @response['Content-Type'] = type + end + private def extract_name self.class.name.match('(?.+)Controller')[:name].downcase end - def set_default_headers - @response['Content-Type'] = 'text/html' - end - def write_response body = render_body diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 7313e730..14b3415c 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -22,7 +22,7 @@ def route_for(env) @routes.find { |route| route.match?(method, path) } end - private7 + private def add_route(method, path, route_point) route_point = route_point.split('#') diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 0beca00a..de0db1e8 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -19,7 +19,7 @@ def extending_render send(key_hash) end - def key_hesh + def key_hash @env['simpler.template'].keys[0] end From 213e80f60fe0b57e15205420477b5aa1dc7029b1 Mon Sep 17 00:00:00 2001 From: Denis Toritsyn Date: Tue, 12 Jul 2022 20:26:01 +0500 Subject: [PATCH 3/6] feat: task20, add handler 404 --- lib/simpler/application.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 711946a9..3a123c6a 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -27,11 +27,14 @@ def routes(&block) end def call(env) - route = @router.route_for(env) - controller = route.controller.new(env) - action = route.action - - make_response(controller, action) + if (route = @router.route_for(env)) + controller = route.controller.new(env) + action = route.action + + make_response(controller, action) + else + Rack::Response.new("Resource not exist!\n", 404, {}).finish + end end private From 9398021273dd2abf4d2308192b29e0478b4e36c7 Mon Sep 17 00:00:00 2001 From: Denis Toritsyn Date: Fri, 15 Jul 2022 00:11:58 +0500 Subject: [PATCH 4/6] feat: task20, add parsing route get show --- app/controllers/tests_controller.rb | 8 +++++--- app/models/test.rb | 1 - app/views/tests/show.html.erb | 12 ++++++++++++ config/routes.rb | 1 + db/test_guru.sqlite | Bin 12288 -> 12288 bytes lib/simpler.rb | 2 -- lib/simpler/application.rb | 8 +++++--- lib/simpler/controller.rb | 20 +++++++++++++------- lib/simpler/router.rb | 2 -- lib/simpler/router/route.rb | 19 +++++++++++++++++-- lib/simpler/view.rb | 8 +++----- 11 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 app/views/tests/show.html.erb diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index d37dacd5..f4c37752 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,13 +1,15 @@ class TestsController < Simpler::Controller - def index @time = Time.now render plain: "#{@time} This is text from Plain" end def create - set_headers('text/html') - render plain: "This is text from Plain create" + render plain: 'This is text from Plain create' end + def show + @test = Test.where(id: params[:id]) + render plain: "Action Show: params: #{params}, @test = #{@test} " + end end diff --git a/app/models/test.rb b/app/models/test.rb index 86376668..98cea0cb 100644 --- a/app/models/test.rb +++ b/app/models/test.rb @@ -4,5 +4,4 @@ # Integer :level, default: 0 # end class Test < Sequel::Model - end diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb new file mode 100644 index 00000000..35e0eea1 --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1,12 @@ + + + + + Show | Simpler application + + +

Simpler framework at work!

+ +

<%= @time %>

+ + diff --git a/config/routes.rb b/config/routes.rb index 4a751251..1700ff3a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Simpler.application.routes do get '/tests', 'tests#index' post '/tests', 'tests#create' + get '/tests/:id', 'tests#show' end diff --git a/db/test_guru.sqlite b/db/test_guru.sqlite index 8624a03ab149d27f007f699649e9822c446f66e1..7116c4cff6ef83de5dbf364c5f750420b1e798ce 100644 GIT binary patch delta 113 zcmZojXh@hK&B!rP##xYqLC^jsF9QPu6Tc7xe+s|QW<`NTe3OOvKMAq(S2FOw<-f&$ zf`2#v68=g2l|Z>.+)Controller')[:name].downcase end @@ -55,12 +56,17 @@ def render_body end def params + form_param @request.params end + def form_param + path_parse = @request.path.split('/') + @request.params[:id] = path_parse[2].to_i if path_parse.size > 2 && path_parse[2] =~ /^\d+$/ + end + def render(template) @request.env['simpler.template'] = template end - end end diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..b05449b3 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -2,7 +2,6 @@ module Simpler class Router - def initialize @routes = [] end @@ -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 4c66b4b7..85c7536c 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -1,7 +1,6 @@ module Simpler class Router class Route - attr_reader :controller, :action def initialize(method, path, controller, action) @@ -12,9 +11,25 @@ def initialize(method, path, controller, action) end def match?(method, path) - @method == method && path.match(@path) + @method == method && path_match?(path) + end + + private + + def path_match?(path) + path_request = path.split('/') + path_router = @path.split('/') + compare(path_request, path_router) end + def compare(path_request, path_router) + return false if path_request.size != path_router.size + + path_router.each_with_index do |item, index| + next if item =~ /^:[\d\w]+$/ && path_request[index] =~ /^\d+$/ + return false if item != path_request[index] + end + end end end end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index de0db1e8..2365e725 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -2,7 +2,6 @@ module Simpler class View - VIEW_BASE_PATH = 'app/views'.freeze def initialize(env) @@ -14,7 +13,7 @@ def render(binding) end private - + def extending_render send(key_hash) end @@ -29,12 +28,12 @@ def plain def file_render(binding) template = File.read(template_path) - + ERB.new(template).result(binding) end def extend? - @env['simpler.template'].class == Hash + @env['simpler.template'].instance_of?(Hash) end def controller @@ -54,6 +53,5 @@ def template_path Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end - end end From 52e55830734864aeca65ee1129eb584e5d1d8c8d Mon Sep 17 00:00:00 2001 From: Denis Toritsyn Date: Sat, 16 Jul 2022 00:03:18 +0500 Subject: [PATCH 5/6] feat: task20, add logger middleware --- app/controllers/tests_controller.rb | 2 +- config.ru | 1 + lib/logger.rb | 39 +++++++++++++++++++++++++++++ lib/simpler/application.rb | 5 ++++ lib/simpler/tmp | 15 +++++++++++ lib/simpler/view.rb | 1 + log/app.log | 32 +++++++++++++++++++++++ 7 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 lib/logger.rb create mode 100644 lib/simpler/tmp create mode 100644 log/app.log diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index f4c37752..8e3890dd 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,7 +1,7 @@ class TestsController < Simpler::Controller def index @time = Time.now - render plain: "#{@time} This is text from Plain" + # render plain: "#{@time} This is text from Plain" end def create diff --git a/config.ru b/config.ru index 3060cc20..283bc97a 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,4 @@ require_relative 'config/environment' +use Logger run Simpler.application diff --git a/lib/logger.rb b/lib/logger.rb new file mode 100644 index 00000000..78fed35f --- /dev/null +++ b/lib/logger.rb @@ -0,0 +1,39 @@ +class Logger + def initialize(app) + @app = app + end + + def call(env) + log_file = File.open('log/app.log', 'a+') + request = Rack::Request.new(env) + status, headers, body = @app.call(env) + response = Rack::Response.new(body, status, headers) + write_log(log_file, request, status, headers) + + response.finish + end + + private + + def write_log(file, request, status, headers) + log_body = log_form(request, status, headers) + file.write(log_body) + + file.close + end + + def log_form(request, status, headers) + method = request.request_method + url = request.fullpath + controller = request.env['simpler.controller'] + action = request.env['simpler.action'] + pattern = request.env['simpler.pattern'] + params = request.params + + "#{Time.now} + Request: #{method} #{url} + Handler: #{controller.class.name}##{action} + Parameters: #{params} + Response: #{status} [#{headers['Content-Type']}] #{pattern} \n\n" + end +end diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 0b6f08e7..fb018289 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -19,6 +19,7 @@ def bootstrap! setup_database require_app require_routes + require_logger end def routes(&block) @@ -50,6 +51,10 @@ def require_routes require Simpler.root.join('config/routes') end + def require_logger + require_relative '../logger' + end + def setup_database database_config = YAML.load_file(Simpler.root.join('config/database.yml')) database_config['database'] = Simpler.root.join(database_config['database']) diff --git a/lib/simpler/tmp b/lib/simpler/tmp new file mode 100644 index 00000000..83ee94c3 --- /dev/null +++ b/lib/simpler/tmp @@ -0,0 +1,15 @@ +b) Для запросов необходимо записывать HTTP-метод запроса, + URL, контроллер и метод который будет обрабатывать запрос, + хэш параметров который будет доступен при вызове метода params + +c) Для ответов необходимо записывать код статуса ответа, +тип тела ответа и название шаблона +(если ответ рендерился с помощью шаблона представления) + +Пример: + + +Request: GET /tests?category=Backend +Handler: TestsController#index +Parameters: {'category' => 'Backend'} +Response: 200 OK [text/html] tests/index.html.erb diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 2365e725..515d5dfc 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -50,6 +50,7 @@ def template def template_path path = template || [controller.name, action].join('/') + @env['simpler.pattern'] = "#{path}.html.erb" Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end diff --git a/log/app.log b/log/app.log new file mode 100644 index 00000000..fa3ccad1 --- /dev/null +++ b/log/app.log @@ -0,0 +1,32 @@ +2022-07-15 23:47:13 +0500 + Request: GET /tests?level=2 + Handler: TestsController#index + Parameters: {"level"=>"2"} + Index: + Response: 200 [text/html] tests/index.html.erb + +2022-07-15 23:48:18 +0500 + Request: GET /tests?level=2 + Handler: TestsController#index + Parameters: {"level"=>"2"} + Index: + Response: 200 [text/html] tests/index.html.erb + +2022-07-15 23:58:08 +0500 + Request: GET /tests/102 + Handler: TestsController#show + Parameters: {} + Response: 200 [text/html] + +2022-07-15 23:58:11 +0500 + Request: GET /tests?level=2 + Handler: TestsController#index + Parameters: {"level"=>"2"} + Response: 200 [text/html] tests/index.html.erb + +2022-07-15 23:58:17 +0500 + Request: POST /tests + Handler: TestsController#create + Parameters: {} + Response: 201 [text/html] + From fcc45ef71e838b820ff79278fa24f0b4ff47b759 Mon Sep 17 00:00:00 2001 From: Denis Toritsyn Date: Sat, 16 Jul 2022 16:54:31 +0500 Subject: [PATCH 6/6] fix: task20, correct params log --- .gitignore | 1 + lib/logger.rb | 2 +- lib/simpler/application.rb | 1 + lib/simpler/controller.rb | 6 +-- lib/simpler/router/route.rb | 12 +++++- log/app.log | 80 ++++++++++++++++++++++++++----------- 6 files changed, 72 insertions(+), 30 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7106fcab --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +./log diff --git a/lib/logger.rb b/lib/logger.rb index 78fed35f..77faeb71 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -28,7 +28,7 @@ def log_form(request, status, headers) controller = request.env['simpler.controller'] action = request.env['simpler.action'] pattern = request.env['simpler.pattern'] - params = request.params + params = request.env['simpler.route_params'] "#{Time.now} Request: #{method} #{url} diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index fb018289..cae066b0 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -28,6 +28,7 @@ def routes(&block) def call(env) if (route = @router.route_for(env)) + env['simpler.route_params'] = route.params controller = route.controller.new(env) action = route.action diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 2ca983d1..e5216544 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -56,13 +56,11 @@ def render_body end def params - form_param - @request.params + @request.params.merge(form_param) end def form_param - path_parse = @request.path.split('/') - @request.params[:id] = path_parse[2].to_i if path_parse.size > 2 && path_parse[2] =~ /^\d+$/ + @request.env['simpler.route_params'].merge!(@request.params) end def render(template) diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 85c7536c..299b157b 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -1,13 +1,14 @@ module Simpler class Router class Route - attr_reader :controller, :action + attr_reader :controller, :action, :params def initialize(method, path, controller, action) @method = method @path = path @controller = controller @action = action + @params = {} end def match?(method, path) @@ -26,10 +27,17 @@ def compare(path_request, path_router) return false if path_request.size != path_router.size path_router.each_with_index do |item, index| - next if item =~ /^:[\d\w]+$/ && path_request[index] =~ /^\d+$/ + if item =~ /^:[\d\w]+$/ + save_params(item, path_request[index]) + next + end return false if item != path_request[index] end end + + def save_params(key, value) + @params[key] = value + end end end end diff --git a/log/app.log b/log/app.log index fa3ccad1..2af53370 100644 --- a/log/app.log +++ b/log/app.log @@ -1,32 +1,66 @@ -2022-07-15 23:47:13 +0500 - Request: GET /tests?level=2 +2022-07-16 16:23:47 +0500 + Request: GET /tests?level=2 Handler: TestsController#index - Parameters: {"level"=>"2"} - Index: + Parameters: {"level"=>"2"} Response: 200 [text/html] tests/index.html.erb -2022-07-15 23:48:18 +0500 - Request: GET /tests?level=2 - Handler: TestsController#index - Parameters: {"level"=>"2"} - Index: - Response: 200 [text/html] tests/index.html.erb +2022-07-16 16:31:37 +0500 + Request: GET /tests/102 + Handler: TestsController#show + Parameters: {} + Response: 200 [text/html] -2022-07-15 23:58:08 +0500 - Request: GET /tests/102 +2022-07-16 16:34:07 +0500 + Request: GET /tests/102 Handler: TestsController#show - Parameters: {} + Parameters: {} Response: 200 [text/html] -2022-07-15 23:58:11 +0500 - Request: GET /tests?level=2 - Handler: TestsController#index - Parameters: {"level"=>"2"} - Response: 200 [text/html] tests/index.html.erb +2022-07-16 16:34:27 +0500 + Request: GET /tests/102 + Handler: TestsController#show + Parameters: {} + Response: 200 [text/html] + +2022-07-16 16:35:36 +0500 + Request: GET /tests/102 + Handler: TestsController#show + Parameters: {} + Response: 200 [text/html] + +2022-07-16 16:36:44 +0500 + Request: GET /tests/102 + Handler: TestsController#show + Parameters: {} + Response: 200 [text/html] -2022-07-15 23:58:17 +0500 - Request: POST /tests - Handler: TestsController#create - Parameters: {} - Response: 201 [text/html] +2022-07-16 16:38:11 +0500 + Request: GET /tests/102 + Handler: TestsController#show + Parameters: {} + Response: 200 [text/html] + +2022-07-16 16:46:56 +0500 + Request: GET /tests/102 + Handler: TestsController#show + Parameters: {} + Response: 200 [text/html] + +2022-07-16 16:48:05 +0500 + Request: GET /tests/102 + Handler: TestsController#show + Parameters: {":id"=>"102"} + Response: 200 [text/html] + +2022-07-16 16:48:38 +0500 + Request: GET /tests/101 + Handler: TestsController#show + Parameters: {":id"=>"101"} + Response: 200 [text/html] + +2022-07-16 16:48:50 +0500 + Request: GET /tests/101?level=2 + Handler: TestsController#show + Parameters: {":id"=>"101", "level"=>"2"} + Response: 200 [text/html]