From 1612c9e8900870a53567bf0aef9d9df935f6515b Mon Sep 17 00:00:00 2001 From: Vitaliy Supron Date: Fri, 22 Dec 2023 18:59:52 +0300 Subject: [PATCH 1/6] add not found response --- lib/simpler/application.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 711946a9..81ac285f 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -3,6 +3,7 @@ require 'sequel' require_relative 'router' require_relative 'controller' +require 'pry' module Simpler class Application @@ -28,6 +29,9 @@ def routes(&block) def call(env) route = @router.route_for(env) + + return not_found if route.nil? + controller = route.controller.new(env) action = route.action @@ -54,5 +58,8 @@ def make_response(controller, action) controller.make_response(action) end + def not_found + [404, { 'Content-Type' => 'text/plain' }, ['Not found']] + end end end From 40ff5345cb6c3baf937a587599f5ab91ef452ff3 Mon Sep 17 00:00:00 2001 From: Vitaliy Supron Date: Wed, 17 Jan 2024 01:36:21 +0300 Subject: [PATCH 2/6] setting response status --- app/controllers/tests_controller.rb | 3 +++ app/views/tests/create.html.erb | 11 +++++++++++ lib/simpler/controller.rb | 5 ++++- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 app/views/tests/create.html.erb diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..0f05a478 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -2,10 +2,13 @@ class TestsController < Simpler::Controller def index @time = Time.now + + status 200 end def create + status 201 end end diff --git a/app/views/tests/create.html.erb b/app/views/tests/create.html.erb new file mode 100644 index 00000000..4cc14e6c --- /dev/null +++ b/app/views/tests/create.html.erb @@ -0,0 +1,11 @@ + + + + + Create | Simpler application + + +

Simpler framework at work!

+

<%= @time %>

+ + diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..7dc69b9e 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -1,4 +1,5 @@ require_relative 'view' +require 'pry' module Simpler class Controller @@ -18,7 +19,6 @@ def make_response(action) set_default_headers send(action) write_response - @response.finish end @@ -50,5 +50,8 @@ def render(template) @request.env['simpler.template'] = template end + def status(code) + @response.status = code + end end end From 363d988cea95e7c6c1823e2ebfbec4ac38d9d579 Mon Sep 17 00:00:00 2001 From: "vitaliy.supron" Date: Tue, 23 Jan 2024 22:13:30 +0300 Subject: [PATCH 3/6] implementation of setting headers in a controller method --- app/controllers/tests_controller.rb | 4 ++-- lib/simpler/controller.rb | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 0f05a478..b67b22d7 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -2,12 +2,12 @@ class TestsController < Simpler::Controller def index @time = Time.now - + render 'tests/list' status 200 end def create - + render plain: "txt" status 201 end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 7dc69b9e..7c668a44 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -29,7 +29,7 @@ def extract_name end def set_default_headers - @response['Content-Type'] = 'text/html' + headers['Content-Type'] = 'text/html' end def write_response @@ -47,9 +47,20 @@ def params end def render(template) + case template + when Hash + headers['Content-Type'] = 'text/plain' if template.has_key?(:plain) + else + set_default_headers + end + @request.env['simpler.template'] = template end + def headers + @response.headers + end + def status(code) @response.status = code end From 39a7556b8b0b69baa6979c61d559215cd5d72318 Mon Sep 17 00:00:00 2001 From: "vitaliy.supron" Date: Mon, 5 Feb 2024 17:49:53 +0300 Subject: [PATCH 4/6] add render plain add render plain add render plain --- lib/simpler/view.rb | 21 ++++++++++++++++----- lib/simpler/view/html_render.rb | 17 +++++++++++++++++ lib/simpler/view/plain_render.rb | 14 ++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 lib/simpler/view/html_render.rb create mode 100644 lib/simpler/view/plain_render.rb diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..1c1bc560 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -1,4 +1,5 @@ -require 'erb' +require_relative 'view/html_render' +require_relative 'view/plain_render' module Simpler class View @@ -10,9 +11,7 @@ def initialize(env) end def render(binding) - template = File.read(template_path) - - ERB.new(template).result(binding) + render_template.result(binding) end private @@ -29,11 +28,23 @@ def template @env['simpler.template'] end + def render_template + template = @env['simpler.template'] + + case template + when String + HtmlRender.new(template_path) + when Hash + PlainRender.new(template) if template.has_key?(:plain) + else + HtmlRender.new(template_path) + end + end + def template_path path = template || [controller.name, action].join('/') Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end - end end diff --git a/lib/simpler/view/html_render.rb b/lib/simpler/view/html_render.rb new file mode 100644 index 00000000..2e4f7f0e --- /dev/null +++ b/lib/simpler/view/html_render.rb @@ -0,0 +1,17 @@ +require 'erb' + +module Simpler + class View + + class HtmlRender + def initialize(template_path) + @path = template_path + end + + def result(binding) + template = File.read(@path) + ERB.new(template).result(binding) + end + end + end +end diff --git a/lib/simpler/view/plain_render.rb b/lib/simpler/view/plain_render.rb new file mode 100644 index 00000000..03e07abc --- /dev/null +++ b/lib/simpler/view/plain_render.rb @@ -0,0 +1,14 @@ +module Simpler + class View + + class PlainRender + def initialize(template) + @template = template + end + + def result(_binding) + @template[:plain] + end + end + end +end From 82acf2b8b92b7d4b7d84e3070e755daa80deefeb Mon Sep 17 00:00:00 2001 From: "vitaliy.supron" Date: Mon, 5 Feb 2024 21:11:52 +0300 Subject: [PATCH 5/6] add mechanism for parsing route parameters --- app/controllers/tests_controller.rb | 4 ++++ app/views/tests/list.html.erb | 5 ++--- config/routes.rb | 1 + lib/simpler/application.rb | 2 +- lib/simpler/controller.rb | 2 +- lib/simpler/router.rb | 3 ++- lib/simpler/router/route.rb | 23 +++++++++++++++++++++-- 7 files changed, 32 insertions(+), 8 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index b67b22d7..76d75cfe 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -11,4 +11,8 @@ def create status 201 end + def show + @test_id = params[:id] + render 'tests/list' + end end diff --git a/app/views/tests/list.html.erb b/app/views/tests/list.html.erb index 0d430491..91436166 100644 --- a/app/views/tests/list.html.erb +++ b/app/views/tests/list.html.erb @@ -6,7 +6,6 @@

