From 3fb606aee8aaf7fef194fbfceb8a502d1713bb14 Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Fri, 24 Mar 2023 14:18:53 +0300 Subject: [PATCH 01/15] add method headers and status --- lib/simpler/controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..40e41084 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -24,6 +24,15 @@ def make_response(action) private + def status(code) + @response.status = code + end + + def headers + @response + end + + def extract_name self.class.name.match('(?.+)Controller')[:name].downcase end From af7cdd1c3e533c69fb4024b0371ed626d82d559c Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sat, 25 Mar 2023 00:17:10 +0300 Subject: [PATCH 02/15] add rout adn view 'show', extending the "render" method to return a response in other formats, rubocop work, require gems 'json' and 'active_support/all' --- app/controllers/tests_controller.rb | 7 ++----- app/models/test.rb | 1 - app/views/tests/show.html.erb | 1 + config/routes.rb | 1 + lib/simpler.rb | 2 -- lib/simpler/application.rb | 2 -- lib/simpler/controller.rb | 24 +++++++++++++++++++++--- lib/simpler/router.rb | 2 -- lib/simpler/router/route.rb | 2 -- lib/simpler/view.rb | 8 +++++--- 10 files changed, 30 insertions(+), 20 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 1526a689..06961f9f 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,11 +1,8 @@ class TestsController < Simpler::Controller - def index @time = Time.now + render plain: { a: 'a', b: 'b' } end - def create - - end - + def create; 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..a25e51fa --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1 @@ +

sadsaddsa

