Skip to content

Ged simpler #182

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 9 commits into
base: master
Choose a base branch
from
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
log/
6 changes: 6 additions & 0 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ class TestsController < Simpler::Controller

def index
@time = Time.now
# render plain: 'Plain text response'
end

def create

end

def show
status(201)
@test = Test.first(id: params['id'])
end

end
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: <%= @test_id %></p>
</body>
</html>
4 changes: 4 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require_relative 'middleware/simpler_logger'
require_relative 'config/environment'

use Middleware::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', 'tests#index'
post '/tests', 'tests#create'
get '/tests/:id', 'tests#show'
end
7 changes: 7 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 if route.nil?
controller = route.controller.new(env)
action = route.action

Expand All @@ -36,6 +37,12 @@ def call(env)

private

def not_found
[ 404,
{ 'Content-Type' => 'text/html' },
['<h1> 404 - Page Not Found</h1>'] ]
end

def require_app
Dir["#{Simpler.root}/app/**/*.rb"].each { |file| require file }
end
Expand Down
18 changes: 18 additions & 0 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def extract_name
end

def set_default_headers
if @request.env['simpler.template'].is_a?(Hash)
return @response['Content-Type'] = case @request.env['simpler.template'].first[0]
when :plain then 'text/plain'
else 'text/html'
end
end
@response['Content-Type'] = 'text/html'
end

Expand All @@ -50,5 +56,17 @@ def render(template)
@request.env['simpler.template'] = template
end

def headers(hash)
hash.each_pair { |key, value| header(key, value) }
end

def header(key, value)
@response[key.to_s] = value.to_s
end

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

end
end
1 change: 0 additions & 1 deletion lib/simpler/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def post(path, route_point)
def route_for(env)
method = env['REQUEST_METHOD'].downcase.to_sym
path = env['PATH_INFO']

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

Expand Down
17 changes: 17 additions & 0 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,25 @@ def initialize(method, path, controller, action)

def match?(method, path)
@method == method && path.match(@path)
@method == method && path.match(to_regex(@path))
end
def extract_params(env)
request_path_items = env['PATH_INFO'].split('/')
path_items = @path.split('/').map { |item| item.sub(':', '') }

params = Hash[path_items.zip(request_path_items)].delete_if { |k, v| k == v }
env['params'] = params
end

private

def to_regex(path)
path.split('/')
.map { |string| string.start_with?(':') ? '\d+' : string }
.join('/')
.concat('$')
.then { |regex| Regexp.new (regex) }
end
end
end
end
2 changes: 2 additions & 0 deletions lib/simpler/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ def template
end

def template_path

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

Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb")
# Simpler.root.join('app/views/tests/index.html.erb')
end

end
Expand Down
24 changes: 24 additions & 0 deletions middleware/simpler_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'logger'

module Middleware
class SimplerLogger
def initialize(app, _logger)
@app = app
@logger = Logger.new(File.expand_path('../log/app.log', __dir__) || STDOUT)
end

def call(env)
request = Rack::Request.new(env)
@app.call(env).tap do |response|
controller = env['simpler.controller']
handler = "#{controller.class.name}##{env['simpler.action']}"
status, headers = *response

@logger.info "Request: #{env['REQUEST_METHOD']} #{env['REQUEST_PATH']}"
@logger.info "Handler: #{handler}"
@logger.info "Params: GET: #{env['simpler.route_params']}, POST: #{request.body.read}"
@logger.info "Response: #{status} #{headers['Content-Type']} #{env['simpler.template']}\n"
end
end
end
end