Simpler framework at work!

- -

<%= @time %>

+

Test id: <%= @test_id %>

- \ No newline at end of file + 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/lib/simpler/application.rb b/lib/simpler/application.rb index 81ac285f..9db17386 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -31,7 +31,7 @@ def call(env) route = @router.route_for(env) return not_found if route.nil? - + # binding.pry controller = route.controller.new(env) action = route.action diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 7c668a44..ff50a27d 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -43,7 +43,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..ede25a15 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -1,4 +1,5 @@ require_relative 'router/route' +require 'pry' module Simpler class Router @@ -19,7 +20,7 @@ def route_for(env) method = env['REQUEST_METHOD'].downcase.to_sym path = env['PATH_INFO'] - @routes.find { |route| route.match?(method, path) } + @routes.find { |route| route.match?(method, path, env) } end private diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 4c66b4b7..49154271 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -11,8 +11,27 @@ def initialize(method, path, controller, action) @action = action end - def match?(method, path) - @method == method && path.match(@path) + def match?(method, path, env) + @method == method && parse_path(path, env) + end + + private + + def parse_path(path, env) + params = {} + request_path = path.split('/') + router_path = @path.split('/') + + return false if request_path.size != router_path.size + + router_path.each_with_index do |element, index| + if element.start_with?(':') + params[element[1..-1].to_sym] = request_path[index] + else + return false unless element == request_path[index] + end + end + env['simpler.params'] = params end end From 389a216c92af93bd9725c757e8ae6cc9b4ff68ad Mon Sep 17 00:00:00 2001 From: "vitaliy.supron" Date: Mon, 5 Feb 2024 22:26:24 +0300 Subject: [PATCH 6/6] add loger --- .gitignore | 1 + config.ru | 3 +++ lib/middleware/logger.rb | 39 +++++++++++++++++++++++++++++++++++++++ lib/simpler/view.rb | 2 +- 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 lib/middleware/logger.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..a8154340 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/log/* diff --git a/config.ru b/config.ru index 3060cc20..507d9054 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,6 @@ require_relative 'config/environment' +require_relative 'lib/middleware/logger' + +use AppLogger, logdev: File.expand_path('log/app.log', __dir__) run Simpler.application diff --git a/lib/middleware/logger.rb b/lib/middleware/logger.rb new file mode 100644 index 00000000..99d865b4 --- /dev/null +++ b/lib/middleware/logger.rb @@ -0,0 +1,39 @@ +require 'logger' + +class AppLogger + def initialize(app, **options) + @logger = Logger.new(options[:logdev] || $stdout) + @app = app + end + + def call(env) + status, headers, body = @app.call(env) + @logger.info(message(env, status, headers)) + [status, headers, body] + end + + def message(env, status, headers) + + controller = env['simpler.controller'] + + if controller + <<~LOGGER + + -------------------------------------------------------------- + Request: #{env['REQUEST_METHOD']} #{env['REQUEST_URI']} + Handler: #{env['simpler.controller'].class}##{env['simpler.action']} + Parameters: #{env['simpler.params']} + Response: #{status} [#{headers['Content-Type']}] #{env['simpler.template_path']} + + LOGGER + else + <<~LOGGER + + -------------------------------------------------------------- + Request: #{env['REQUEST_METHOD']} #{env['REQUEST_URI']} + Response: #{} + + LOGGER + end + end +end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 1c1bc560..e8e45d6e 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -43,7 +43,7 @@ def render_template def template_path path = template || [controller.name, action].join('/') - + @env['simpler.template_path'] = "#{path}.html.erb" Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end end