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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/log/*
9 changes: 8 additions & 1 deletion 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
render 'tests/list'
status 200
end

def create

render plain: "txt"
status 201
end

def show
@test_id = params[:id]
render 'tests/list'
end
end
11 changes: 11 additions & 0 deletions app/views/tests/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Create | Simpler application</title>
</head>
<body>
<h1>Simpler framework at work!</h1>
<p><%= @time %></p>
</body>
</html>
5 changes: 2 additions & 3 deletions app/views/tests/list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</head>
<body>
<h1>Simpler framework at work!</h1>

<p><%= @time %></p>
<p>Test id: <%= @test_id %></p>
</body>
</html>
</html>
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require_relative 'config/environment'
require_relative 'lib/middleware/logger'

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

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

def call(env)
status, headers, body = @app.call(env)
@logger.info(message(env, status, headers))
[status, headers, body]
end

def message(env, status, headers)

controller = env['simpler.controller']

if controller
<<~LOGGER

--------------------------------------------------------------
Request: #{env['REQUEST_METHOD']} #{env['REQUEST_URI']}
Handler: #{env['simpler.controller'].class}##{env['simpler.action']}
Parameters: #{env['simpler.params']}
Response: #{status} [#{headers['Content-Type']}] #{env['simpler.template_path']}

LOGGER
else
<<~LOGGER

--------------------------------------------------------------
Request: #{env['REQUEST_METHOD']} #{env['REQUEST_URI']}
Response: #{}

LOGGER
end
end
end
7 changes: 7 additions & 0 deletions lib/simpler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'sequel'
require_relative 'router'
require_relative 'controller'
require 'pry'

module Simpler
class Application
Expand All @@ -28,6 +29,9 @@ def routes(&block)

def call(env)
route = @router.route_for(env)

return not_found if route.nil?
# binding.pry
controller = route.controller.new(env)
action = route.action

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

def not_found
[404, { 'Content-Type' => 'text/plain' }, ['Not found']]
end
end
end
20 changes: 17 additions & 3 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'view'
require 'pry'

module Simpler
class Controller
Expand All @@ -18,7 +19,6 @@ def make_response(action)
set_default_headers
send(action)
write_response

@response.finish
end

Expand All @@ -29,7 +29,7 @@ def extract_name
end

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

def write_response
Expand All @@ -43,12 +43,26 @@ def render_body
end

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

def render(template)
case template
when Hash
headers['Content-Type'] = 'text/plain' if template.has_key?(:plain)
else
set_default_headers
end

@request.env['simpler.template'] = template
end

def headers
@response.headers
end

def status(code)
@response.status = code
end
end
end
3 changes: 2 additions & 1 deletion lib/simpler/router.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'router/route'
require 'pry'

module Simpler
class Router
Expand All @@ -19,7 +20,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
23 changes: 21 additions & 2 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,27 @@ def initialize(method, path, controller, action)
@action = action
end

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

private

def parse_path(path, env)
params = {}
request_path = path.split('/')
router_path = @path.split('/')

return false if request_path.size != router_path.size

router_path.each_with_index do |element, index|
if element.start_with?(':')
params[element[1..-1].to_sym] = request_path[index]
else
return false unless element == request_path[index]
end
end
env['simpler.params'] = params
end

end
Expand Down
23 changes: 17 additions & 6 deletions lib/simpler/view.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'erb'
require_relative 'view/html_render'
require_relative 'view/plain_render'

module Simpler
class View
Expand All @@ -10,9 +11,7 @@ def initialize(env)
end

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

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

private
Expand All @@ -29,11 +28,23 @@ def template
@env['simpler.template']
end

def render_template
template = @env['simpler.template']

case template
when String
HtmlRender.new(template_path)
when Hash
PlainRender.new(template) if template.has_key?(:plain)
else
HtmlRender.new(template_path)
end
end

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

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

end
end
17 changes: 17 additions & 0 deletions lib/simpler/view/html_render.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'erb'

module Simpler
class View

class HtmlRender
def initialize(template_path)
@path = template_path
end

def result(binding)
template = File.read(@path)
ERB.new(template).result(binding)
end
end
end
end
14 changes: 14 additions & 0 deletions lib/simpler/view/plain_render.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Simpler
class View

class PlainRender
def initialize(template)
@template = template
end

def result(_binding)
@template[:plain]
end
end
end
end