From 18ecbedbee313a0e2ac1e05cd694705d51ef8090 Mon Sep 17 00:00:00 2001 From: Alexey Alyoshin Date: Sun, 3 Dec 2023 16:17:32 +0300 Subject: [PATCH 1/2] looks like plain text works --- lib/simpler/view.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..fb8c3391 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -10,13 +10,19 @@ def initialize(env) end def render(binding) - template = File.read(template_path) + template = check_plain || File.read(template_path) ERB.new(template).result(binding) end private + def check_plain + return nil if @env['simpler.template'].nil? + + @env['simpler.template'][:plain] + end + def controller @env['simpler.controller'] end From 9da93f824ccae70297a6abbbe1892cac0d91d5d3 Mon Sep 17 00:00:00 2001 From: Alexey Alyoshin Date: Fri, 8 Mar 2024 21:32:35 +0300 Subject: [PATCH 2/2] complete all tasks --- app/controllers/error_controller.rb | 7 +++++++ app/controllers/tests_controller.rb | 6 ++++++ app/views/tests/show.html.erb | 11 +++++++++++ config.ru | 2 ++ config/routes.rb | 1 + lib/simpler/application.rb | 11 +++++++---- lib/simpler/controller.rb | 16 +++++++++++++++- lib/simpler/router.rb | 7 +++++-- lib/simpler/router/route.rb | 8 ++++++-- log/app.log | 16 ++++++++++++++++ logger/logger.rb | 21 +++++++++++++++++++++ 11 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 app/controllers/error_controller.rb create mode 100644 app/views/tests/show.html.erb create mode 100644 log/app.log create mode 100644 logger/logger.rb diff --git a/app/controllers/error_controller.rb b/app/controllers/error_controller.rb new file mode 100644 index 00000000..980be0ee --- /dev/null +++ b/app/controllers/error_controller.rb @@ -0,0 +1,7 @@ +class ErrorController < Simpler::Controller + + def no_page + render plain: "Page not found" + status 404 + end +end \ No newline at end of file diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..3c0c4be6 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -2,10 +2,16 @@ class TestsController < Simpler::Controller def index @time = Time.now + render plain: "Text for render" + status 201 + headers['Content-Type'] = 'text/plain' end def create + end + def show + render plain: "id #{@request.env[:id]}" end end diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb new file mode 100644 index 00000000..9f6b7029 --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1,11 @@ + + + + + Show | Simpler application + + +

Simpler framework at work!

+

<%= @time %>

+ + \ No newline at end of file diff --git a/config.ru b/config.ru index 3060cc20..9fb7191c 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,5 @@ require_relative 'config/environment' +require_relative 'logger/logger' +use Applogger 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/simpler/application.rb b/lib/simpler/application.rb index 711946a9..a9328a8a 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -28,10 +28,13 @@ def routes(&block) def call(env) route = @router.route_for(env) - controller = route.controller.new(env) - action = route.action - - make_response(controller, action) + begin + controller = route.controller.new(env) + action = route.action + make_response(controller, action) + rescue => e + make_response(ErrorController.new(env), 'no_page') + end end private diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..7cb5281c 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -13,7 +13,9 @@ def initialize(env) def make_response(action) @request.env['simpler.controller'] = self + @request.env['simpler.controllername'] = self.class.name @request.env['simpler.action'] = action + @request.env[:id] = get_id(@request.env) set_default_headers send(action) @@ -24,6 +26,18 @@ def make_response(action) private + def get_id(env) + env['PATH_INFO'].match('\d+').to_s.to_i + end + + def headers + @response + end + + def status(status) + @response.status = status + end + def extract_name self.class.name.match('(?.+)Controller')[:name].downcase end @@ -49,6 +63,6 @@ def params def render(template) @request.env['simpler.template'] = template end - end + end diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..4ecd64e4 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -18,12 +18,15 @@ def post(path, route_point) 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) } end private + def no_route + Route.new('get', '/no_page', ErrorController, 'no_page') + end + def add_route(method, path, route_point) route_point = route_point.split('#') controller = controller_from_string(route_point[0]) diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 4c66b4b7..d9cfc04c 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -6,13 +6,17 @@ class Route def initialize(method, path, controller, action) @method = method - @path = path + @path = path_for_id(path) @controller = controller @action = action end + def path_for_id(path) + path.gsub(/:id/, '\d+') + end + def match?(method, path) - @method == method && path.match(@path) + @method == method && path.match(@path + '\z') end end diff --git a/log/app.log b/log/app.log new file mode 100644 index 00000000..b85c8f6b --- /dev/null +++ b/log/app.log @@ -0,0 +1,16 @@ +I, [2024-03-08T20:57:11.299500 #42217] INFO -- request: GET/tests/312 +I, [2024-03-08T20:57:11.303658 #42217] INFO -- Handler: TestsController +I, [2024-03-08T20:57:11.303703 #42217] INFO -- Parameter: #show +I, [2024-03-08T20:57:11.303720 #42217] INFO -- Responce: 200 OK [text/html] id 312 +I, [2024-03-08T20:57:15.189928 #42217] INFO -- request: GET/tests/303 +I, [2024-03-08T20:57:15.190177 #42217] INFO -- Handler: TestsController +I, [2024-03-08T20:57:15.190203 #42217] INFO -- Parameter: #show +I, [2024-03-08T20:57:15.190219 #42217] INFO -- Responce: 200 OK [text/html] id 303 +I, [2024-03-08T20:57:19.222804 #42217] INFO -- request: GET/tests/500 +I, [2024-03-08T20:57:19.223113 #42217] INFO -- Handler: TestsController +I, [2024-03-08T20:57:19.223143 #42217] INFO -- Parameter: #show +I, [2024-03-08T20:57:19.223162 #42217] INFO -- Responce: 200 OK [text/html] id 500 +I, [2024-03-08T20:57:23.190205 #42217] INFO -- request: GET/tests/999 +I, [2024-03-08T20:57:23.190521 #42217] INFO -- Handler: TestsController +I, [2024-03-08T20:57:23.190555 #42217] INFO -- Parameter: #show +I, [2024-03-08T20:57:23.190575 #42217] INFO -- Responce: 200 OK [text/html] id 999 diff --git a/logger/logger.rb b/logger/logger.rb new file mode 100644 index 00000000..56a69ee5 --- /dev/null +++ b/logger/logger.rb @@ -0,0 +1,21 @@ +require 'logger' + +class Applogger + + def initialize(app) + file = File.open(File.expand_path(File.dirname(__dir__)) + '/log/app.log', File::WRONLY | File::APPEND | File::CREAT) + @logger = Logger.new(file) + @app = app + end + + def call(env) + @logger.info('request'){ env['REQUEST_METHOD'] + env['PATH_INFO']} + responce = @app.call(env) + + @logger.info('Handler'){env['simpler.controllername']} + @logger.info('Parameter'){env['QUERY_STRING'] + '#' + env['simpler.action']} + @logger.info('Responce'){responce[0].to_s + ' ' + Rack::Utils::HTTP_STATUS_CODES[responce[0]] + ' [' + responce[1]['Content-Type'] + '] ' + responce[2][0]} + responce + end + +end \ No newline at end of file