diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..78fdb7ab 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -5,7 +5,10 @@ def index end def create + end + def show + @test = Test.find(id: params[:id]) end end diff --git a/app/log/app.log b/app/log/app.log new file mode 100644 index 00000000..8d254994 --- /dev/null +++ b/app/log/app.log @@ -0,0 +1,16 @@ +I, [2022-10-25T22:54:40.913440 #10948] INFO -- : Request: GET / +I, [2022-10-25T22:54:40.913805 #10948] INFO -- : Handler: NilClass # +I, [2022-10-25T22:54:40.913904 #10948] INFO -- : Parameters: +I, [2022-10-25T22:54:40.913981 #10948] INFO -- : 404 [text/plain] +I, [2022-10-25T22:54:41.824050 #10948] INFO -- : Request: GET /favicon.ico +I, [2022-10-25T22:54:41.824392 #10948] INFO -- : Handler: NilClass # +I, [2022-10-25T22:54:41.824493 #10948] INFO -- : Parameters: +I, [2022-10-25T22:54:41.824898 #10948] INFO -- : 404 [text/plain] +I, [2022-10-25T22:54:48.787383 #10948] INFO -- : Request: GET /tests +I, [2022-10-25T22:54:48.787683 #10948] INFO -- : Handler: TestsController # index +I, [2022-10-25T22:54:48.787848 #10948] INFO -- : Parameters: {} +I, [2022-10-25T22:54:48.788159 #10948] INFO -- : 200 [text/html] tests/index.html.erb +I, [2022-10-25T22:55:15.034593 #10948] INFO -- : Request: GET /tests/1 +I, [2022-10-25T22:55:15.034882 #10948] INFO -- : Handler: TestsController # show +I, [2022-10-25T22:55:15.034998 #10948] INFO -- : Parameters: {:id=>"1"} +I, [2022-10-25T22:55:15.035067 #10948] INFO -- : 200 [text/html] tests/show.html.erb diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb new file mode 100644 index 00000000..ebb18530 --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1,11 @@ + + +
+ +<%= @test.level %>
+ + diff --git a/config.ru b/config.ru index 3060cc20..f6e89c5d 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,5 @@ require_relative 'config/environment' +require_relative 'lib/middleware/applogger' +use AppLogger, logdev: File.expand_path('app/log/app.log', __dir__) run Simpler.application 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/middleware/applogger.rb b/lib/middleware/applogger.rb new file mode 100644 index 00000000..f6a76252 --- /dev/null +++ b/lib/middleware/applogger.rb @@ -0,0 +1,30 @@ +require 'logger' + +class AppLogger + def initialize(app, **options) + @logger = Logger.new(options[:logdev] || STDOUT) + @app = app + end + + def call(env) + @env = env + response = @app.call(env) + + request_log + response_log(response) + + response + end + + def request_log + @logger.info "Request: #{@env['REQUEST_METHOD']} #{@env['REQUEST_URI']}" + @logger.info "Handler: #{@env['simpler.controller'].class} # #{@env['simpler.action']}" + @logger.info "Parameters: #{@env['simpler.params']}" + end + + def response_log(response) + template_path = @env['simpler.template_path'] ? "#{@env['simpler.template_path']}.html.erb" : '' + @logger.info "#{response[0]} [#{response[1]['Content-Type']}] #{template_path}" + end + +end diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 711946a9..e0c0cfb3 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -28,6 +28,7 @@ def routes(&block) def call(env) route = @router.route_for(env) + return not_found_response if route.nil? controller = route.controller.new(env) action = route.action @@ -54,5 +55,9 @@ def make_response(controller, action) controller.make_response(action) end + def not_found_response + Rack::Response.new('Page not found', 404, { 'Content-Type' => 'text/plain' }).finish + end + end end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..81a75a29 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -39,15 +39,32 @@ def write_response end def render_body - View.new(@request.env).render(binding) + @request.env['simpler.plain_text'] || View.new(@request.env).render(binding) end def params - @request.params + @request.params.merge(@request.env['simpler.params']) + end + + def status(code) + @response.status = code + end + + def headers(key, value) + @response[key] = value end def render(template) - @request.env['simpler.template'] = template + if template[:plain] + render_plain_text(template[:plain]) + else + @request.env['simpler.template'] = template + end + end + + def render_plain_text(text) + @response['Content-Type'] = 'text/plain' + @request.env['simpler.plain_text'] = text end end diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..9ec4adc0 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -19,7 +19,9 @@ def route_for(env) method = env['REQUEST_METHOD'].downcase.to_sym path = env['PATH_INFO'] - @routes.find { |route| route.match?(method, path) } + route = @routes.find { |route| route.match?(method, path) } + route&.set_params(env, path) + route end private diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 4c66b4b7..0cace0d2 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -6,13 +6,25 @@ class Route def initialize(method, path, controller, action) @method = method - @path = path + @path = path_regexp(path) @controller = controller @action = action end def match?(method, path) - @method == method && path.match(@path) + @method == method && path =~ @path + end + + def set_params(env, path) + env['simpler.params'] = @path.match(path).named_captures.transform_keys!(&:to_sym) + end + + def path_regexp(path) + regex = path.split('/'). + map { |part| part.start_with?(':') ? "(?<#{part.delete_prefix(':')}>\\w+)" : part }. + join('/') + + Regexp.new('^' + regex + '$') end end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..1254621d 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -31,7 +31,7 @@ def template def template_path path = template || [controller.name, action].join('/') - + @env['simpler.template_path'] = path Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end