From 909d75e98a2d27b6d831124effbe8f73ee4ce498 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 13 Mar 2016 17:09:40 -0400 Subject: add guff-stuff static files handler --- src/guff/config.cr | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 10 deletions(-) (limited to 'src/guff/config.cr') diff --git a/src/guff/config.cr b/src/guff/config.cr index 1be8d8d..43eaf13 100644 --- a/src/guff/config.cr +++ b/src/guff/config.cr @@ -1,4 +1,5 @@ require "option_parser" +require "yaml" module Guff class Config @@ -8,6 +9,7 @@ module Guff "data": "./data", "public": "./public", "environment": "development", + "stuff": "/usr/local/share/guff/stuff", } DIRS = %w{ @@ -38,10 +40,21 @@ module Guff private def parse_args(app, args) OptionParser.parse(args) do |p| + p.on( + "--config PATH", + "Read config from YAML file." + ) do |path| + YAML.parse(File.read(path)).each do |k, v| + self[k.as_s] = v.as_s + end + end + p.on( "-h HOST", "--host HOST", - "Host (defaults to \"localhost\")." + "Host (defaults to \"%s\")." % [ + DEFAULTS["host"] + ] ) do |host| self["host"] = host end @@ -49,7 +62,9 @@ module Guff p.on( "-p PORT", "--port PORT", - "Port (defaults to 8989)." + "Port (defaults to %s)." % [ + DEFAULTS["port"] + ] ) do |port| self["port"] = port end @@ -57,23 +72,37 @@ module Guff p.on( "-d PATH", "--data-dir PATH", - "Path to data directory (defaults to \"./data\")." + "Path to data directory (defaults to \"%s\")." % [ + DEFAULTS["data"], + ] ) do |path| self["data"] = path end - + p.on( - "-u PATH", "--public-dir PATH", - "Path to public directory (defaults to \"./public\")." + "Path to public directory (defaults to \"%s\")." % [ + DEFAULTS["public"], + ] ) do |path| self["public"] = path end + p.on( + "--stuff-dir PATH", + "Path to Guff static files directory (defaults to \"%s\")." % [ + DEFAULTS["stuff"], + ] + ) do |path| + self["stuff"] = path + end + p.on( "-e ENV", "--environment ENV", - "Environment (defaults to \"development\")." + "Environment (defaults to \"%s\")." % [ + DEFAULTS["environment"], + ] ) do |path| self["public"] = path end @@ -82,7 +111,7 @@ module Guff p.on( "--help", "Print usage" - ) do + ) do puts p.to_s exit 0 end @@ -90,9 +119,14 @@ module Guff # expand output directory paths and create them %w{data public}.each do |key| - self[key] = File.expand_path(File.join(File.dirname(app), self[key])) + # expand path (unless it is absolute) + self[key] = File.expand_path(File.join( + File.dirname(app), + self[key] + )) unless self[key][0] == '/' + # puts "DEBUG: %s: %s" % [key, self[key]] - Dir.mkdir(self[key], 0o770) unless File.directory?(self[key]) + Dir.mkdir(self[key], 0o770) unless Dir.exists?(self[key]) end end end -- cgit v1.2.3