-
Notifications
You must be signed in to change notification settings - Fork 0
first try #1
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
base: master
Are you sure you want to change the base?
first try #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .DS_Store | ||
| .idea | ||
| .env | ||
| log/app.log | ||
| .byebug_history |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| gem 'sequel' | ||
| gem 'sqlite3' | ||
| gem 'rack' | ||
| gem 'byebug' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| byebug (11.0.1) | ||
| rack (2.0.7) | ||
| sequel (5.19.0) | ||
| sqlite3 (1.4.1) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| byebug | ||
| rack | ||
| sequel | ||
| sqlite3 | ||
|
|
||
| BUNDLED WITH | ||
| 2.0.1 |
| 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.title %></p> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| require_relative 'config/environment' | ||
|
|
||
| use AppLogger, logdev: File.expand_path('log/app.log', __dir__) | ||
| run Simpler.application |
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,21 +14,31 @@ def initialize(env) | |
| def make_response(action) | ||
| @request.env['simpler.controller'] = self | ||
| @request.env['simpler.action'] = action | ||
| @request.env['simpler.params'].merge!(@request.params) | ||
|
|
||
| set_default_headers | ||
| send(action) | ||
| write_response | ||
| set_default_headers | ||
|
|
||
| @response.finish | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def status(status) | ||
| @response.status = status | ||
| end | ||
|
|
||
| def headers | ||
| @response.headers | ||
| end | ||
|
|
||
| def extract_name | ||
| self.class.name.match('(?<name>.+)Controller')[:name].downcase | ||
| end | ||
|
|
||
| def set_default_headers | ||
| return @response['Content-Type'] = 'text/plain' if @request.env['simpler.template'].is_a?(Hash) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. На самом деле, не факт что мы обязательно будет рендерить plain text. Может потребоваться рендеринг json, xml или pdf, например. |
||
| @response['Content-Type'] = 'text/html' | ||
| end | ||
|
|
||
|
|
@@ -43,7 +53,7 @@ def render_body | |
| end | ||
|
|
||
| def params | ||
| @request.params | ||
| @request.env['simpler.params'] | ||
| end | ||
|
|
||
| def render(template) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 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(log_composer(env, status, headers)) | ||
| [status, headers, body] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def log_composer(env, status, headers) | ||
| <<~HEREDOC | ||
| \nRequest: #{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']} | ||
| HEREDOC | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ def initialize(env) | |
| end | ||
|
|
||
| def render(binding) | ||
| template = File.read(template_path) | ||
| template = template_path.is_a?(Hash) ? template_path.first[1] : File.read(template_path) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь лучше разделить логику рендеринга ответов разных типов между отдельными классами. Для каждого типа ответа может быть свой собственный класс с методом |
||
|
|
||
| ERB.new(template).result(binding) | ||
| end | ||
|
|
@@ -31,7 +31,7 @@ def template | |
|
|
||
| def template_path | ||
| path = template || [controller.name, action].join('/') | ||
|
|
||
| return path if template.is_a?(Hash) | ||
| Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") | ||
| end | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше подключить это сразу в
config.ru, поскольку middleware там же подключается.