-
Notifications
You must be signed in to change notification settings - Fork 296
hw #168
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
RamzanBersanov
wants to merge
2
commits into
psylone:master
Choose a base branch
from
RamzanBersanov:new_branch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
hw #168
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,4 +9,4 @@ | |
|
|
||
| <p><%= @time %></p> | ||
| </body> | ||
| </html> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_id %></p> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| require_relative 'middleware/custom_logger' | ||
| require_relative 'config/environment' | ||
|
|
||
| use CustomLogger, logdev: File.expand_path('log/app.log', __dir__) | ||
| run Simpler.application |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| module Simpler | ||
| class Controller | ||
|
|
||
| attr_reader :name, :request, :response | ||
| attr_reader :name, :request, :response, :status | ||
|
|
||
| def initialize(env) | ||
| @name = extract_name | ||
|
|
@@ -32,8 +32,14 @@ def set_default_headers | |
| @response['Content-Type'] = 'text/html' | ||
| end | ||
|
|
||
| def set_headers | ||
| @response.headers | ||
| end | ||
|
|
||
| def write_response | ||
| body = render_body | ||
| @request.env['simpler.response.status'] = @response.status | ||
| @request.env['simpler.response.header'] = set_headers['Content-Type'] | ||
|
|
||
| @response.write(body) | ||
| end | ||
|
|
@@ -43,11 +49,25 @@ def render_body | |
| end | ||
|
|
||
| def params | ||
| @request.params | ||
| @request.env['simpler.params'].merge!(@request.params) | ||
| end | ||
|
|
||
| def render(template) | ||
| @request.env['simpler.template'] = template | ||
| # def render(template, status = {}) | ||
| # case template | ||
| # when String | ||
| # set_headers['Content-Type'] = 'text/html' | ||
| # when Hash | ||
| # if template.has_key?(:plain) | ||
| # set_headers['Content-Type'] = 'text/plain' | ||
| # end | ||
| # end | ||
|
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. это тоже мусор в коде |
||
|
|
||
| def render(template, status = {}) | ||
| @request.env['simpler.template'] = template | ||
| if status.is_a?(Integer) && status >= 100 | ||
| status = status[:status] || template[:status] | ||
| @response.status = status | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,32 @@ 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 = {} | ||
| router_parts = @path.split('/') # ["", "tests", ":id"] | ||
| request_parts = path.split('/') # ["", "tests", "1"] | ||
|
|
||
| return false if router_parts.size != request_parts.size # false | ||
|
|
||
| router_parts.each_index do |index| | ||
| unless router_parts[index] == request_parts[index] # если части не совпадают полностью | ||
| match_data = router_parts[index].match(/^:(.+)/) # ["", "tests", ":id"] поочередно проверяются на соответствие /^:(.+)/ | ||
| # выбирается часть с :id | ||
| # match_data = [':id'] | ||
|
|
||
|
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. такие комментарии не пишут |
||
| return false if match_data.nil? | ||
|
|
||
| params[match_data[1].to_sym] = request_parts[index] # params[:id] = 1 (соответствующая часть пути) | ||
| end | ||
| end | ||
|
|
||
| env['simpler.params'] = params | ||
| end | ||
|
|
||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| I, [2023-10-01T20:28:44.252388 #6864] INFO -- : | ||
| ******************************************************** | ||
| Request: GET /tests/1 | ||
| Handler: TestsController#show | ||
| Parameters: {:id=>"1"} | ||
| Response: 200 [text/html] tests/show | ||
|
|
||
| I, [2023-10-01T20:28:50.838787 #6864] INFO -- : | ||
| ******************************************************** | ||
| Request: GET /tests | ||
| Handler: TestsController#index | ||
| Parameters: {} | ||
| Response: 200 [text/html] tests/index | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| require 'logger' | ||
|
|
||
| class CustomLogger | ||
|
|
||
| def initialize(app, **options) | ||
| @logger = Logger.new(options[:logdev] || STDOUT) | ||
| @app = app | ||
| end | ||
|
|
||
| def call(env) | ||
| response = @app.call(env) | ||
| @logger.info(message(env)) | ||
| response | ||
| end | ||
|
|
||
| def message(env) | ||
| controller = env['simpler.controller'] | ||
|
|
||
| if controller | ||
| <<~LOGGER | ||
|
|
||
| ******************************************************** | ||
| Request: #{env['REQUEST_METHOD']} #{env['PATH_INFO']} | ||
| Handler: #{controller.name.capitalize}Controller##{env['simpler.action']} | ||
| Parameters: #{env['simpler.params']} | ||
| Response: #{env['simpler.response.status']} [#{env['simpler.response.header']}] #{env['simpler.template']} | ||
| LOGGER | ||
| else | ||
| <<~LOGGER | ||
|
|
||
| Request: #{env['REQUEST_METHOD']} #{env['PATH_INFO']} | ||
| Response: #{} | ||
| LOGGER | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
такой комментарий ни какой пользы не несёт и должен быть удалён