Add read config from yml
This commit is contained in:
parent
85a383b328
commit
58f9d8c590
2 changed files with 15 additions and 5 deletions
|
@ -17,7 +17,7 @@ at_exit do
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
logger = Kemal::Logger.new
|
logger = Kemal::Logger.new
|
||||||
config.add_handler logger
|
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
|
config.add_handler Kemal::Handler::INSTANCE
|
||||||
|
|
||||||
server = HTTP::Server.new(config.port, config.handlers)
|
server = HTTP::Server.new(config.port, config.handlers)
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
|
require "yaml"
|
||||||
|
|
||||||
module Kemal
|
module Kemal
|
||||||
class Config
|
class Config
|
||||||
INSTANCE = Config.new
|
INSTANCE = Config.new
|
||||||
HANDLERS = [] of HTTP::Handler
|
HANDLERS = [] of HTTP::Handler
|
||||||
property ssl
|
property ssl, port, env, workers, public_folder
|
||||||
property port
|
|
||||||
property env
|
|
||||||
property workers
|
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@port = 3000
|
@port = 3000
|
||||||
@env = "development" unless @env
|
@env = "development" unless @env
|
||||||
@workers = 1
|
@workers = 1
|
||||||
|
@public_folder = "./public"
|
||||||
|
read_file
|
||||||
end
|
end
|
||||||
|
|
||||||
def scheme
|
def scheme
|
||||||
|
@ -24,6 +25,15 @@ module Kemal
|
||||||
def add_handler(handler : HTTP::Handler)
|
def add_handler(handler : HTTP::Handler)
|
||||||
HANDLERS << handler
|
HANDLERS << handler
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def self.config
|
def self.config
|
||||||
|
|
Loading…
Reference in a new issue