diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..6e969564 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -8,4 +8,8 @@ def create end + def show + render plain: params[:id] + end + 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/show.html.erb b/app/views/tests/show.html.erb new file mode 100644 index 00000000..4d7ab57c --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1,11 @@ + + + + + Show Page | Simpler application + + +

Simpler framework at work!

+ + + diff --git a/config.ru b/config.ru index 3060cc20..ed15acb4 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,4 @@ require_relative 'config/environment' - +require_relative 'lib/simpler_logger' +use SimplerLogger 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..fa82e0c4 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -27,11 +27,13 @@ def routes(&block) end def call(env) - route = @router.route_for(env) - controller = route.controller.new(env) - action = route.action - - make_response(controller, action) + if route = @router.route_for(env) + controller = route.controller.new(env) + action = route.action + make_response(controller, action) + else + error + end end private @@ -54,5 +56,9 @@ def make_response(controller, action) controller.make_response(action) end + def error + [404, {'Content-Type' => 'text/plain'}, ['404: Page not found']] + end + end end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..e37831b2 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -22,6 +22,14 @@ def make_response(action) @response.finish end + def status(code) + @response.status = code + end + + def headers + @response.headers + end + private def extract_name @@ -39,16 +47,15 @@ def write_response end def render_body - View.new(@request.env).render(binding) + View.new(@request.env, @response).render(binding) end def params - @request.params + @request.env['simpler.params'].merge!(@request.params) end 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..8177a662 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -18,8 +18,10 @@ 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) } + if found_route = @routes.find { |route| route.match?(method, path) } + env['simpler.params'] = found_route.params + found_route + end end private diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 4c66b4b7..9b5f9a56 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -1,20 +1,39 @@ module Simpler class Router class Route - - attr_reader :controller, :action + attr_reader :controller, :action, :params def initialize(method, path, controller, action) @method = method @path = path @controller = controller @action = action + @params = {} end def match?(method, path) - @method == method && path.match(@path) + @method == method && path_handler(path) + end + + def path_handler(path) + router_path = @path.split('/') + request = path.split('/') + return if request.size != router_path.size + + router_path.each_with_index do |part, id| + if part.include?(':') && request[id].to_i.positive? + params[sym!(part)] = request[id].to_i + elsif part != request[id] + return + end + end end + private + + def sym!(part) + part.delete(':').to_sym + end end end end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..5e641554 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -1,17 +1,20 @@ require 'erb' +require 'json' module Simpler class View VIEW_BASE_PATH = 'app/views'.freeze - def initialize(env) + def initialize(env, response) @env = env + @response = response end def render(binding) - template = File.read(template_path) + return send template.keys.first, template.values.first if template&.keys + template = File.read(template_path) ERB.new(template).result(binding) end @@ -30,10 +33,20 @@ def template end def template_path - path = template || [controller.name, action].join('/') - + path = [controller.name, action].join('/') + @env['simpler.template_path'] = "#{path}.html.erb" Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end + def plain(content) + @response.headers['Content-Type'] = 'text/plain' + content + end + + def json(content) + @response.headers['Content-Type'] = 'json' + content.to_json + end + end end diff --git a/lib/simpler_logger.rb b/lib/simpler_logger.rb new file mode 100644 index 00000000..fef361aa --- /dev/null +++ b/lib/simpler_logger.rb @@ -0,0 +1,32 @@ +require 'logger' +class SimplerLogger + def initialize(app) + @app = app + end + + def call(env) + @request = Rack::Request.new(env) + status, headers, body = @app.call(env) + logger = Logger.new('log/app.log') + if status == 404 + logger.error("\n#{log_error(@request, status, headers)}") + else + logger.info("\n#{log_info(@request, status, headers)}") + end + [status, headers, body] + end + + private + + def log_info(request, status, headers) + "Request: #{request.request_method} #{request.fullpath}\n" \ + "Handler: #{request.env['simpler.controller'].class}##{request.env['simpler.action']}\n" \ + "Parameters: #{request.env['simpler.params'].merge!(request.params)}\n" \ + "Response: #{status} #{headers['Content-Type']} #{request.env['simpler.template_path']}\n" \ + end + + def log_error(request, status, headers) + "Request: #{request.request_method} #{request.fullpath}\n" \ + "Response: #{status} #{headers['Content-Type']}\n" \ + end +end diff --git a/log/app.log b/log/app.log new file mode 100644 index 00000000..e69de29b