-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (159 loc) · 4.81 KB
/
main.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"path"
"runtime/pprof"
"time"
"github.com/gwd/session-scheduler/event"
"github.com/gwd/session-scheduler/id"
"github.com/gwd/session-scheduler/keyvalue"
"github.com/gwd/session-scheduler/timezones"
)
var kvs *keyvalue.KeyValueStore
const (
ScheduleDebug = "EventScheduleDebug"
ScheduleDebugVerbose = "EventScheduleDebugVerbose"
SearchAlgo = "EventSearchAlgo"
SearchDuration = "EventSearchDuration"
Validate = "EventValidate"
KeyDefaultLocation = "EventDefaultLocation"
VerificationCode = "ServeVerificationCode"
)
var DefaultLocation = "Europe/Berlin"
var DefaultLocationTZ event.TZLocation
var TimezoneList []string
// Template and data code expect CWD to be in the same directory as
// the binary; make this so.
func cwd() {
execpath, err := os.Executable()
if err != nil {
log.Printf("WARNING: Error getting executable path (%v), cannot cd to root", err)
return
}
execdir := path.Dir(execpath)
log.Printf("Changing to directory %s", execdir)
err = os.Chdir(execdir)
if err != nil {
log.Printf("WARNING: Chdir to %s failed: %v", execdir, err)
return
}
}
func main() {
var err error
TimezoneList, err = timezones.GetTimezoneList()
if err != nil {
log.Fatal("Getting timezone list: %v", err)
}
cwd()
templatesInit()
kvs, err = keyvalue.OpenFile("data/serverconfig.sqlite")
if err != nil {
log.Fatal("Opening serverconfig: %v", err)
}
adminPwd := flag.String("admin-password", "", "Set admin password")
flag.Var(kvs.GetFlagValue(KeyServeAddress), "address", "Address to serve http from")
flag.Var(kvs.GetFlagValue(ScheduleDebug), "sched-debug", "Debug level for logging (default 0)")
flag.Var(kvs.GetFlagValue(SearchAlgo), "searchalgo", "Search algorithm. Options are heuristic, genetic, and random.")
flag.Var(kvs.GetFlagValue(SearchDuration), "searchtime", "Duration to run search")
flag.Var(kvs.GetFlagValue(Validate), "validate", "Extra validation of schedule consistency")
flag.Var(kvs.GetFlagValue(KeyDefaultLocation), "default-location", "Default location to use for times")
flag.Var(kvs.GetFlagValue(LockingMethod), "servelock", "Server locking method. Valid options are none, quit, wait, and error (default quit)")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
{
vcode, err := kvs.Get(VerificationCode)
switch {
case err == keyvalue.ErrNoRows:
vcode = id.GenerateRawID(8)
if err = kvs.Set(VerificationCode, vcode); err != nil {
log.Fatalf("Setting Event Verification Code: %v", err)
}
case err != nil:
log.Fatalf("Getting Event Verification Code: %v", err)
}
}
locstring, err := kvs.Get(KeyDefaultLocation)
switch {
case err == keyvalue.ErrNoRows:
locstring = DefaultLocation
if err = kvs.Set(KeyDefaultLocation, locstring); err != nil {
log.Fatalf("Setting default location: %v", err)
} else {
log.Printf("Default location to %s", DefaultLocation)
}
case err != nil:
log.Fatalf("Getting default location: %v", err)
}
DefaultLocation = locstring
DefaultLocationTZ, err = event.LoadLocation(locstring)
if err != nil {
log.Fatalf("Couldn't load location %s: %v", locstring, err)
}
err = event.Load(event.EventOptions{AdminPwd: *adminPwd, DefaultLocation: locstring})
if err != nil {
log.Fatalf("Loading schedule data: %v", err)
}
cmd := flag.Arg(0)
if cmd == "" {
cmd = "serve"
}
switch cmd {
case "serve":
serve()
case "schedule":
MakeSchedule(false)
case "editTimetable":
EditTimetable()
default:
log.Fatalf("Unknown command: %s", cmd)
}
}
func getSearchDuration() time.Duration {
durationString, err := kvs.Get(SearchDuration)
var duration time.Duration
if err != nil {
duration, err = time.ParseDuration(durationString)
}
if err != nil {
// Default search time 5 seconds
return time.Second * 5
}
return duration
}
func MakeSchedule(async bool) error {
opt := event.SearchOptions{Async: async}
algostring, err := kvs.Get(SearchAlgo)
switch {
case err == keyvalue.ErrNoRows:
opt.Algo = event.SearchRandom
case err != nil:
log.Fatalf("Error getting keyvalue: %v", err)
default:
opt.Algo = event.SearchAlgo(algostring)
}
opt.Validate = kvs.GetBoolDef(Validate)
if kvs.GetBoolDef(ScheduleDebug) {
opt.DebugLevel = 1
opt.Debug = log.New(os.Stderr, "schedule.go ", log.LstdFlags)
if kvs.GetBoolDef(ScheduleDebugVerbose) {
opt.DebugLevel = 2
}
} else {
opt.Debug = log.New(ioutil.Discard, "schedule.go ", log.LstdFlags)
}
opt.SearchDuration = getSearchDuration()
return event.MakeSchedule(opt)
}