Add log macro

This commit is contained in:
Sdogruyol 2016-01-08 18:44:37 +02:00
parent 265bfb1db2
commit aabfe4ff99
3 changed files with 14 additions and 4 deletions

View file

@ -18,6 +18,7 @@ describe "Logger" do
logger = Kemal::Logger.new logger = Kemal::Logger.new
logger.handler.should be_a File logger.handler.should be_a File
end end
it "writes to a file in production" do it "writes to a file in production" do
config = Kemal.config config = Kemal.config
config.env = "production" config.env = "production"

View file

@ -34,3 +34,10 @@ end
macro public_folder(path) macro public_folder(path)
Kemal.config.public_folder = {{path}} Kemal.config.public_folder = {{path}}
end end
# Logs to output stream.
# development: STDOUT in
# production: kemal.log
macro log(message)
Kemal::Logger::INSTANCE.write "#{{{message}}}\n"
end

View file

@ -8,8 +8,10 @@ module Kemal::Middleware
# Kemal.config.add_handler auth_handler # Kemal.config.add_handler auth_handler
# #
class HTTPBasicAuth < HTTP::Handler class HTTPBasicAuth < HTTP::Handler
BASIC = "Basic" BASIC = "Basic"
AUTH = "Authorization" AUTH = "Authorization"
AUTH_MESSAGE = "Could not verify your access level for that URL.\nYou have to login with proper credentials"
HEADER_LOGIN_REQUIRED = "Basic realm=\"Login Required\""
def initialize(@username, @password) def initialize(@username, @password)
end end
@ -23,8 +25,8 @@ module Kemal::Middleware
end end
end end
headers = HTTP::Headers.new headers = HTTP::Headers.new
headers["WWW-Authenticate"] = "Basic realm=\"Login Required\"" headers["WWW-Authenticate"] = HEADER_LOGIN_REQUIRED
HTTP::Response.new(401, "Could not verify your access level for that URL.\nYou have to login with proper credentials", headers, nil, "HTTP/1.1", nil) HTTP::Response.new(401, AUTH_MESSAGE, headers, nil, "HTTP/1.1", nil)
end end
def authorized?(value) def authorized?(value)