From e4f070d23501f85ec302275a30ac61fdce1bc264 Mon Sep 17 00:00:00 2001 From: Skvortsovvg Date: Fri, 19 Aug 2022 22:25:22 +0300 Subject: [PATCH 1/3] finish --- app/controllers/tests_controller.rb | 8 +++++ app/views/tests/question_show.html.erb | 1 + app/views/tests/show.html.erb | 1 + config.ru | 2 ++ config/routes.rb | 2 ++ lib/simpler/application.rb | 16 ++++++--- lib/simpler/controller.rb | 45 ++++++++++++++++++++------ lib/simpler/router.rb | 2 +- lib/simpler/router/route.rb | 14 ++++++-- lib/simpler/view.rb | 1 - middlewares/logger.rb | 21 ++++++++++++ 11 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 app/views/tests/question_show.html.erb create mode 100644 app/views/tests/show.html.erb create mode 100644 middlewares/logger.rb diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..3f49543c 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -4,6 +4,14 @@ def index @time = Time.now end + def show + @test = params[:id] + end + + def question_show + @test, @qst = params[:id], params[:question_id] + end + def create end diff --git a/app/views/tests/question_show.html.erb b/app/views/tests/question_show.html.erb new file mode 100644 index 00000000..da3c37d6 --- /dev/null +++ b/app/views/tests/question_show.html.erb @@ -0,0 +1 @@ +

Question #<%= @qst %> (test #<%= @test %>)

\ 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..d7991184 --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1 @@ +

Test #<%= @test %>

