From a435d1aa46d6c02f60921947d7dee3f9e9b01cee Mon Sep 17 00:00:00 2001 From: MrSaharok Date: Mon, 13 Nov 2023 22:00:14 +0700 Subject: [PATCH 1/2] Simpler --- app/controllers/tests_controller.rb | 5 ++++ config.ru | 2 ++ config/routes.rb | 1 + lib/simpler.rb | 1 - lib/simpler/application.rb | 3 +++ lib/simpler/controller.rb | 40 ++++++++++++++++++++++++++++- lib/simpler/router.rb | 10 ++++++++ lib/simpler/router/route.rb | 19 +++++++++++++- lib/simpler/view.rb | 1 - middleware/logger.rb | 34 ++++++++++++++++++++++++ 10 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 middleware/logger.rb diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..3f953149 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -4,6 +4,11 @@ def index @time = Time.now end + + def show + render plain: params['id'] + end + def create end diff --git a/config.ru b/config.ru index 3060cc20..e065e105 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,5 @@ require_relative 'config/environment' +require_relative 'middleware/logger' +use Logger run Simpler.application diff --git a/config/routes.rb b/config/routes.rb index 4a751251..da59b495 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Simpler.application.routes do get '/tests', 'tests#index' + get '/tests/:id', 'tests#show' post '/tests', 'tests#create' end diff --git a/lib/simpler.rb b/lib/simpler.rb index f9dfe3c4..70384ae0 100644 --- a/lib/simpler.rb +++ b/lib/simpler.rb @@ -12,5 +12,4 @@ def root Pathname.new(File.expand_path('..', __dir__)) end end - end diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 711946a9..37445c26 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -28,6 +28,9 @@ def routes(&block) def call(env) route = @router.route_for(env) + return @router.not_found(env) unless route + + route.extract_params(env) controller = route.controller.new(env) action = route.action diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..b6b47ad0 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -8,7 +8,9 @@ class Controller def initialize(env) @name = extract_name @request = Rack::Request.new(env) + @request.params.merge!(env['params']) @response = Rack::Response.new + @rendered = false end def make_response(action) @@ -17,7 +19,7 @@ def make_response(action) set_default_headers send(action) - write_response + write_response unless @rendered @response.finish end @@ -36,6 +38,7 @@ def write_response body = render_body @response.write(body) + @rendered = true end def render_body @@ -47,8 +50,43 @@ def params end def render(template) + case template + when String + render_string(template) + when Hash + render_hash(template) + else + render_error("Template rendering error #{template.class}") + end + @rendered = true + end + + def render_string(template) @request.env['simpler.template'] = template end + def render_error(error_message) + status 500 + @response.write(error_message) + @response.finish + end + + def render_hash(params) + if params.key?(:plain) + @request.env['simpler.render'] = :plain + @response.write(params[:plain].to_s) + @response.finish + else + render_error("The template with #{params.keys} option cannot be displayed") + end + end + + def status(status) + response.status = status + end + + def headers + @response.headers + end end end diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..99b58dfd 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -22,6 +22,16 @@ def route_for(env) @routes.find { |route| route.match?(method, path) } end + def not_found(env) + method = env['REQUEST_METHOD'].downcase.to_sym + path = env['PATH_INFO'] + Rack::Response.new.then do |response| + response.status = 404 + response.write("[#{method} #{path}] route not found\n") + response.finish + end + end + private def add_route(method, path, route_point) diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 4c66b4b7..adc5b0cf 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -12,9 +12,26 @@ def initialize(method, path, controller, action) end def match?(method, path) - @method == method && path.match(@path) + @method == method && path.match(to_regex(@path)) end + def extract_params(env) + request_path_items = env['PATH_INFO'].split('/') + path_items = @path.split('/').map { |item| item.sub(':', '') } + + params = Hash[path_items.zip(request_path_items)].delete_if { |k, v| k == v } + env['params'] = params + end + + private + + def to_regex(path) + path.split('/') + .map { |string| string.start_with?(':') ? '\d+' : string } + .join('/') + .concat('$') + .then { |regex| Regexp.new regex } + end end end end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..ee82679b 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -34,6 +34,5 @@ def template_path Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end - end end diff --git a/middleware/logger.rb b/middleware/logger.rb new file mode 100644 index 00000000..a7652e4f --- /dev/null +++ b/middleware/logger.rb @@ -0,0 +1,34 @@ +class Logger + def initialize(app) + @app = app + end + + def call(env) + @env = env + status, headers, body = @app.call(env) + + log_content = collect_logs(status, headers, body) + log(log_content) + + [status, headers, body] + end + + def log(content) + filepath = File.expand_path('log/log.txt') + write_mode = File.exist?(filepath) ? 'a' : 'w' + File.write(filepath, content, mode: write_mode) + end + + def collect_logs(status, headers, _body) + controller = @env['simpler.controller'].class.name + action = @env['simpler.action'] + template = @env['simpler.template'] || @env['simpler.render'] + parameters = @env['params'] + """ + Request: #{@env['REQUEST_METHOD']} + Handler: #{controller}##{action} + Parameters: #{parameters} + Response: #{status} [#{headers['Content-Type']}] #{template} + """ + end +end From 00e535a89f63a64d2a3efddab78b45003a12f809 Mon Sep 17 00:00:00 2001 From: MrSaharok Date: Mon, 13 Nov 2023 22:01:59 +0700 Subject: [PATCH 2/2] fix --- app/controllers/tests_controller.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 3f953149..19d5e991 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -4,7 +4,6 @@ def index @time = Time.now end - def show render plain: params['id'] end @@ -12,5 +11,4 @@ def show def create end - end