diff --git a/config/routes.rb b/config/routes.rb index 4a751251..347c4041 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Simpler.application.routes do + get '/tests/:id', 'tests#show' get '/tests', 'tests#index' post '/tests', 'tests#create' end diff --git a/lib/simpler.rb b/lib/simpler.rb index f9dfe3c4..d4d365c9 100644 --- a/lib/simpler.rb +++ b/lib/simpler.rb @@ -2,7 +2,6 @@ require_relative 'simpler/application' module Simpler - class << self def application Application.instance @@ -12,5 +11,4 @@ def root Pathname.new(File.expand_path('..', __dir__)) end end - end diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 711946a9..0d970a21 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -6,7 +6,6 @@ module Simpler class Application - include Singleton attr_reader :db @@ -53,6 +52,5 @@ def setup_database def make_response(controller, action) controller.make_response(action) end - end end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 40e41084..253e49c1 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -1,8 +1,9 @@ require_relative 'view' +require 'active_support/all' +require 'json' module Simpler class Controller - attr_reader :name, :request, :response def initialize(env) @@ -32,7 +33,6 @@ def headers @response end - def extract_name self.class.name.match('(?.+)Controller')[:name].downcase end @@ -55,9 +55,27 @@ def params @request.params end + def format_response(hash) + @request.env['simpler.body'] = if hash[:json] + headers['Content-Type'] = 'text/json' + hash[:json].to_json + elsif hash[:xml] + headers['Content-Type'] = 'text/xml' + hash[:xml].to_xml + elsif hash[:plain] + headers['Content-Type'] = 'text/plain' + hash[:plain].to_s + elsif hash[:inline] + hash[:inline] + else + 'Unknown format' + end + end + def render(template) + format_response(template) if template.instance_of?(Hash) + @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..ea36cbe4 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) @@ -14,7 +13,6 @@ def initialize(method, path, controller, action) def match?(method, path) @method == method && path.match(@path) end - end end end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..7d34f6b1 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) @@ -10,13 +9,17 @@ def initialize(env) end def render(binding) - template = File.read(template_path) + template = render_body || File.read(template_path) ERB.new(template).result(binding) end private + def render_body + @env['simpler.body'] + end + def controller @env['simpler.controller'] end @@ -34,6 +37,5 @@ def template_path Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end - end end From 5ab58c80e72a5a50590b1489f42c427cc6ab02f6 Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sat, 25 Mar 2023 00:42:02 +0300 Subject: [PATCH 03/15] change action title and implementation of the method of throwing an error on an incorrect path --- app/controllers/tests_controller.rb | 2 +- lib/simpler/application.rb | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 06961f9f..ad00f75e 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -4,5 +4,5 @@ def index render plain: { a: 'a', b: 'b' } end - def create; end + def show; end end diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 0d970a21..baf25af2 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -27,14 +27,27 @@ def routes(&block) def call(env) route = @router.route_for(env) - controller = route.controller.new(env) - action = route.action + if route + controller = route.controller.new(env) + action = route.action - make_response(controller, action) + make_response(controller, action) + else + response_not_found + end end private + def response_not_found + response = Rack::Response.new + + response.status = 404 + response['Content-Type'] = 'text/plain' + response.body = ['Not Found'] + response.finish + end + def require_app Dir["#{Simpler.root}/app/**/*.rb"].each { |file| require file } end From 68c1af6dc887bb9e5881ed5817910efb189a4b14 Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sat, 25 Mar 2023 01:29:55 +0300 Subject: [PATCH 04/15] removed the render method call from the TestsController class --- app/controllers/tests_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index ad00f75e..516bc580 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,7 +1,6 @@ class TestsController < Simpler::Controller def index @time = Time.now - render plain: { a: 'a', b: 'b' } end def show; end From 9c7b0808765cc654b5b994375c4c705bd90d5ab5 Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sat, 25 Mar 2023 01:31:59 +0300 Subject: [PATCH 05/15] created AppLogger class in middleware folder --- lib/middleware/applogger.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 lib/middleware/applogger.rb diff --git a/lib/middleware/applogger.rb b/lib/middleware/applogger.rb new file mode 100644 index 00000000..fe53c9fa --- /dev/null +++ b/lib/middleware/applogger.rb @@ -0,0 +1,9 @@ +class AppLogger + def initialize(app) + @app = app + end + + def call(env) + status, headers, body = @app.call(env) + end +end From 15574797562f2f29e7a030a890f85664c1452ca3 Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sat, 25 Mar 2023 01:33:41 +0300 Subject: [PATCH 06/15] created a method in the Application class to call the AppLogger connection --- lib/simpler/application.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index baf25af2..0c9ed759 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) @@ -56,6 +57,10 @@ def require_routes require Simpler.root.join('config/routes') end + def require_logger + require Simpler.root.join('lib/middleware/applogger') + end + def setup_database database_config = YAML.load_file(Simpler.root.join('config/database.yml')) database_config['database'] = Simpler.root.join(database_config['database']) From bbcb53568cae0422d0158169ba3835736c209ddb Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sat, 25 Mar 2023 01:34:40 +0300 Subject: [PATCH 07/15] connecting AppLogger in the config.ru file --- config.ru | 1 + 1 file changed, 1 insertion(+) diff --git a/config.ru b/config.ru index 3060cc20..67b662e4 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,4 @@ require_relative 'config/environment' +use AppLogger run Simpler.application From e79e94396bb3853f8a7c23fe8eff3e65298f854f Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sat, 25 Mar 2023 02:07:30 +0300 Subject: [PATCH 08/15] specifying the path to create a app.log file --- config.ru | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.ru b/config.ru index 67b662e4..5ac7059e 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,4 @@ require_relative 'config/environment' -use AppLogger +use AppLogger, logdev: File.expand_path('log/app.log', __dir__) run Simpler.application From 8933ff611a36126be4e590db7fb6929f16a569c9 Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sat, 25 Mar 2023 02:08:45 +0300 Subject: [PATCH 09/15] added a parameter to the applogger constructor to work with the logger object --- lib/middleware/applogger.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/middleware/applogger.rb b/lib/middleware/applogger.rb index fe53c9fa..37fc9dd4 100644 --- a/lib/middleware/applogger.rb +++ b/lib/middleware/applogger.rb @@ -1,9 +1,12 @@ class AppLogger - def initialize(app) + def initialize(app, **options) + @logger = Logger.new(options[:logdev] || STDOUT) @app = app end def call(env) status, headers, body = @app.call(env) + + [status, headers, [body]] end end From b524e94b4dc4b405a361d42e230634ffb08c12cf Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sun, 26 Mar 2023 00:54:39 +0300 Subject: [PATCH 10/15] instance variable definition --- app/controllers/tests_controller.rb | 4 +++- app/views/tests/show.html.erb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 516bc580..9439113d 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -3,5 +3,7 @@ def index @time = Time.now end - def show; end + def show + @params = params[:id] + end end diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb index a25e51fa..bcc4780e 100644 --- a/app/views/tests/show.html.erb +++ b/app/views/tests/show.html.erb @@ -1 +1 @@ -

sadsaddsa

+

<%= @params%>