\ No newline at end of file diff --git a/config.ru b/config.ru index 3060cc20..aaa44e9c 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,5 @@ require_relative 'config/environment' +require_relative "middlewares/logger" +use Log run Simpler.application diff --git a/config/routes.rb b/config/routes.rb index 4a751251..1118e66a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Simpler.application.routes do get '/tests', 'tests#index' post '/tests', 'tests#create' + get '/tests/:id', 'tests#show' + get '/tests/:id/questions/:question_id', 'tests#question_show' end diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 711946a9..d7cb5978 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -27,10 +27,18 @@ def routes(&block) end def call(env) - route = @router.route_for(env) - controller = route.controller.new(env) - action = route.action - + route = @router.route_for(env) + if route + controller = route.controller.new(env, route.route_params) + action = route.action + else + controller = Controller.new(env) + end + + env['simpler.handler'] = "#{controller.name}##{action}" + env['simpler.params'] = controller.request.params + env['simpler.erb'] = "#{[controller.name, action].join('/')}.html.erb" + make_response(controller, action) end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..ffa498ea 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -5,19 +5,22 @@ class Controller attr_reader :name, :request, :response - def initialize(env) + def initialize(env, route_params = {}) @name = extract_name @request = Rack::Request.new(env) @response = Rack::Response.new + params.merge!(route_params) end def make_response(action) - @request.env['simpler.controller'] = self - @request.env['simpler.action'] = action - set_default_headers - send(action) - write_response + @request.env['simpler.route_error'] = true if !action + @request.env['simpler.controller'] = self + @request.env['simpler.action'] = action + + set_default_headers + send(action) if action + write_response @response.finish end @@ -32,8 +35,26 @@ def set_default_headers @response['Content-Type'] = 'text/html' end + def set_plain_text_header + @response['Content-Type'] = 'text/plain' + end + + def set_response_status(status = 200) + @response.status = status + end + def write_response - body = render_body + if @request.env['simpler.route_error'] + set_plain_text_header + set_response_status(404) + body = "No such page: #{@request.path}" + elsif @request.env['simpler.plain_text'] + set_plain_text_header + set_response_status(201) + body = @request.env['simpler.plain_text'] + else + body = render_body + end @response.write(body) end @@ -43,12 +64,16 @@ def render_body end def params - @request.params + @request.params end def render(template) - @request.env['simpler.template'] = template + if template.is_a?(Hash) + @request.env['simpler.plain_text'] = template[:plain] + else + @request.env['simpler.template'] = template + end end end -end +end \ No newline at end of file diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..c7717c6d 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -38,4 +38,4 @@ def controller_from_string(controller_name) end end -end +end \ No newline at end of file diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 4c66b4b7..58017585 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -2,17 +2,27 @@ module Simpler class Router class Route - attr_reader :controller, :action + attr_reader :controller, :action, :route_params def initialize(method, path, controller, action) @method = method @path = path @controller = controller @action = action + @route_params = {} end def match?(method, path) - @method == method && path.match(@path) + return false if @method != method + + sample = @path.split('/').map { |pth| pth.include?(':') ? pth[1...].to_sym : pth } + + if (path =~ /#{sample.map { |pth| pth.is_a?(Symbol) ? "[^\/]+" : pth }.join('\/')}[\/]*$/) + path = path.split('/') + sample.each_with_index { |v, i| @route_params[v] = path[i] if v.is_a?(Symbol) } + else + false + end end end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..302baa7a 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -11,7 +11,6 @@ def initialize(env) def render(binding) template = File.read(template_path) - ERB.new(template).result(binding) end diff --git a/middlewares/logger.rb b/middlewares/logger.rb new file mode 100644 index 00000000..e238bb4c --- /dev/null +++ b/middlewares/logger.rb @@ -0,0 +1,21 @@ +require 'logger' + +class Log + + def initialize(app, **options) + @logger = Logger.new(options[:logdev] || STDOUT) + @app = app + end + + def call(env) + + response = @app.call(env) + + @logger.info("Request: #{env['REQUEST_METHOD']} #{env['PATH_INFO']}") + @logger.info("Handler: #{env['simpler.handler']}") + @logger.info("Parameters: #{env['simpler.params']}") + @logger.info("Response: #{response[0]} [#{response[1]["Content-Type"]}] #{env['simpler.erb']}") + response + end + +end From 267f3a52fbd9c936d7948819add4769d7fb223fe Mon Sep 17 00:00:00 2001 From: Skvortsovvg Date: Fri, 19 Aug 2022 22:35:24 +0300 Subject: [PATCH 2/3] file log --- config.ru | 2 +- lib/simpler/controller.rb | 2 +- lib/simpler/router.rb | 2 +- log/app.log | 5 +++++ 4 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 log/app.log diff --git a/config.ru b/config.ru index aaa44e9c..14e1072c 100644 --- a/config.ru +++ b/config.ru @@ -1,5 +1,5 @@ require_relative 'config/environment' require_relative "middlewares/logger" -use Log +use Log, logdev: File.expand_path("log/app.log", __dir__) run Simpler.application diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index ffa498ea..5bb9dc30 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -76,4 +76,4 @@ def render(template) end end -end \ No newline at end of file +end diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index c7717c6d..14b3415c 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -38,4 +38,4 @@ def controller_from_string(controller_name) end end -end \ No newline at end of file +end diff --git a/log/app.log b/log/app.log new file mode 100644 index 00000000..658fd836 --- /dev/null +++ b/log/app.log @@ -0,0 +1,5 @@ +# Logfile created on 2022-08-19 22:34:26 +0300 by logger.rb/v1.5.0 +I, [2022-08-19T22:34:38.176146 #12180] INFO -- : Request: GET /tests/101 +I, [2022-08-19T22:34:38.176472 #12180] INFO -- : Handler: tests#show +I, [2022-08-19T22:34:38.176610 #12180] INFO -- : Parameters: {:id=>"101"} +I, [2022-08-19T22:34:38.176793 #12180] INFO -- : Response: 200 [text/html] tests/show.html.erb From 750c8db017540e907882f0ca5c29584430d93bbc Mon Sep 17 00:00:00 2001 From: Skvortsovvg Date: Fri, 19 Aug 2022 22:37:17 +0300 Subject: [PATCH 3/3] indents --- app/views/tests/index.html.erb | 2 +- app/views/tests/list.html.erb | 2 +- app/views/tests/question_show.html.erb | 2 +- app/views/tests/show.html.erb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/views/tests/index.html.erb b/app/views/tests/index.html.erb index 39fce580..0915cef4 100644 --- a/app/views/tests/index.html.erb +++ b/app/views/tests/index.html.erb @@ -9,4 +9,4 @@

<%= @time %>

- \ No newline at end of file + diff --git a/app/views/tests/list.html.erb b/app/views/tests/list.html.erb index 0d430491..33894ed7 100644 --- a/app/views/tests/list.html.erb +++ b/app/views/tests/list.html.erb @@ -9,4 +9,4 @@

<%= @time %>

- \ No newline at end of file + diff --git a/app/views/tests/question_show.html.erb b/app/views/tests/question_show.html.erb index da3c37d6..a981dfba 100644 --- a/app/views/tests/question_show.html.erb +++ b/app/views/tests/question_show.html.erb @@ -1 +1 @@ -

Question #<%= @qst %> (test #<%= @test %>)

\ No newline at end of file +

Question #<%= @qst %> (test #<%= @test %>)

diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb index d7991184..d896d20d 100644 --- a/app/views/tests/show.html.erb +++ b/app/views/tests/show.html.erb @@ -1 +1 @@ -

Test #<%= @test %>

\ No newline at end of file +

Test #<%= @test %>