From 58f9d8c590bfd377932402fbe3598c01094ef5e2 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Sat, 12 Dec 2015 20:27:46 +0200 Subject: [PATCH] Add read config from yml --- src/kemal.cr | 2 +- src/kemal/config.cr | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/kemal.cr b/src/kemal.cr index 5064ec1..64830bf 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -17,7 +17,7 @@ at_exit do config = Kemal.config logger = Kemal::Logger.new config.add_handler logger - config.add_handler Kemal::StaticFileHandler.new("./public") + config.add_handler Kemal::StaticFileHandler.new(config.public_folder) config.add_handler Kemal::Handler::INSTANCE server = HTTP::Server.new(config.port, config.handlers) diff --git a/src/kemal/config.cr b/src/kemal/config.cr index 69b8b37..5baa392 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -1,16 +1,17 @@ +require "yaml" + module Kemal class Config INSTANCE = Config.new HANDLERS = [] of HTTP::Handler - property ssl - property port - property env - property workers + property ssl, port, env, workers, public_folder def initialize @port = 3000 @env = "development" unless @env @workers = 1 + @public_folder = "./public" + read_file end def scheme @@ -24,6 +25,15 @@ module Kemal def add_handler(handler : HTTP::Handler) HANDLERS << handler end + + def read_file + path = File.expand_path("config.yml", Dir.working_directory) + if File.exists?(path) + data = YAML.load(File.read(path)) as Hash + public_folder = File.expand_path("./#{data["public_folder"]}", Dir.working_directory) + @public_folder = public_folder + end + end end def self.config