Skip to content

HomeWork #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*/log/*
9 changes: 6 additions & 3 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion app/models/test.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: true

require_relative 'config/environment'

use RequestLogger
run Simpler.application
3 changes: 3 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: true

require_relative '../lib/simpler'
require_relative '../lib/middleware/request_logger'

Simpler.application.bootstrap!
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions lib/middleware/request_logger.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions lib/simpler.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require 'pathname'
require_relative 'simpler/application'

module Simpler

class << self
def application
Application.instance
Expand All @@ -12,5 +13,4 @@ def root
Pathname.new(File.expand_path('..', __dir__))
end
end

end
12 changes: 9 additions & 3 deletions lib/simpler/application.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'yaml'
require 'singleton'
require 'sequel'
Expand All @@ -6,7 +8,6 @@

module Simpler
class Application

include Singleton

attr_reader :db
Expand All @@ -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

Expand All @@ -36,8 +39,12 @@ def call(env)

private

def page_404
Rack::Response.new(['<h1>404 Not Found</h1>'], 404, { 'Content-Type' => 'text/html' }).finish
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
Expand All @@ -53,6 +60,5 @@ def setup_database
def make_response(controller, action)
controller.make_response(action)
end

end
end
29 changes: 18 additions & 11 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require_relative 'view'

module Simpler
class Controller

attr_reader :name, :request, :response

def initialize(env)
Expand All @@ -15,40 +16,46 @@ def make_response(action)
@request.env['simpler.controller'] = self
@request.env['simpler.action'] = action

set_default_headers
send(action)
write_response

@response.finish
end

def status(status_code)
@response.status = status_code
end

def headers
@response
end

private

def extract_name
self.class.name.match('(?<name>.+)Controller')[:name].downcase
end

def set_default_headers
@response['Content-Type'] = 'text/html'
end

def write_response
body = render_body

@response.write(body)
end

def render_body
View.new(@request.env).render(binding)
view = View.new(@request.env)
@response['Rander-Template-Path'] = view.used_template.to_s
@response['Content-Type'] = view.content_type.to_s
view.render(binding)
end

def params
@request.params
path = '((?<path>.*)/(?<id>\d*))'
@request.path.match(path)
end

def render(template)
@request.env['simpler.template'] = template
def render(parametr)
@request.env['simpler.template'] = parametr
end

end
end
4 changes: 2 additions & 2 deletions lib/simpler/router.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require_relative 'router/route'

module Simpler
class Router

def initialize
@routes = []
end
Expand Down Expand Up @@ -36,6 +37,5 @@ def add_route(method, path, route_point)
def controller_from_string(controller_name)
Object.const_get("#{controller_name.capitalize}Controller")
end

end
end
15 changes: 13 additions & 2 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

module Simpler
class Router
class Route

attr_reader :controller, :action

def initialize(method, path, controller, action)
Expand All @@ -12,9 +13,19 @@ def initialize(method, path, controller, action)
end

def match?(method, path)
@method == method && path.match(@path)
path = idless_path(path) if id?
@method == method && path.match("^#{@path}$")
end

private

def id?
@path.match(':id')
end

def idless_path(path)
"#{path.split(%r{/\d}).first}/:id"
end
end
end
end
19 changes: 15 additions & 4 deletions lib/simpler/view.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
# frozen_string_literal: true

require 'erb'

module Simpler
class View
VIEW_BASE_PATH = 'app/views'

VIEW_BASE_PATH = 'app/views'.freeze
attr_reader :used_template, :content_type

def initialize(env)
@env = env
@used_template = template_path
@content_type = 'text/html'
end

def render(binding)
template = File.read(template_path)
return plain_render if template.is_a?(Hash) && template.key?(:plain)

template_file = File.read(@used_template)

ERB.new(template).result(binding)
ERB.new(template_file).result(binding)
end

private

def plain_render
@content_type = 'text/plain'
template[:plain]
end

def controller
@env['simpler.controller']
end
Expand All @@ -34,6 +46,5 @@ def template_path

Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb")
end

end
end