From f8157c5090033cf56b7e86ca5f47467d45c23ba6 Mon Sep 17 00:00:00 2001 From: ksilex Date: Tue, 23 Feb 2021 23:46:48 +0300 Subject: [PATCH 1/4] start --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) 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 From 1b96b723deb3f8369abb9caa8f759cdff5bbed0d Mon Sep 17 00:00:00 2001 From: ksilex Date: Wed, 24 Feb 2021 17:08:55 +0300 Subject: [PATCH 2/4] logger and show method --- app/controllers/tests_controller.rb | 4 ++++ app/views/tests/index.html.erb | 4 ++-- app/views/tests/show.html.erb | 12 ++++++++++++ config.ru | 3 ++- lib/simpler/controller.rb | 10 +++++++++- lib/simpler/router.rb | 6 ++++-- lib/simpler/router/route.rb | 17 ++++++++++++++++- lib/simpler_logger.rb | 24 ++++++++++++++++++++++++ log/app.log | 12 ++++++++++++ 9 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 app/views/tests/show.html.erb create mode 100644 lib/simpler_logger.rb create mode 100644 log/app.log diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..479168e0 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -8,4 +8,8 @@ def create end + def show + @time = "dd" + end + end diff --git a/app/views/tests/index.html.erb b/app/views/tests/index.html.erb index 39fce580..5bc1eeb1 100644 --- a/app/views/tests/index.html.erb +++ b/app/views/tests/index.html.erb @@ -7,6 +7,6 @@

Simpler framework at work!

-

<%= @time %>

+

<%= @request.env %>

- \ No newline at end of file + diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb new file mode 100644 index 00000000..f196d110 --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1,12 @@ + + + + + Index | Simpler application + + +

Simpler framework at work!

+ +

<%= render %>

