mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-04-27 14:57:40 +00:00
use environment for configuration
This commit is contained in:
parent
9ac9e9e4be
commit
d9731944c2
2 changed files with 20 additions and 20 deletions
|
|
@ -23,7 +23,7 @@ class HarpBlog
|
||||||
PERSISTENT_FIELDS = %w[author title date timestamp link url tags].map(&:to_sym)
|
PERSISTENT_FIELDS = %w[author title date timestamp link url tags].map(&:to_sym)
|
||||||
TRANSIENT_FIELDS = %w[time slug body draft].map(&:to_sym)
|
TRANSIENT_FIELDS = %w[time slug body draft].map(&:to_sym)
|
||||||
FIELDS = PERSISTENT_FIELDS + TRANSIENT_FIELDS
|
FIELDS = PERSISTENT_FIELDS + TRANSIENT_FIELDS
|
||||||
attr_accessor *FIELDS
|
FIELDS.each { |f| attr_accessor f }
|
||||||
|
|
||||||
def initialize(fields = nil)
|
def initialize(fields = nil)
|
||||||
if fields
|
if fields
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,11 @@
|
||||||
# An HTTP interface for my Harp blog.
|
# An HTTP interface for my Harp blog.
|
||||||
|
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'optparse'
|
|
||||||
require 'sinatra'
|
require 'sinatra'
|
||||||
require './auth'
|
require './auth'
|
||||||
require './harp_blog'
|
require './harp_blog'
|
||||||
|
|
||||||
$config = {
|
CONFIG_DEFAULTS = {
|
||||||
auth: false,
|
auth: false,
|
||||||
dry_run: false,
|
dry_run: false,
|
||||||
path: File.expand_path('../test-blog', __FILE__),
|
path: File.expand_path('../test-blog', __FILE__),
|
||||||
|
|
@ -16,27 +15,28 @@ $config = {
|
||||||
port: 6706,
|
port: 6706,
|
||||||
}
|
}
|
||||||
|
|
||||||
OptionParser.new do |opts|
|
def env_value(name)
|
||||||
opts.banner = "Usage: server.rb [options]"
|
env_name = "BLOG_#{name.to_s.upcase}"
|
||||||
|
raw_value = ENV[env_name]
|
||||||
opts.on("-a", "--[no-]auth", "Enable authentication") do |auth|
|
case name
|
||||||
$config[:auth] = auth
|
when :auth, :dry_run
|
||||||
|
raw_value ? raw_value.to_i != 0 : false
|
||||||
|
when :port
|
||||||
|
raw_value ? raw_value.to_i : nil
|
||||||
|
else
|
||||||
|
raw_value
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
opts.on("-h", "--host [HOST]", "Host to bind") do |host|
|
$config = CONFIG_DEFAULTS.dup
|
||||||
$config[:host] = host
|
$config.each_key do |name|
|
||||||
|
value = env_value(name)
|
||||||
|
unless value.nil?
|
||||||
|
$config[name] = value
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
opts.on("-p", "--port [PORT]", "Port to bind") do |port|
|
unless File.directory?($config[:path])
|
||||||
$config[:port] = port.to_i
|
|
||||||
end
|
|
||||||
|
|
||||||
opts.on("-P", "--path [PATH]", "Path to Harp blog") do |path|
|
|
||||||
$config[:path] = path
|
|
||||||
end
|
|
||||||
end.parse!
|
|
||||||
|
|
||||||
unless File.exist?($config[:path])
|
|
||||||
raise RuntimeError.new("file not found: #{$config[:path]}")
|
raise RuntimeError.new("file not found: #{$config[:path]}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue