diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 00000000..926b2842 --- /dev/null +++ b/.byebug_history @@ -0,0 +1,256 @@ +c +@out_params +c +@out_params +c +v +env['simpler.controller'] +"#{env['simpler.controller'].class}##{env['simpler.action']}" +c +@out_params +c +handler +response +headers +status +c +params +c +@params +c +controller.params +c +controller.params +c +params +c +controller.params +c +controller.params +c +controller.params +c +controller.params +c +controller.params +controller +c +controller.headers +controller.params +c +controller.params +controller.params= +controller.params +c +@params +c +@request.env['simpler.controller.params'] +c +controller.request.env['simpler.controller.params'] +c +controller.request.env['simpler.action'] +controller.request +route.params +controller.route +controller +c +route +c +route.determ_params(env) +env +route +exit +@request.env['simpler.controller'] +@request.env['simpler.contoller.params'] +@request.env['simpler.contoller'] +self +@request.env['simpler.contoller'] +@request.env['simpler.action'] +env['simpler.action'] +{env['simpler.controller'] +#{env['simpler.controller'].class}##{env['simpler.action']}" +exit +@request +exit +env['simpler.action'] +env['simpler.router'] +env['simpler'] +@app.call(env) +env['router'] +env['simpler.router'] +env +exit +env['simpler.controller'] +env +exit +handler +exit +env['simpler.action'] +env['simpler.controller'] +env['simpler.controller'].class +env +env['simpler.controller'].class +#{env{'simpler.action'}} +"#{env['simpler.controller'].class}" +env['simpler.controller'].class +env +@app.call(env) +exit +handler.class +handler +exit +respone +response +header +headers +status +status, header, respone = @app.call(env) +all.status +all +exit +all +env +all +c +route_path +user_path +c +@params +route_path +exit +route_path +user_path +user_path.match?(route_path) +@path +exit +"/tests/1".match(/\/tests\/(\w+)\Z/) +"/tests/1".match?(/\/tests\/(\w+)\Z/) +"/tests/1".match?(/\/tests\/(\w+)/) +"/tests/1".match?(/\/tests\/(\w+)/Z) +/tests/1.match?(/\/tests\/(\w+)/) +"/tests/1".match?(/\/tests\/(\w+)/) +"/tests/1".match?("/\/tests\/(\w+)/") +"/tests/1".match? "/\/tests\/(\w+)/" +"/tests/1".match?('/\/tests\/(\w+)/') +route_path +"/tests/1".match?(route_path) +"/tests/1".match?("/\/tests\/(\w+)/") +user_path.match?(route_path) +route_path +user_path +c +route_path +user_path +@path +exit +Hash[keys.captures.zip(values.captures)] +Hash[*keys.captures.zip(values.captures)] +keys.captures.zip(values.captures) +values.captures +keys.captures +keys.captures.zip(values.captures) +keys.captures +keys +keys.names +@path +c +@path +exit +keys +values +route_path +user_path +user_path.match?(route_path) +@path +exit +values +keys +@path +c +@path +path +c +keys +values +c +keys +exit +route_path +@path +route_path +keys +route_path +c +route_path +values +route_path +c +@router +@router.route_for(env) +c +@routes.find { |route| route.match?(method, path) } +c +route +c +@routes.find { |route| route.match?(method, path) } +path +@routes +c +@router.route_for(env) +env +@router +route +c +@method == method && correct_path?(path) +c +@method == method && correct_path?(path) +correct_path?(path) +c +@path +path +correct_path?(path) +c +correct_path?(path) +exit +c +correct_path?(path) +@method == method +@method +c +@routes.find { |route| route.match?(method, path) } +c +route +c +@params +c +@path +c +@path +path +exit +@params +c +route +c +@path +path +c +@path +user_path +route_path +@params +c +exit +user_path +path +@params +params +c +@path +path +c +@path +path +exit +@path +path diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..19353b96 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.byebug_history +/app/app.log diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..67d529a0 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,11 +1,15 @@ -class TestsController < Simpler::Controller +# frozen_string_literal: true +class TestsController < Simpler::Controller def index - @time = Time.now + @tests = Test.all + # render plain: "ololo" + status 201 end - def create - + def show + @test = Test.find(id: @params[:id]) end + def create; 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/app/views/tests/index.html.erb b/app/views/tests/index.html.erb index 39fce580..673818d6 100644 --- a/app/views/tests/index.html.erb +++ b/app/views/tests/index.html.erb @@ -7,6 +7,11 @@

Simpler framework at work!

-

<%= @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..28ba9135 --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1,10 @@ + + + + + Index | Simpler application + + +

<%= @test.title %>

+ + diff --git a/config.ru b/config.ru index 3060cc20..1437c576 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,5 @@ require_relative 'config/environment' +require_relative 'lib/middleware/historylogger' +use HistoryLogger run Simpler.application diff --git a/config/environment.rb b/config/environment.rb index 7a0d38c3..a566c8d3 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative '../lib/simpler' Simpler.application.bootstrap! diff --git a/config/routes.rb b/config/routes.rb index 4a751251..0efcec16 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' + get '/tests/:id', 'tests#show' post '/tests', 'tests#create' end diff --git a/lib/middleware/historylogger.rb b/lib/middleware/historylogger.rb new file mode 100644 index 00000000..bd131273 --- /dev/null +++ b/lib/middleware/historylogger.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class HistoryLogger + attr_reader :out_params + + def initialize(app) + @app = app + @out_params = {} + end + + def call(env) + status, headers, response = @app.call(env) + + @out_params[:Request] = "#{env['REQUEST_METHOD']} #{env['REQUEST_PATH']}" + @out_params[:Handler] = "#{env['simpler.controller'].class}##{env['simpler.action']}" + @out_params[:Parameters] = (env['simpler.controller.params']).to_s + @out_params[:Response] = "#{status} [#{headers['Content-Type']}] #{env['simpler.template_path']}" + + add_log(@out_params) + + [status, headers, response] + end + + def add_log(content) + logfile = 'log/app.log' + File.open(logfile, 'a') do |file| + content.each { |k, v| file << ("#{k}: #{v}\n") } + file << "\n" + end + end +end diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 711946a9..88f094ed 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -1,12 +1,14 @@ +# frozen_string_literal: true + require 'yaml' require 'singleton' require 'sequel' +require 'byebug' require_relative 'router' require_relative 'controller' module Simpler class Application - include Singleton attr_reader :db @@ -28,7 +30,12 @@ def routes(&block) def call(env) route = @router.route_for(env) + + return no_route unless route + + route.determ_params(env) controller = route.controller.new(env) + controller.params = route.params action = route.action make_response(controller, action) @@ -36,8 +43,12 @@ def call(env) private + def no_route + [404, { 'Content-Type' => 'text\plain' }, ["Page not found\n"]] + end + def require_app - Dir["#{Simpler.root}/app/**/*.rb"].each { |file| require file } + Dir["#{Simpler.root}/app/**/*.rb"].sort.each { |file| require file } end def require_routes @@ -53,6 +64,5 @@ def setup_database def make_response(controller, action) controller.make_response(action) end - end end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..a30c5e30 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -1,19 +1,24 @@ +# frozen_string_literal: true + require_relative 'view' module Simpler class Controller - attr_reader :name, :request, :response + attr_accessor :headers, :params def initialize(env) @name = extract_name @request = Rack::Request.new(env) @response = Rack::Response.new + @headers = @response.headers + @params = {} end def make_response(action) @request.env['simpler.controller'] = self @request.env['simpler.action'] = action + @request.env['simpler.controller.params'] = @incoming_params set_default_headers send(action) @@ -24,6 +29,10 @@ def make_response(action) private + def status(status) + @response.status = status + end + def extract_name self.class.name.match('(?.+)Controller')[:name].downcase end @@ -33,7 +42,11 @@ def set_default_headers end def write_response - body = render_body + body = if @request.env['simpler.template'].is_a?(Hash) && @request.env['simpler.template'].key?(:plain) + @request.env['simpler.template'][:plain] + else + render_body + end @response.write(body) end @@ -42,13 +55,8 @@ def render_body View.new(@request.env).render(binding) end - def params - @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..bef7cf96 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + require_relative 'router/route' module Simpler class Router - def initialize @routes = [] end @@ -18,7 +19,6 @@ 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) } end @@ -36,6 +36,5 @@ def add_route(method, path, route_point) def controller_from_string(controller_name) Object.const_get("#{controller_name.capitalize}Controller") end - end end diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 4c66b4b7..d553159d 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -1,20 +1,39 @@ +# frozen_string_literal: true + 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 && correct_path?(path) + end + + def determ_params(env) + user_path = env['PATH_INFO'] + values = user_path.match(regexp_path) + keys = @path.gsub(':', '').match(regexp_path) + + @params = Hash[keys.captures.map(&:to_sym).zip(values.captures)] end + private + + def correct_path?(user_path) + user_path.match?(regexp_path) + end + + def regexp_path + Regexp.new("#{@path.gsub(/(:\w+)/, '(\\w+)')}\\Z") + end end end end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..7309c725 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + require 'erb' module Simpler class View - VIEW_BASE_PATH = 'app/views'.freeze def initialize(env) @@ -34,6 +35,5 @@ def template_path Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end - end end diff --git a/log/app.log b/log/app.log new file mode 100644 index 00000000..cc0f7643 --- /dev/null +++ b/log/app.log @@ -0,0 +1,18 @@ +Request: GET /tests/1 +Handler: TestsController#show +Parameters: +Response: 200 [text/html] +Request: GET /tests/1 +Handler: TestsController#show +Parameters: +Response: 200 [text/html] +Request: GET /tests/1 +Handler: TestsController#show +Parameters: +Response: 200 [text/html] + +Request: GET /tests +Handler: TestsController#index +Parameters: +Response: 201 [text/html] +