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
4 changes: 4 additions & 0 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ def create

end

def show
render inline: '<%= Test.find(params[:id].to_i) %>'
end

end
Empty file added app/views/tests/create.html.erb
Empty file.
1 change: 1 addition & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative 'config/environment'

use AppLogger
run Simpler.application
1 change: 1 addition & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative '../lib/simpler'
require_relative '../lib/middleware/app_logger'

Simpler.application.bootstrap!
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 '/test/:id', 'tests#show'
end
37 changes: 37 additions & 0 deletions lib/middleware/app_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'logger'

class AppLogger
def initialize(app, **options)
@logger = Logger.new(Simpler.root.join('log/app.log'))
@app = app
end

def call(env)
@response = @app.call(env)
@logger.info(request(env))
@logger.info(handler(env))
@logger.info(parameters(env))
@logger.info(response(env))

@response
end

private

def request(env)
"Request: #{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
end

def handler(env)
"Handler: #{env['simpler.controller'].class.name}##{env['simpler.action']}"
end

def parameters(env)
"Parameters: #{env['simpler.params']}"
end

def response(env)
"Response: #{@response[0]} [#{@response[1]['Content-Type']}] #{env['simpler.template_path']}"
end

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

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

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

make_response(controller, action)
make_response(controller, action, params)
end

private
Expand All @@ -50,8 +53,12 @@ def setup_database
@db = Sequel.connect(database_config)
end

def make_response(controller, action)
controller.make_response(action)
def make_response(controller, action, params)
controller.make_response(action, params)
end

def not_found
Rack::Response.new('Not Found', 404, { 'Content-Type' => 'text/plain' }).finish
end

end
Expand Down
27 changes: 23 additions & 4 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ def initialize(env)
@response = Rack::Response.new
end

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

set_default_headers
send(action)
Expand All @@ -32,6 +33,19 @@ def set_default_headers
@response['Content-Type'] = 'text/html'
end

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

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

def set_render_params(**options)
set_header('Content-Type', options[:content_type]) if options.has_key? :content_type
set_status(options[:status]) if options.has_key? :status
end

def write_response
body = render_body

Expand All @@ -43,11 +57,16 @@ def render_body
end

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

def render(template)
@request.env['simpler.template'] = template
def render(*args, **options)
set_render_params(**options)
@request.env['simpler.template'] = if args.length.zero?
options.except(:content_type, :status)
else
{ erb: args }
end
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(path)
route
end

private
Expand Down
8 changes: 6 additions & 2 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Simpler
class Router
class Route

attr_reader :controller, :action
attr_reader :controller, :action, :params

def initialize(method, path, controller, action)
@method = method
Expand All @@ -12,7 +12,11 @@ def initialize(method, path, controller, action)
end

def match?(method, path)
@method == method && path.match(@path)
@method == method && (path =~ Regexp.new(@path.gsub(/:(\w+)/, '\w+'))) == 0
end

def set_params(path)
@params = path.match(Regexp.new(@path.gsub(/:(\w+)/, '(?<\1>.+)'))).named_captures.transform_keys!(&:to_sym)
end

end
Expand Down
34 changes: 30 additions & 4 deletions lib/simpler/view.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
require 'erb'
require 'json'

module Simpler
class View

VIEW_BASE_PATH = 'app/views'.freeze
VIEW_TYPES = %i[erb plain json inline].freeze

def initialize(env)
@env = env
@view_type = @env['simpler.template'].nil? ? :erb : set_view_type
@view_body = @env['simpler.template'].nil? ? nil : @env['simpler.template'][@view_type]
end

def set_view_type
available_types = @env['simpler.template'].keys.intersection(VIEW_TYPES)
available_types.nil? ? :erb : available_types[0]
end

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

ERB.new(template).result(binding)
send @view_type
end

private

def erb
template = File.read(template_path)
ERB.new(template).result(@binding)
end

def plain
@view_body
end

def json
@view_body.to_json
end

def inline
ERB.new(@env['simpler.template'][@view_type]).result(@binding)
end

def controller
@env['simpler.controller']
end
Expand All @@ -26,11 +52,11 @@ def action
end

def template
@env['simpler.template']
@view_body
end

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

Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb")
end
Expand Down
12 changes: 12 additions & 0 deletions log/app.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
I, [2023-04-02T12:47:44.696636 #22331] INFO -- : Request: GET /tests
I, [2023-04-02T12:47:44.696723 #22331] INFO -- : Handler: TestsController#index
I, [2023-04-02T12:47:44.696751 #22331] INFO -- : Parameters: {}
I, [2023-04-02T12:47:44.696774 #22331] INFO -- : Response: 200 [text/html] tests/index
I, [2023-04-02T12:48:58.203803 #22331] INFO -- : Request: GET /test/1
I, [2023-04-02T12:48:58.212994 #22331] INFO -- : Handler: TestsController#show
I, [2023-04-02T12:48:58.213071 #22331] INFO -- : Parameters: {:id=>"1"}
I, [2023-04-02T12:48:58.213111 #22331] INFO -- : Response: 200 [text/html]
I, [2023-04-02T12:50:48.216637 #22331] INFO -- : Request: POST /tests
I, [2023-04-02T12:50:48.216711 #22331] INFO -- : Handler: TestsController#create
I, [2023-04-02T12:50:48.216745 #22331] INFO -- : Parameters: {}
I, [2023-04-02T12:50:48.216773 #22331] INFO -- : Response: 200 [text/html] tests/create