Skip to content
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

Simpler Upgrade #147

Open
wants to merge 3 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
8 changes: 8 additions & 0 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ def index
@time = Time.now
end

def show
@test = params[:id]
end

def question_show
@test, @qst = params[:id], params[:question_id]
end

def create

end
Expand Down
2 changes: 1 addition & 1 deletion app/views/tests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

<p><%= @time %></p>
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion app/views/tests/list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

<p><%= @time %></p>
</body>
</html>
</html>
1 change: 1 addition & 0 deletions app/views/tests/question_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Question #<%= @qst %> (test #<%= @test %>)</h1>
1 change: 1 addition & 0 deletions app/views/tests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Test #<%= @test %></h1>
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 "middlewares/logger"

use Log, logdev: File.expand_path("log/app.log", __dir__)
run Simpler.application
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Simpler.application.routes do
get '/tests', 'tests#index'
post '/tests', 'tests#create'
get '/tests/:id', 'tests#show'
get '/tests/:id/questions/:question_id', 'tests#question_show'
end
16 changes: 12 additions & 4 deletions lib/simpler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ def routes(&block)
end

def call(env)
route = @router.route_for(env)
controller = route.controller.new(env)
action = route.action

route = @router.route_for(env)
if route
controller = route.controller.new(env, route.route_params)
action = route.action
else
controller = Controller.new(env)
end

env['simpler.handler'] = "#{controller.name}##{action}"
env['simpler.params'] = controller.request.params
env['simpler.erb'] = "#{[controller.name, action].join('/')}.html.erb"

make_response(controller, action)
end

Expand Down
43 changes: 34 additions & 9 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ class Controller

attr_reader :name, :request, :response

def initialize(env)
def initialize(env, route_params = {})
@name = extract_name
@request = Rack::Request.new(env)
@response = Rack::Response.new
params.merge!(route_params)
end

def make_response(action)
@request.env['simpler.controller'] = self
@request.env['simpler.action'] = action

set_default_headers
send(action)
write_response
@request.env['simpler.route_error'] = true if !action
@request.env['simpler.controller'] = self
@request.env['simpler.action'] = action

set_default_headers
send(action) if action
write_response

@response.finish
end
Expand All @@ -32,8 +35,26 @@ def set_default_headers
@response['Content-Type'] = 'text/html'
end

def set_plain_text_header
@response['Content-Type'] = 'text/plain'
end

def set_response_status(status = 200)
@response.status = status
end

def write_response
body = render_body
if @request.env['simpler.route_error']
set_plain_text_header
set_response_status(404)
body = "No such page: #{@request.path}"
elsif @request.env['simpler.plain_text']
set_plain_text_header
set_response_status(201)
body = @request.env['simpler.plain_text']
else
body = render_body
end

@response.write(body)
end
Expand All @@ -43,11 +64,15 @@ def render_body
end

def params
@request.params
@request.params
end

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

end
Expand Down
14 changes: 12 additions & 2 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ module Simpler
class Router
class Route

attr_reader :controller, :action
attr_reader :controller, :action, :route_params

def initialize(method, path, controller, action)
@method = method
@path = path
@controller = controller
@action = action
@route_params = {}
end

def match?(method, path)
@method == method && path.match(@path)
return false if @method != method

sample = @path.split('/').map { |pth| pth.include?(':') ? pth[1...].to_sym : pth }

if (path =~ /#{sample.map { |pth| pth.is_a?(Symbol) ? "[^\/]+" : pth }.join('\/')}[\/]*$/)
path = path.split('/')
sample.each_with_index { |v, i| @route_params[v] = path[i] if v.is_a?(Symbol) }
else
false
end
end

end
Expand Down
1 change: 0 additions & 1 deletion lib/simpler/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def initialize(env)

def render(binding)
template = File.read(template_path)

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

Expand Down
5 changes: 5 additions & 0 deletions log/app.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Logfile created on 2022-08-19 22:34:26 +0300 by logger.rb/v1.5.0
I, [2022-08-19T22:34:38.176146 #12180] INFO -- : Request: GET /tests/101
I, [2022-08-19T22:34:38.176472 #12180] INFO -- : Handler: tests#show
I, [2022-08-19T22:34:38.176610 #12180] INFO -- : Parameters: {:id=>"101"}
I, [2022-08-19T22:34:38.176793 #12180] INFO -- : Response: 200 [text/html] tests/show.html.erb
21 changes: 21 additions & 0 deletions middlewares/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'logger'

class Log

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

def call(env)

response = @app.call(env)

@logger.info("Request: #{env['REQUEST_METHOD']} #{env['PATH_INFO']}")
@logger.info("Handler: #{env['simpler.handler']}")
@logger.info("Parameters: #{env['simpler.params']}")
@logger.info("Response: #{response[0]} [#{response[1]["Content-Type"]}] #{env['simpler.erb']}")
response
end

end