1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
require "http/server"
require "ecr/macros"
require "json"
require "yaml"
require "secure_random"
require "sqlite3"
module Guff
# TODO: think about versioning a bit (semantic, datestamp, etc)
VERSION = "20160715"
end
require "./guff/**"
module Guff
module CLI
module Actions
abstract class Action
def self.run(config : Config)
new(config).run
end
def initialize(@config : Config)
end
abstract def run
end
class InitAction < Action
class Data
YAML.mapping({
init_sql: Array(String),
add_user: String,
test_posts: Array(String),
})
def self.load(system_dir : String) : Data
self.from_yaml(File.read(File.join(system_dir, "init.yaml")))
end
end
def initialize(config : Config)
super(config)
# read init data
@data = Data.load(@config.system_dir)
end
def run
STDERR.puts "Initializing data directory"
Dir.mkdir(@config.data_dir) unless Dir.exists?(@config.data_dir)
Guff::Database.new(@config.db_path) do |db|
@data.init_sql.each do |sql|
db.query(sql)
end
# gen random password and add admin user
# TODO: move these to init.yaml
password = Password.random_password
add_user(db, "Admin", "admin@admin", password)
add_user(db, "Test", "test@test", "test")
add_test_posts(db)
STDERR.puts "admin user: admin@admin, password: #{password}"
end
end
private def add_user(
db : Database,
name : String,
email : String,
password : String
) : Int64
db.query(@data.add_user, [
name,
email,
Password.create(password),
"admin",
])
db.last_insert_row_id.to_i64
end
private def add_test_posts(db)
# STDERR.puts "DEBUG: adding test data"
@data.test_posts.each do |sql|
db.query(sql)
end
end
end
class RunAction < Action
def run
STDERR.puts "Running web server"
check_dirs
# create context
context = Context.new(@config)
STDERR.puts "listening on %s:%s" % [@config.host, @config.port]
# run server
HTTP::Server.new(
@config.host,
@config.port.to_i,
Handlers.get(context)
).listen
end
private def check_dirs
{
system: @config.system_dir,
data: @config.data_dir,
}.each do |name, dir|
unless Dir.exists?(dir)
raise "missing #{name} directory: \"#{dir}\""
end
end
end
end
end
def self.run(app : String, args : Array(String))
begin
begin
# parse command-line arguments
config = Config.parse(app, args)
rescue err
raise "#{err}. Use --help for usage"
end
case config.mode
when "init"
Actions::InitAction.run(config)
when "run"
Actions::RunAction.run(config)
when "help"
# do nothing
else
# never reached
raise "unknown mode: #{config.mode}"
end
rescue err
STDERR.puts "ERROR: #{err}."
exit -1
end
end
end
end
Guff::CLI.run($0, ARGV)
|