Skip to content
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
3 changes: 3 additions & 0 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ def index
end

def create
end

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

end
16 changes: 16 additions & 0 deletions app/log/app.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
I, [2022-10-25T22:54:40.913440 #10948] INFO -- : Request: GET /
I, [2022-10-25T22:54:40.913805 #10948] INFO -- : Handler: NilClass #
I, [2022-10-25T22:54:40.913904 #10948] INFO -- : Parameters:
I, [2022-10-25T22:54:40.913981 #10948] INFO -- : 404 [text/plain]
I, [2022-10-25T22:54:41.824050 #10948] INFO -- : Request: GET /favicon.ico
I, [2022-10-25T22:54:41.824392 #10948] INFO -- : Handler: NilClass #
I, [2022-10-25T22:54:41.824493 #10948] INFO -- : Parameters:
I, [2022-10-25T22:54:41.824898 #10948] INFO -- : 404 [text/plain]
I, [2022-10-25T22:54:48.787383 #10948] INFO -- : Request: GET /tests
I, [2022-10-25T22:54:48.787683 #10948] INFO -- : Handler: TestsController # index
I, [2022-10-25T22:54:48.787848 #10948] INFO -- : Parameters: {}
I, [2022-10-25T22:54:48.788159 #10948] INFO -- : 200 [text/html] tests/index.html.erb
I, [2022-10-25T22:55:15.034593 #10948] INFO -- : Request: GET /tests/1
I, [2022-10-25T22:55:15.034882 #10948] INFO -- : Handler: TestsController # show
I, [2022-10-25T22:55:15.034998 #10948] INFO -- : Parameters: {:id=>"1"}
I, [2022-10-25T22:55:15.035067 #10948] INFO -- : 200 [text/html] tests/show.html.erb
11 changes: 11 additions & 0 deletions app/views/tests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>show test</title>
</head>
<body>
<h1><%= @test.title %></h1>
<p><%= @test.level %></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/applogger'

use AppLogger, logdev: File.expand_path('app/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', 'tests#index'
post '/tests', 'tests#create'
get '/tests/:id', 'tests#show'
end
30 changes: 30 additions & 0 deletions lib/middleware/applogger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'logger'

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

def call(env)
@env = env
response = @app.call(env)

request_log
response_log(response)

response
end

def request_log
@logger.info "Request: #{@env['REQUEST_METHOD']} #{@env['REQUEST_URI']}"
@logger.info "Handler: #{@env['simpler.controller'].class} # #{@env['simpler.action']}"
@logger.info "Parameters: #{@env['simpler.params']}"
end

def response_log(response)
template_path = @env['simpler.template_path'] ? "#{@env['simpler.template_path']}.html.erb" : ''
@logger.info "#{response[0]} [#{response[1]['Content-Type']}] #{template_path}"
end

end
5 changes: 5 additions & 0 deletions lib/simpler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def routes(&block)

def call(env)
route = @router.route_for(env)
return not_found_response if route.nil?
controller = route.controller.new(env)
action = route.action

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

def not_found_response
Rack::Response.new('Page not found', 404, { 'Content-Type' => 'text/plain' }).finish
end

end
end
23 changes: 20 additions & 3 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,32 @@ def write_response
end

def render_body
View.new(@request.env).render(binding)
@request.env['simpler.plain_text'] || View.new(@request.env).render(binding)
end

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

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

def headers(key, value)
@response[key] = value
end

def render(template)
@request.env['simpler.template'] = template
if template[:plain]
render_plain_text(template[:plain])
else
@request.env['simpler.template'] = template
end
end

def render_plain_text(text)
@response['Content-Type'] = 'text/plain'
@request.env['simpler.plain_text'] = text
end

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

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

private
Expand Down
16 changes: 14 additions & 2 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ class Route

def initialize(method, path, controller, action)
@method = method
@path = path
@path = path_regexp(path)
@controller = controller
@action = action
end

def match?(method, path)
@method == method && path.match(@path)
@method == method && path =~ @path
end

def set_params(env, path)
env['simpler.params'] = @path.match(path).named_captures.transform_keys!(&:to_sym)
end

def path_regexp(path)
regex = path.split('/').
map { |part| part.start_with?(':') ? "(?<#{part.delete_prefix(':')}>\\w+)" : part }.
join('/')

Regexp.new('^' + regex + '$')
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/simpler/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def template

def template_path
path = template || [controller.name, action].join('/')

@env['simpler.template_path'] = path
Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb")
end

Expand Down