Skip to content

Development #163

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 15 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

log/app.log
6 changes: 2 additions & 4 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
class TestsController < Simpler::Controller

def index
@time = Time.now
end

def create

def show
@params = params[:id]
end

end
1 change: 0 additions & 1 deletion app/models/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
# Integer :level, default: 0
# end
class Test < Sequel::Model

end
1 change: 1 addition & 0 deletions app/views/tests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1> <%= @params%></h1>
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, 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/:id', 'tests#show'
get '/tests', 'tests#index'
post '/tests', 'tests#create'
end
31 changes: 31 additions & 0 deletions lib/middleware/applogger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class AppLogger
def initialize(app, **options)
@logger = Logger.new(options[:logdev] || STDOUT)
@app = app
end

def call(env)
status, headers, body = @app.call(env)

request = Rack::Request.new(env)

request_info(request)
responce_info(status, headers, request)

[status, headers, ["#{body}"]]
end

def request_info(request)
log = "Request: #{request.env['REQUEST_METHOD']} #{request.env['REQUEST_URI']}
Handler: #{request.env['simpler.controller'].class.name}##{request.env['simpler.action']}
Parameters: #{request.env['simpler.params']}"

@logger.info(log)
end

def responce_info(status, headers, request)
log = "Response: #{status} #{headers['Content-Type']} #{request.env['simpler.file_path']} "

@logger.info(log) if request.env['simpler.file_path']
end
end
2 changes: 0 additions & 2 deletions lib/simpler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require_relative 'simpler/application'

module Simpler

class << self
def application
Application.instance
Expand All @@ -12,5 +11,4 @@ def root
Pathname.new(File.expand_path('..', __dir__))
end
end

end
26 changes: 21 additions & 5 deletions lib/simpler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

module Simpler
class Application

include Singleton

attr_reader :db
Expand All @@ -20,6 +19,7 @@ def bootstrap!
setup_database
require_app
require_routes
require_logger
end

def routes(&block)
Expand All @@ -28,14 +28,27 @@ def routes(&block)

def call(env)
route = @router.route_for(env)
controller = route.controller.new(env)
action = route.action
if route
controller = route.controller.new(env)
action = route.action

make_response(controller, action)
make_response(controller, action)
else
response_not_found
end
end

private

def response_not_found
response = Rack::Response.new

response.status = 404
response['Content-Type'] = 'text/plain'
response.body = ['Not Found']
response.finish
end

def require_app
Dir["#{Simpler.root}/app/**/*.rb"].each { |file| require file }
end
Expand All @@ -44,6 +57,10 @@ def require_routes
require Simpler.root.join('config/routes')
end

def require_logger
require Simpler.root.join('lib/middleware/applogger')
end

def setup_database
database_config = YAML.load_file(Simpler.root.join('config/database.yml'))
database_config['database'] = Simpler.root.join(database_config['database'])
Expand All @@ -53,6 +70,5 @@ def setup_database
def make_response(controller, action)
controller.make_response(action)
end

end
end
42 changes: 38 additions & 4 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require_relative 'view'
require 'active_support/all'
require 'json'

module Simpler
class Controller

attr_reader :name, :request, :response

def initialize(env)
Expand All @@ -15,6 +16,8 @@ def make_response(action)
@request.env['simpler.controller'] = self
@request.env['simpler.action'] = action

parameter_assignment

set_default_headers
send(action)
write_response
Expand All @@ -24,6 +27,21 @@ def make_response(action)

private

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

def parameter_assignment
number = @request.env['PATH_INFO'].split('/').map(&:to_i).max

@params ||= { id: number }
@request.env['simpler.params'] = @params
end

def headers
@response
end

def extract_name
self.class.name.match('(?<name>.+)Controller')[:name].downcase
end
Expand All @@ -42,13 +60,29 @@ def render_body
View.new(@request.env).render(binding)
end

def params
@request.params
attr_reader :params

def format_response(hash)
@request.env['simpler.body'] = if hash[:json]
headers['Content-Type'] = 'text/json'
hash[:json].to_json
elsif hash[:xml]
headers['Content-Type'] = 'text/xml'
hash[:xml].to_xml
elsif hash[:plain]
headers['Content-Type'] = 'text/plain'
hash[:plain].to_s
elsif hash[:inline]
hash[:inline]
else
'Unknown format'
end
end

def render(template)
format_response(template) if template.instance_of?(Hash)

@request.env['simpler.template'] = template
end

end
end
2 changes: 0 additions & 2 deletions lib/simpler/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Simpler
class Router

def initialize
@routes = []
end
Expand Down Expand Up @@ -36,6 +35,5 @@ def add_route(method, path, route_point)
def controller_from_string(controller_name)
Object.const_get("#{controller_name.capitalize}Controller")
end

end
end
5 changes: 2 additions & 3 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Simpler
class Router
class Route

attr_reader :controller, :action

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

def match?(method, path)
@method == method && path.match(@path)
primary_key = @path.split('/')[-1]
@method == method && path.gsub(/\d/, primary_key).match(@path)
end

end
end
end
16 changes: 13 additions & 3 deletions lib/simpler/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

module Simpler
class View

VIEW_BASE_PATH = 'app/views'.freeze

def initialize(env)
@env = env
end

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

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

private

def render_body
@env['simpler.body']
end

def controller
@env['simpler.controller']
end
Expand All @@ -29,11 +32,18 @@ def template
@env['simpler.template']
end

def template_file_path
return unless controller.name

@env['simpler.file_path'] = [controller.name, action].join('/') + '.html.erb'
end

def template_path
template_file_path

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

Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb")
end

end
end