From 2346f15631c72dfafbd137a5268eabab4a2e7d7c Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sun, 26 Mar 2023 00:59:38 +0300 Subject: [PATCH 11/15] creation of middleware for logging HTTP requests and responses --- .gitignore | 2 ++ lib/middleware/applogger.rb | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..df89b950 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +log/app.log \ No newline at end of file diff --git a/lib/middleware/applogger.rb b/lib/middleware/applogger.rb index 37fc9dd4..7d073c6c 100644 --- a/lib/middleware/applogger.rb +++ b/lib/middleware/applogger.rb @@ -7,6 +7,25 @@ def initialize(app, **options) def call(env) status, headers, body = @app.call(env) - [status, headers, [body]] + request = Rack::Request.new(env) + + request_info(request) + responce_info(status, headers, request) + + [status, headers, ["#{body}"]] + end + + def request_info(request) + log = "Request: #{request.env["REQUEST_METHOD"]} #{request.env["REQUEST_URI"]} + Handler: #{request.env['simpler.controller'].class.name}##{request.env['simpler.action']} + Parameters: #{request.params}" + + @logger.info(log) + end + + def responce_info(status, headers, request) + log = "Response: #{status} #{headers["Content-Type"]} #{request.env['simpler.file_path']} " + + @logger.info(log) if request.env['simpler.file_path'] end end From 2c0090277eb4861d4ad378f56c83eb0686a7f6fd Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sun, 26 Mar 2023 00:59:54 +0300 Subject: [PATCH 12/15] defining a template route to display in the log --- lib/simpler/view.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 7d34f6b1..541455ce 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -32,7 +32,15 @@ def template @env['simpler.template'] end + def template_file_path + if controller.name + @env['simpler.file_path'] = [controller.name, action].join('/') + '.html.erb' + end + end + def template_path + template_file_path + path = template || [controller.name, action].join('/') Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") From b233b94d919d74545dd099a69b10159e76513821 Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sun, 26 Mar 2023 01:22:06 +0300 Subject: [PATCH 13/15] enhancement of route method to call template :show --- lib/simpler/router/route.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index ea36cbe4..615a654b 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -11,7 +11,7 @@ def initialize(method, path, controller, action) end def match?(method, path) - @method == method && path.match(@path) + @method == method && path.gsub(/\d/, ':id').match(@path) end end end From b7ed8fd6df28b986927f493c58a51b4b1db16bd3 Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sun, 26 Mar 2023 01:38:46 +0300 Subject: [PATCH 14/15] route method improvement --- lib/simpler/router/route.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 615a654b..dec387a3 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -11,7 +11,8 @@ def initialize(method, path, controller, action) end def match?(method, path) - @method == method && path.gsub(/\d/, ':id').match(@path) + primary_key = @path.split('/')[-1] + @method == method && path.gsub(/\d/, primary_key).match(@path) end end end From 18075d8c15ee4269db25546e9c35b17ab5dc604d Mon Sep 17 00:00:00 2001 From: Tavbulatov Date: Sun, 26 Mar 2023 02:16:40 +0300 Subject: [PATCH 15/15] implementation of assignment of parameters and reading parameters in the 'view' and in the log --- lib/middleware/applogger.rb | 6 +++--- lib/simpler/controller.rb | 13 ++++++++++--- lib/simpler/view.rb | 6 +++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/middleware/applogger.rb b/lib/middleware/applogger.rb index 7d073c6c..ddc75a1c 100644 --- a/lib/middleware/applogger.rb +++ b/lib/middleware/applogger.rb @@ -16,15 +16,15 @@ def call(env) end def request_info(request) - log = "Request: #{request.env["REQUEST_METHOD"]} #{request.env["REQUEST_URI"]} + log = "Request: #{request.env['REQUEST_METHOD']} #{request.env['REQUEST_URI']} Handler: #{request.env['simpler.controller'].class.name}##{request.env['simpler.action']} - Parameters: #{request.params}" + Parameters: #{request.env['simpler.params']}" @logger.info(log) end def responce_info(status, headers, request) - log = "Response: #{status} #{headers["Content-Type"]} #{request.env['simpler.file_path']} " + log = "Response: #{status} #{headers['Content-Type']} #{request.env['simpler.file_path']} " @logger.info(log) if request.env['simpler.file_path'] end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 253e49c1..492618f9 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -16,6 +16,8 @@ def make_response(action) @request.env['simpler.controller'] = self @request.env['simpler.action'] = action + parameter_assignment + set_default_headers send(action) write_response @@ -29,6 +31,13 @@ def status(code) @response.status = code end + def parameter_assignment + number = @request.env['PATH_INFO'].split('/').map(&:to_i).max + + @params ||= { id: number } + @request.env['simpler.params'] = @params + end + def headers @response end @@ -51,9 +60,7 @@ def render_body View.new(@request.env).render(binding) end - def params - @request.params - end + attr_reader :params def format_response(hash) @request.env['simpler.body'] = if hash[:json] diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 541455ce..d4362674 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -33,9 +33,9 @@ def template end def template_file_path - if controller.name - @env['simpler.file_path'] = [controller.name, action].join('/') + '.html.erb' - end + return unless controller.name + + @env['simpler.file_path'] = [controller.name, action].join('/') + '.html.erb' end def template_path