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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
log/*
8 changes: 6 additions & 2 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
class TestsController < Simpler::Controller

def index
@time = Time.now
@tests = Test.all
headers["Content-Type"] = "text/html"
status 201
end

def create

end

def show
@test = Test.where(id: params[:id]).first
end
end
10 changes: 8 additions & 2 deletions app/views/tests/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta charset="utf-8" />
<title>Index | Simpler application</title>
</head>
<body>
<h1>Simpler framework at work!</h1>

<p><%= @time %></p>

<ul>
<% @tests.each do |test| %>
<li><%= test.title %> (<%= test.level %>)</li>
<% end %>
</ul>
</body>
</html>
</html>
16 changes: 16 additions & 0 deletions app/views/tests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simpler</title>
</head>
<body>
<h1>Simpler framework at work!</h1>

<% if @test %>
<p><%= @test.title %> (<%= @test.level %>)</p>
<% else %>
<p> Not found </p>
<% end %>
</body>
</html>
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require_relative 'middleware/logger'
require_relative 'config/environment'


use AppLogger, logdev: File.expand_path('log/app.log', __dir__)
run Simpler.application
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Simpler.application.routes do
get '/tests', 'tests#index'
get '/tests/:id', 'tests#show'
post '/tests', 'tests#create'

end
32 changes: 21 additions & 11 deletions lib/simpler/application.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require 'yaml'
require 'singleton'
require 'sequel'
require_relative 'router'
require_relative 'controller'
require "yaml"
require "singleton"
require "sequel"
require_relative "router"
require_relative "controller"

module Simpler
class Application

include Singleton

attr_reader :db
Expand All @@ -26,11 +25,15 @@ def routes(&block)
@router.instance_eval(&block)
end

def call(env)
def call(env, logger)
@logger = logger
route = @router.route_for(env)
controller = route.controller.new(env)
return not_found_page unless route

controller = route.controller.new(env, @logger)
action = route.action

@logger.info("Handler: " + controller.class.to_s + "#" + action.to_s)
make_response(controller, action)
end

Expand All @@ -41,18 +44,25 @@ def require_app
end

def require_routes
require Simpler.root.join('config/routes')
require Simpler.root.join("config/routes")
end

def setup_database
database_config = YAML.load_file(Simpler.root.join('config/database.yml'))
database_config['database'] = Simpler.root.join(database_config['database'])
database_config = YAML.load_file(Simpler.root.join("config/database.yml"))
database_config["database"] = Simpler.root.join(database_config["database"])
@db = Sequel.connect(database_config)
end

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

def not_found_page
[
404,
{ "Content-Type" => "text/html" },
["<h1>404 Page Not Found</h1>"],
]
end
end
end
41 changes: 34 additions & 7 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
require_relative 'view'
require_relative "view"

module Simpler
class Controller
HTTP_CODE_STATUSES = {
"200" => "Ok",
"201" => "Created",
"400" => "Bad Request",
"404" => "Not Found",
"500" => "Internal Server Error",
}.freeze

attr_reader :name, :request, :response

def initialize(env)
def initialize(env, logger)
@logger = logger
@name = extract_name
@request = Rack::Request.new(env)
@response = Rack::Response.new
build_params(env)
end

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

set_default_headers
send(action)
write_response

log_info
@response.finish
end

private

def build_params(env)
array = env["REQUEST_PATH"].split("/")
@request.params[:id] = array[2].to_i if array[2].to_i && array[2].to_i != 0
@logger.info("Parameters: #{@request.params.to_s} ")
end

def extract_name
self.class.name.match('(?<name>.+)Controller')[:name].downcase
self.class.name.match("(?<name>.+)Controller")[:name].downcase
end

def set_default_headers
@response['Content-Type'] = 'text/html'
@response["Content-Type"] = "text/html"
end

def write_response
Expand All @@ -47,8 +63,19 @@ def params
end

def render(template)
@request.env['simpler.template'] = template
@request.env["simpler.template"] = template
end

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

def headers
@response
end

def log_info
@logger.info("Response: #{@response.status} #{HTTP_CODE_STATUSES[@response.status.to_s]} #{@response["Content-Type"]} #{@request.env["simpler.template_path"]}")
end
end
end
39 changes: 33 additions & 6 deletions lib/simpler/router.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require_relative 'router/route'
require_relative "router/route"

module Simpler
class Router

def initialize
@routes = []
end
Expand All @@ -16,16 +15,39 @@ def post(path, route_point)
end

def route_for(env)
method = env['REQUEST_METHOD'].downcase.to_sym
path = env['PATH_INFO']
method = env["REQUEST_METHOD"].downcase.to_sym
path = env["PATH_INFO"]

@routes.find { |route| route.match?(method, path) }
# @routes.find { |route| route.match?(method, path) }
check_nested_route(method, path)
find_route(method, path)
end

private

def check_nested_route(method, path)
path_details = path.split("/")

find_nested_route(method, path, path_details) if path_details[2].to_i
end

def find_nested_route(method, path, path_details)
controller_name = path_details[1]
path_details[2] = ":id"

path_for_matching = path_details.join("/")
nested_route = find_route(method, path_for_matching)
create_nested_route(method, path, nested_route, controller_name) if nested_route
end

def create_nested_route(method, path, nested_route, controller)
route = find_route(method, path)
route_point = controller + "#" + nested_route.action
add_route(method, path, route_point) unless route
end

def add_route(method, path, route_point)
route_point = route_point.split('#')
route_point = route_point.split("#")
controller = controller_from_string(route_point[0])
action = route_point[1]
route = Route.new(method, path, controller, action)
Expand All @@ -37,5 +59,10 @@ def controller_from_string(controller_name)
Object.const_get("#{controller_name.capitalize}Controller")
end

def find_route(method, path)
@routes.find do |route|
route.match?(method, path)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(method, path, controller, action)
end

def match?(method, path)
@method == method && path.match(@path)
@method == method && @path == path
end

end
Expand Down
33 changes: 23 additions & 10 deletions lib/simpler/view.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
require 'erb'
require "erb"

module Simpler
class View

VIEW_BASE_PATH = 'app/views'.freeze
VIEW_BASE_PATH = "app/views".freeze

def initialize(env)
@env = env
end

def render(binding)
def render(bind)
custom = check_for_custom_template
return custom unless custom.nil?

template = File.read(template_path)

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

private

def controller
@env['simpler.controller']
@env["simpler.controller"]
end

def action
@env['simpler.action']
@env["simpler.action"]
end

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

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

path = @template || [controller.name, action].join('/')
@env['simpler.template_path'] = "#{path}.html.erb"
Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb")
end

def check_for_custom_template
@template = template
return nil unless @template.instance_of?(Hash)

template_key = @template.keys[0]
template_value = @template[template_key]

case template_key
when :plain then template_value
end
end
end
end
19 changes: 19 additions & 0 deletions middleware/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'logger'

class AppLogger

def initialize(app, **option)
@logger = Logger.new(option[:logdev] || STDOUT)
@app = app
end

def call(env)
@logger.info(log_request(env))
@app.call(env, @logger)
end

def log_request(env)
"Request: " + env['REQUEST_METHOD'] + " " + env['PATH_INFO'] + "?" + env['QUERY_STRING']
end

end