diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..df89b950 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +log/app.log \ No newline at end of file diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..9439113d 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,11 +1,9 @@ class TestsController < Simpler::Controller - def index @time = Time.now end - def create - + def show + @params = params[:id] end - end diff --git a/app/models/test.rb b/app/models/test.rb index 86376668..98cea0cb 100644 --- a/app/models/test.rb +++ b/app/models/test.rb @@ -4,5 +4,4 @@ # Integer :level, default: 0 # end class Test < Sequel::Model - end diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb new file mode 100644 index 00000000..bcc4780e --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1 @@ +

<%= @params%>

diff --git a/config.ru b/config.ru index 3060cc20..5ac7059e 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,4 @@ require_relative 'config/environment' +use AppLogger, logdev: File.expand_path('log/app.log', __dir__) run Simpler.application diff --git a/config/routes.rb b/config/routes.rb index 4a751251..347c4041 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Simpler.application.routes do + get '/tests/:id', 'tests#show' get '/tests', 'tests#index' post '/tests', 'tests#create' end diff --git a/lib/middleware/applogger.rb b/lib/middleware/applogger.rb new file mode 100644 index 00000000..ddc75a1c --- /dev/null +++ b/lib/middleware/applogger.rb @@ -0,0 +1,31 @@ +class AppLogger + def initialize(app, **options) + @logger = Logger.new(options[:logdev] || STDOUT) + @app = app + end + + def call(env) + status, headers, body = @app.call(env) + + request = Rack::Request.new(env) + + request_info(request) + responce_info(status, headers, request) + + [status, headers, ["#{body}"]] + end + + def request_info(request) + log = "Request: #{request.env['REQUEST_METHOD']} #{request.env['REQUEST_URI']} + Handler: #{request.env['simpler.controller'].class.name}##{request.env['simpler.action']} + Parameters: #{request.env['simpler.params']}" + + @logger.info(log) + end + + def responce_info(status, headers, request) + log = "Response: #{status} #{headers['Content-Type']} #{request.env['simpler.file_path']} " + + @logger.info(log) if request.env['simpler.file_path'] + end +end diff --git a/lib/simpler.rb b/lib/simpler.rb index f9dfe3c4..d4d365c9 100644 --- a/lib/simpler.rb +++ b/lib/simpler.rb @@ -2,7 +2,6 @@ require_relative 'simpler/application' module Simpler - class << self def application Application.instance @@ -12,5 +11,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..0c9ed759 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -6,7 +6,6 @@ module Simpler class Application - include Singleton attr_reader :db @@ -20,6 +19,7 @@ def bootstrap! setup_database require_app require_routes + require_logger end def routes(&block) @@ -28,14 +28,27 @@ def routes(&block) def call(env) route = @router.route_for(env) - controller = route.controller.new(env) - action = route.action + if route + controller = route.controller.new(env) + action = route.action - make_response(controller, action) + make_response(controller, action) + else + response_not_found + end end private + def response_not_found + response = Rack::Response.new + + response.status = 404 + response['Content-Type'] = 'text/plain' + response.body = ['Not Found'] + response.finish + end + def require_app Dir["#{Simpler.root}/app/**/*.rb"].each { |file| require file } end @@ -44,6 +57,10 @@ def require_routes require Simpler.root.join('config/routes') end + def require_logger + require Simpler.root.join('lib/middleware/applogger') + end + def setup_database database_config = YAML.load_file(Simpler.root.join('config/database.yml')) database_config['database'] = Simpler.root.join(database_config['database']) @@ -53,6 +70,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..492618f9 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -1,8 +1,9 @@ require_relative 'view' +require 'active_support/all' +require 'json' module Simpler class Controller - attr_reader :name, :request, :response def initialize(env) @@ -15,6 +16,8 @@ def make_response(action) @request.env['simpler.controller'] = self @request.env['simpler.action'] = action + parameter_assignment + set_default_headers send(action) write_response @@ -24,6 +27,21 @@ def make_response(action) private + def status(code) + @response.status = code + end + + def parameter_assignment + number = @request.env['PATH_INFO'].split('/').map(&:to_i).max + + @params ||= { id: number } + @request.env['simpler.params'] = @params + end + + def headers + @response + end + def extract_name self.class.name.match('(?.+)Controller')[:name].downcase end @@ -42,13 +60,29 @@ def render_body View.new(@request.env).render(binding) end - def params - @request.params + attr_reader :params + + def format_response(hash) + @request.env['simpler.body'] = if hash[:json] + headers['Content-Type'] = 'text/json' + hash[:json].to_json + elsif hash[:xml] + headers['Content-Type'] = 'text/xml' + hash[:xml].to_xml + elsif hash[:plain] + headers['Content-Type'] = 'text/plain' + hash[:plain].to_s + elsif hash[:inline] + hash[:inline] + else + 'Unknown format' + end end def render(template) + format_response(template) if template.instance_of?(Hash) + @request.env['simpler.template'] = template end - end end diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..b05449b3 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -2,7 +2,6 @@ module Simpler class Router - def initialize @routes = [] end @@ -36,6 +35,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..dec387a3 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -1,7 +1,6 @@ module Simpler class Router class Route - attr_reader :controller, :action def initialize(method, path, controller, action) @@ -12,9 +11,9 @@ def initialize(method, path, controller, action) end def match?(method, path) - @method == method && path.match(@path) + primary_key = @path.split('/')[-1] + @method == method && path.gsub(/\d/, primary_key).match(@path) end - end end end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..d4362674 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -2,7 +2,6 @@ module Simpler class View - VIEW_BASE_PATH = 'app/views'.freeze def initialize(env) @@ -10,13 +9,17 @@ def initialize(env) end def render(binding) - template = File.read(template_path) + template = render_body || File.read(template_path) ERB.new(template).result(binding) end private + def render_body + @env['simpler.body'] + end + def controller @env['simpler.controller'] end @@ -29,11 +32,18 @@ def template @env['simpler.template'] end + def template_file_path + return unless controller.name + + @env['simpler.file_path'] = [controller.name, action].join('/') + '.html.erb' + end + def template_path + template_file_path + path = template || [controller.name, action].join('/') Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end - end end