+ + diff --git a/config.ru b/config.ru index 3060cc20..ed15acb4 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,4 @@ require_relative 'config/environment' - +require_relative 'lib/simpler_logger' +use SimplerLogger run Simpler.application diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..906aa9bb 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -22,6 +22,14 @@ def make_response(action) @response.finish end + def status(code) + @response.status = code + end + + def headers + @response.headers + end + private def extract_name @@ -43,7 +51,7 @@ def render_body end def params - @request.params + @request.env['simpler.params'].merge!(@request.params) end def render(template) diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..f68936c8 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -18,8 +18,10 @@ def post(path, route_point) def route_for(env) method = env['REQUEST_METHOD'].downcase.to_sym path = env['PATH_INFO'] - - @routes.find { |route| route.match?(method, path) } + env['simpler.params'] ||= {} + found_route = @routes.find { |route| route.match?(method, path) } + env['simpler.params'].merge!(found_route.params) if found_route + found_route end private diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 4c66b4b7..caf0f8b6 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -3,18 +3,33 @@ class Router class Route attr_reader :controller, :action + attr_accessor :params def initialize(method, path, controller, action) @method = method @path = path @controller = controller @action = action + @params = {} end def match?(method, path) - @method == method && path.match(@path) + @method == method && path_handler(path) end + def path_handler(path) + router_path = @path.split('/') + request = path.split('/') + return if request.size != router_path.size + + router_path.each_with_index do |part, id| + params[sym!(part)] = request[id] if part.include? ':' + end + end + + def sym!(part) + part.delete(':').to_sym + end end end end diff --git a/lib/simpler_logger.rb b/lib/simpler_logger.rb new file mode 100644 index 00000000..a4fdc4f5 --- /dev/null +++ b/lib/simpler_logger.rb @@ -0,0 +1,24 @@ +require 'logger' +class SimplerLogger + def initialize(app) + @app = app + end + + def call(env) + @request = Rack::Request.new(env) + status, headers, body = @app.call(env) + log = log_format(@request, status, headers) + logger = Logger.new('log/app.log') + logger.info("\n#{log}") + [status, headers, body] + end + + private + + def log_format(request, status, headers) + "Request: #{request.request_method} #{request.fullpath}\n" \ + "Handler: #{request.env['simpler.controller'].class}##{request.env['simpler.action']}\n" \ + "Parameters: #{request.env['simpler.params'].merge!(request.params)}\n" \ + "Response: #{status} #{headers['Content-Type']} #{request.env['simpler.render_view']}\n" \ + end +end diff --git a/log/app.log b/log/app.log new file mode 100644 index 00000000..b2292d16 --- /dev/null +++ b/log/app.log @@ -0,0 +1,12 @@ +I, [2021-02-24T17:06:40.170524 #44465] INFO -- : +Request: GET /tests/101?path=101 +Handler: TestsController#show +Parameters: {:id=>"101", "path"=>"101"} +Response: 200 text/html + +I, [2021-02-24T17:06:40.359602 #44465] INFO -- : +Request: GET /favicon.ico +Handler: TestsController#index +Parameters: {} +Response: 200 text/html + From 092c0a12f485345a52fc18b6eff5ebd1662cc9f7 Mon Sep 17 00:00:00 2001 From: ksilex Date: Thu, 25 Feb 2021 15:09:38 +0300 Subject: [PATCH 3/4] renderers ,fixes --- app/controllers/tests_controller.rb | 2 +- app/views/tests/index.html.erb | 2 +- app/views/tests/show.html.erb | 3 +-- lib/simpler/application.rb | 16 +++++++++++----- lib/simpler/controller.rb | 4 ++-- lib/simpler/router.rb | 8 ++++---- lib/simpler/router/route.rb | 12 ++++++++---- lib/simpler/view.rb | 21 +++++++++++++++++---- lib/simpler_logger.rb | 16 ++++++++++++---- log/app.log | 12 ------------ 10 files changed, 57 insertions(+), 39 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 479168e0..6e969564 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -9,7 +9,7 @@ def create end def show - @time = "dd" + render plain: params[:id] end end diff --git a/app/views/tests/index.html.erb b/app/views/tests/index.html.erb index 5bc1eeb1..0915cef4 100644 --- a/app/views/tests/index.html.erb +++ b/app/views/tests/index.html.erb @@ -7,6 +7,6 @@

Simpler framework at work!

-

<%= @request.env %>

+

<%= @time %>

diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb index f196d110..4d7ab57c 100644 --- a/app/views/tests/show.html.erb +++ b/app/views/tests/show.html.erb @@ -2,11 +2,10 @@ - Index | Simpler application + Show Page | Simpler application

Simpler framework at work!

-

<%= render %>

diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 711946a9..fa82e0c4 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -27,11 +27,13 @@ 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 + error + end end private @@ -54,5 +56,9 @@ def make_response(controller, action) controller.make_response(action) end + def error + [404, {'Content-Type' => 'text/plain'}, ['404: Page not found']] + end + end end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 906aa9bb..ee688fc2 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -1,3 +1,4 @@ +require 'json' require_relative 'view' module Simpler @@ -47,7 +48,7 @@ def write_response end def render_body - View.new(@request.env).render(binding) + View.new(@request.env, @response).render(binding) end def params @@ -57,6 +58,5 @@ def params def render(template) @request.env['simpler.template'] = template end - end end diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index f68936c8..8177a662 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -18,10 +18,10 @@ def post(path, route_point) def route_for(env) method = env['REQUEST_METHOD'].downcase.to_sym path = env['PATH_INFO'] - env['simpler.params'] ||= {} - found_route = @routes.find { |route| route.match?(method, path) } - env['simpler.params'].merge!(found_route.params) if found_route - found_route + if found_route = @routes.find { |route| route.match?(method, path) } + env['simpler.params'] = found_route.params + found_route + end end private diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index caf0f8b6..9b5f9a56 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -1,9 +1,7 @@ module Simpler class Router class Route - - attr_reader :controller, :action - attr_accessor :params + attr_reader :controller, :action, :params def initialize(method, path, controller, action) @method = method @@ -23,10 +21,16 @@ def path_handler(path) return if request.size != router_path.size router_path.each_with_index do |part, id| - params[sym!(part)] = request[id] if part.include? ':' + if part.include?(':') && request[id].to_i.positive? + params[sym!(part)] = request[id].to_i + elsif part != request[id] + return + end end end + private + def sym!(part) part.delete(':').to_sym end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..631bd021 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -1,17 +1,20 @@ require 'erb' +require 'json' module Simpler class View VIEW_BASE_PATH = 'app/views'.freeze - def initialize(env) + def initialize(env, response) @env = env + @response = response end def render(binding) - template = File.read(template_path) + return send template&.keys&.first, template&.values&.first if template&.keys + template = File.read(template_path) ERB.new(template).result(binding) end @@ -30,10 +33,20 @@ def template end def template_path - path = template || [controller.name, action].join('/') - + path = [controller.name, action].join('/') + @env['simpler.template_path'] = "#{path}.html.erb" Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end + def plain(content) + @response.headers['Content-Type'] = 'text/plain' + content + end + + def json(content) + @response.headers['Content-Type'] = 'json' + content.to_json + end + end end diff --git a/lib/simpler_logger.rb b/lib/simpler_logger.rb index a4fdc4f5..fef361aa 100644 --- a/lib/simpler_logger.rb +++ b/lib/simpler_logger.rb @@ -7,18 +7,26 @@ def initialize(app) def call(env) @request = Rack::Request.new(env) status, headers, body = @app.call(env) - log = log_format(@request, status, headers) logger = Logger.new('log/app.log') - logger.info("\n#{log}") + if status == 404 + logger.error("\n#{log_error(@request, status, headers)}") + else + logger.info("\n#{log_info(@request, status, headers)}") + end [status, headers, body] end private - def log_format(request, status, headers) + def log_info(request, status, headers) "Request: #{request.request_method} #{request.fullpath}\n" \ "Handler: #{request.env['simpler.controller'].class}##{request.env['simpler.action']}\n" \ "Parameters: #{request.env['simpler.params'].merge!(request.params)}\n" \ - "Response: #{status} #{headers['Content-Type']} #{request.env['simpler.render_view']}\n" \ + "Response: #{status} #{headers['Content-Type']} #{request.env['simpler.template_path']}\n" \ + end + + def log_error(request, status, headers) + "Request: #{request.request_method} #{request.fullpath}\n" \ + "Response: #{status} #{headers['Content-Type']}\n" \ end end diff --git a/log/app.log b/log/app.log index b2292d16..e69de29b 100644 --- a/log/app.log +++ b/log/app.log @@ -1,12 +0,0 @@ -I, [2021-02-24T17:06:40.170524 #44465] INFO -- : -Request: GET /tests/101?path=101 -Handler: TestsController#show -Parameters: {:id=>"101", "path"=>"101"} -Response: 200 text/html - -I, [2021-02-24T17:06:40.359602 #44465] INFO -- : -Request: GET /favicon.ico -Handler: TestsController#index -Parameters: {} -Response: 200 text/html - From 92cb624c084a9a1ba5303340ef53f52cbc0f9e2f Mon Sep 17 00:00:00 2001 From: ksilex Date: Thu, 25 Feb 2021 16:06:02 +0300 Subject: [PATCH 4/4] fixes --- lib/simpler/controller.rb | 1 - lib/simpler/view.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index ee688fc2..e37831b2 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -1,4 +1,3 @@ -require 'json' require_relative 'view' module Simpler diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 631bd021..5e641554 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -12,7 +12,7 @@ def initialize(env, response) end def render(binding) - return send template&.keys&.first, template&.values&.first if template&.keys + return send template.keys.first, template.values.first if template&.keys template = File.read(template_path) ERB.new(template).result(binding)