diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..cbe5cb43 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*/log/* diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..d41e5519 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,11 +1,14 @@ -class TestsController < Simpler::Controller +# frozen_string_literal: true +class TestsController < Simpler::Controller def index @time = Time.now + render plain: 'Some plain text' end - def create + def create; end + def show + render plain: "Params: #{params} Id: #{params[:id]}, name: #{@name}" end - end diff --git a/app/models/test.rb b/app/models/test.rb index 86376668..db135e04 100644 --- a/app/models/test.rb +++ b/app/models/test.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + # Simpler.application.db.create_table(:tests) do # primary_key :id # String :title, null: false # Integer :level, default: 0 # end class Test < Sequel::Model - end diff --git a/config.ru b/config.ru index 3060cc20..9242c112 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,6 @@ +# frozen_string_literal: true + require_relative 'config/environment' +use RequestLogger run Simpler.application diff --git a/config/environment.rb b/config/environment.rb index 7a0d38c3..57b4d869 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,3 +1,6 @@ +# frozen_string_literal: true + require_relative '../lib/simpler' +require_relative '../lib/middleware/request_logger' Simpler.application.bootstrap! diff --git a/config/routes.rb b/config/routes.rb index 4a751251..89c43bcf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ +# frozen_string_literal: true + Simpler.application.routes do get '/tests', 'tests#index' post '/tests', 'tests#create' + get '/tests/:id', 'tests#show' end diff --git a/lib/middleware/request_logger.rb b/lib/middleware/request_logger.rb new file mode 100644 index 00000000..bc41f89b --- /dev/null +++ b/lib/middleware/request_logger.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'logger' + +class RequestLogger + def initialize(app) + @app = app + @logger = Logger.new(File.expand_path('../log/app.log', __dir__)) + end + + def call(env) + request = Rack::Request.new(env) + response = @app.call(env) + + logging(request, response) + + response + end + + private + + def logging(request, response) + status, headers, _body = response + content_type = headers['Content-Type'] + template = headers['Rander-Template-Path'] + + @logger.info( + "\nRequest: [#{request.request_method}] #{request.fullpath} \n" \ + "Handler: #{request.env['simpler.controller']}#"\ + "#{request.env['simpler.action']} \n" \ + "Parameters: #{request.params.inspect}\n" \ + "Response: [#{status}] #{content_type} #{template}\n" + ) + end +end diff --git a/lib/simpler.rb b/lib/simpler.rb index f9dfe3c4..6fb23105 100644 --- a/lib/simpler.rb +++ b/lib/simpler.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + require 'pathname' require_relative 'simpler/application' module Simpler - class << self def application Application.instance @@ -12,5 +13,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..8077af07 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'yaml' require 'singleton' require 'sequel' @@ -6,7 +8,6 @@ module Simpler class Application - include Singleton attr_reader :db @@ -28,6 +29,8 @@ def routes(&block) def call(env) route = @router.route_for(env) + return page_404 unless route + controller = route.controller.new(env) action = route.action @@ -36,8 +39,12 @@ def call(env) private + def page_404 + Rack::Response.new(['