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/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 new file mode 100644 index 00000000..a981dfba --- /dev/null +++ b/app/views/tests/question_show.html.erb @@ -0,0 +1 @@ +

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

diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb new file mode 100644 index 00000000..d896d20d --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1 @@ +

Test #<%= @test %>

diff --git a/config.ru b/config.ru index 3060cc20..14e1072c 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,5 @@ require_relative 'config/environment' +require_relative "middlewares/logger" +use Log, logdev: File.expand_path("log/app.log", __dir__) 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..5bb9dc30 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,11 +64,15 @@ 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 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/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 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