kemal/src/kemal/helpers.cr

85 lines
1.7 KiB
Crystal
Raw Normal View History

2016-02-14 18:37:55 +00:00
require "kilt"
2015-12-14 18:05:46 +00:00
2016-07-09 15:57:35 +00:00
CONTENTS = {} of String => MemoryIO -> String
def content_for(name : String, &block : MemoryIO -> String)
puts "Called content_for"
CONTENTS[name] = block
# CONTENTS[name] = block
end
def yield_content(name)
puts "Called yield_content"
CONTENTS[name].call
end
2015-12-14 18:05:46 +00:00
# Uses built-in ECR to render views.
# # Usage
# get '/' do
# render 'hello.ecr'
# end
macro render(filename, layout)
content = render {{filename}}
2016-07-09 15:57:35 +00:00
if CONTENTS.size > 0
puts "CONTENTS greater than 0"
render {{layout}}
else
render {{layout}}
end
2015-12-14 18:05:46 +00:00
end
macro render(filename, *args)
Kilt.render({{filename}}, {{*args}})
end
macro return_with(env, status_code = 200, response = "")
{{env}}.response.status_code = {{status_code}}
{{env}}.response.print {{response}}
next
end
2016-07-09 15:57:35 +00:00
2016-04-18 18:40:48 +00:00
# Adds given HTTP::Handler+ to handlers.
def add_handler(handler)
Kemal.config.add_handler handler
2016-01-05 12:20:40 +00:00
end
2015-12-27 09:53:54 +00:00
# Uses Kemal::Middleware::HTTPBasicAuth to easily add HTTP Basic Auth support.
def basic_auth(username, password)
auth_handler = Kemal::Middleware::HTTPBasicAuth.new(username, password)
2016-01-05 12:20:40 +00:00
add_handler auth_handler
2015-12-27 09:53:54 +00:00
end
2015-12-30 18:16:04 +00:00
2016-04-18 18:40:48 +00:00
# Sets public folder from which the static assets will be served.
# By default this is `/public` not `src/public`.
def public_folder(path)
Kemal.config.public_folder = path
2015-12-30 18:16:04 +00:00
end
2016-01-08 16:44:37 +00:00
# Logs to output stream.
# development: STDOUT in
# production: kemal.log
def log(message)
Kemal.config.logger.write "#{message}\n"
2016-01-08 16:44:37 +00:00
end
2016-01-17 11:08:12 +00:00
# Enables / Disables logging
def logging(status)
Kemal.config.logging = status
2016-02-14 13:15:28 +00:00
end
def logger(logger)
Kemal.config.logger = logger
Kemal.config.add_handler logger
2016-01-17 11:08:12 +00:00
end
2016-02-15 07:01:41 +00:00
def serve_static(status)
Kemal.config.serve_static = status
end
def headers(env, additional_headers)
env.response.headers.merge!(additional_headers)
end