Skip to content

Simpler enhancement #179

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 2 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
7 changes: 7 additions & 0 deletions app/controllers/error_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ErrorController < Simpler::Controller

def no_page
render plain: "Page not found"
status 404
end
end
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: "Text for render"
status 201
headers['Content-Type'] = 'text/plain'
end

def create
end

def show
render plain: "id #{@request.env[:id]}"
end

end
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>
<head>
<meta charset="utf-8">
<title>Show | Simpler application</title>
</head>
<body>
<h1>Simpler framework at work!</h1>
<p><%= @time %></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 'logger/logger'

use Applogger
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
11 changes: 7 additions & 4 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)
controller = route.controller.new(env)
action = route.action

make_response(controller, action)
begin
controller = route.controller.new(env)
action = route.action
make_response(controller, action)
rescue => e
make_response(ErrorController.new(env), 'no_page')
end
end

private
Expand Down
16 changes: 15 additions & 1 deletion lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def initialize(env)

def make_response(action)
@request.env['simpler.controller'] = self
@request.env['simpler.controllername'] = self.class.name
@request.env['simpler.action'] = action
@request.env[:id] = get_id(@request.env)

set_default_headers
send(action)
Expand All @@ -24,6 +26,18 @@ def make_response(action)

private

def get_id(env)
env['PATH_INFO'].match('\d+').to_s.to_i
end

def headers
@response
end

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

def extract_name
self.class.name.match('(?<name>.+)Controller')[:name].downcase
end
Expand All @@ -49,6 +63,6 @@ def params
def render(template)
@request.env['simpler.template'] = template
end

end

end
7 changes: 5 additions & 2 deletions lib/simpler/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ 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) }
route = @routes.find { |route| route.match?(method, path) }
end

private

def no_route
Route.new('get', '/no_page', ErrorController, 'no_page')
end

def add_route(method, path, route_point)
route_point = route_point.split('#')
controller = controller_from_string(route_point[0])
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 @@ -6,13 +6,17 @@ class Route

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

def path_for_id(path)
path.gsub(/:id/, '\d+')
end

def match?(method, path)
@method == method && path.match(@path)
@method == method && path.match(@path + '\z')
end

end
Expand Down
8 changes: 7 additions & 1 deletion lib/simpler/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ def initialize(env)
end

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

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

private

def check_plain
return nil if @env['simpler.template'].nil?

@env['simpler.template'][:plain]
end

def controller
@env['simpler.controller']
end
Expand Down
16 changes: 16 additions & 0 deletions log/app.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
I, [2024-03-08T20:57:11.299500 #42217] INFO -- request: GET/tests/312
I, [2024-03-08T20:57:11.303658 #42217] INFO -- Handler: TestsController
I, [2024-03-08T20:57:11.303703 #42217] INFO -- Parameter: #show
I, [2024-03-08T20:57:11.303720 #42217] INFO -- Responce: 200 OK [text/html] id 312
I, [2024-03-08T20:57:15.189928 #42217] INFO -- request: GET/tests/303
I, [2024-03-08T20:57:15.190177 #42217] INFO -- Handler: TestsController
I, [2024-03-08T20:57:15.190203 #42217] INFO -- Parameter: #show
I, [2024-03-08T20:57:15.190219 #42217] INFO -- Responce: 200 OK [text/html] id 303
I, [2024-03-08T20:57:19.222804 #42217] INFO -- request: GET/tests/500
I, [2024-03-08T20:57:19.223113 #42217] INFO -- Handler: TestsController
I, [2024-03-08T20:57:19.223143 #42217] INFO -- Parameter: #show
I, [2024-03-08T20:57:19.223162 #42217] INFO -- Responce: 200 OK [text/html] id 500
I, [2024-03-08T20:57:23.190205 #42217] INFO -- request: GET/tests/999
I, [2024-03-08T20:57:23.190521 #42217] INFO -- Handler: TestsController
I, [2024-03-08T20:57:23.190555 #42217] INFO -- Parameter: #show
I, [2024-03-08T20:57:23.190575 #42217] INFO -- Responce: 200 OK [text/html] id 999
21 changes: 21 additions & 0 deletions logger/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'logger'

class Applogger

def initialize(app)
file = File.open(File.expand_path(File.dirname(__dir__)) + '/log/app.log', File::WRONLY | File::APPEND | File::CREAT)
@logger = Logger.new(file)
@app = app
end

def call(env)
@logger.info('request'){ env['REQUEST_METHOD'] + env['PATH_INFO']}
responce = @app.call(env)

@logger.info('Handler'){env['simpler.controllername']}
@logger.info('Parameter'){env['QUERY_STRING'] + '#' + env['simpler.action']}
@logger.info('Responce'){responce[0].to_s + ' ' + Rack::Utils::HTTP_STATUS_CODES[responce[0]] + ' [' + responce[1]['Content-Type'] + '] ' + responce[2][0]}
responce
end

end