Skip to content

[lesson 20] #187

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 1 commit 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/*
11 changes: 9 additions & 2 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ class TestsController < Simpler::Controller

def index
@time = Time.now
@tests = Test.all
# status 201
# headers['Content-Type'] = 'text/plain'
# render plain: "Time: #{@time}"
# puts headers
end

def create

def show
@test = Test.find(id: params[:id])
end

def create; end

end
3 changes: 2 additions & 1 deletion app/views/tests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<h1>Simpler framework at work!</h1>

<p><%= @time %></p>
<p><%= @tests %></p>
</body>
</html>
</html>
12 changes: 12 additions & 0 deletions app/views/tests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Show | Simpler application</title>
</head>
<body>
<h1>Simpler framework at work!</h1>

<p><%= @test&.title %></p>
</body>
</html>
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative 'config/environment'
require_relative 'lib/middleware/simpler_logger'

use SimplerLogger, logdev: File.expand_path('log/app.log', __dir__)
run Simpler.application
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Simpler.application.routes do
get '/tests/:id', 'tests#show'
get '/tests', 'tests#index'
post '/tests', 'tests#create'
end
47 changes: 47 additions & 0 deletions lib/middleware/simpler_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'logger'

class SimplerLogger
def initialize(app, **options)
@logger = Logger.new(options[:logdev] || STDOUT)
@app = app
end

def call(env)
request = Rack::Request.new(env)
status, headers, body = @app.call(env)
@logger.info(log_message(request, status, headers, env))

[status, headers, body]
end

private

def log_message(request, status, headers, env)
<<~LOG
Request: #{request.request_method} #{request.fullpath}
Handler: #{controller(env)}##{action(env)}
Parameters: #{request.params.inspect}
Response: #{status} [#{content_type(headers)}] #{template(env)}
LOG
end

def controller(env)
env['simpler.controller'] ? env['simpler.controller'].class.name : 'UnknownController'
end

def action(env)
env['simpler.action'] || 'unknown_action'
end

def template(env)
return "#{env['simpler.template']}.html.erb" if env['simpler.template']

controller_name = env['simpler.controller']&.name || 'unknown_controller'
action = env['simpler.action'] || 'unknown_action'
"#{controller_name}/#{action}.html.erb"
end

def content_type(headers)
headers['Content-Type'] || 'unknown_content_type'
end
end
6 changes: 6 additions & 0 deletions lib/simpler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def routes(&block)

def call(env)
route = @router.route_for(env)
return not_found_response unless route

controller = route.controller.new(env)
action = route.action

Expand All @@ -54,5 +56,9 @@ def make_response(controller, action)
controller.make_response(action)
end

def not_found_response
[404, { 'Content-Type' => 'text/plain' }, ['404 Not Found']]
end

end
end
26 changes: 22 additions & 4 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def make_response(action)
@response.finish
end

def params
@request.params.update(@request.env['simpler.params'])
end

def headers
@response.headers
end

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

private

def extract_name
Expand All @@ -42,12 +54,18 @@ def render_body
View.new(@request.env).render(binding)
end

def params
@request.params
def render(template)
if template.is_a?(Hash)
render_plain(template[:plain]) if template.key?(:plain)
else
@request.env['simpler.template'] = template
end
end

def render(template)
@request.env['simpler.template'] = template
def render_plain(text)
@response['Content-Type'] = 'text/plain'

@response.write(text)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/simpler/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def route_for(env)
method = env['REQUEST_METHOD'].downcase.to_sym
path = env['PATH_INFO']

@routes.find { |route| route.match?(method, path) }
@routes.find { |route| route.match?(method, path, env) }
end

private
Expand Down
17 changes: 14 additions & 3 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ 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
@pattern = path_pattern(path)
end

def match?(method, path)
@method == method && path.match(@path)
def match?(method, path, env)
match = path.match(@pattern)
return false unless @method == method && match

@params = match.named_captures.transform_keys(&:to_sym)
env['simpler.params'] = @params
end

private

def path_pattern(path)
Regexp.new("^#{path.gsub('/', '\/').gsub(/:(\w+)/, '(?<\1>[^/]+)')}$")
end

end
Expand Down