-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathylcc.go
125 lines (116 loc) · 3.13 KB
/
ylcc.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
package main
import (
"encoding/json"
"flag"
"github.com/potix/utils/signal"
"github.com/potix/utils/configurator"
"github.com/potix/ylcc/collector"
"github.com/potix/ylcc/handler"
"github.com/potix/ylcc/processor"
"github.com/potix/ylcc/server"
"log"
"log/syslog"
)
type ylccProcessorConfig struct {
Mecabrc string `toml:"mecabrc"`
Font string `toml:"font"`
}
type ylccCollectorConfig struct {
ApiKeyFile string `toml:"apiKeyFile"`
DatabasePath string `toml:"databasePath"`
}
type ylccServerConfig struct {
AddrPort string `toml:"addrPort"`
TlsCertPath string `toml:"tlsCertPath"`
TlsKeyPath string `toml:"tlsKeyPath"`
}
type ylccLogConfig struct {
UseSyslog bool `toml:"useSyslog"`
}
type ylccConfig struct {
Verbose bool `toml:"verbose"`
Processor *ylccProcessorConfig `toml:"processor"`
Collector *ylccCollectorConfig `toml:"collector"`
Server *ylccServerConfig `toml:"server"`
Log *ylccLogConfig `toml:"log"`
}
type commandArguments struct {
configFile string
}
func verboseLoadedConfig(config *ylccConfig) {
if !config.Verbose {
return
}
j, err := json.Marshal(config)
if err != nil {
log.Printf("can not dump config: %v", err)
return
}
log.Printf("loaded config: %v", string(j))
}
func main() {
cmdArgs := new(commandArguments)
flag.StringVar(&cmdArgs.configFile, "config", "./ylcc.conf", "config file")
flag.Parse()
cf, err := configurator.NewConfigurator(cmdArgs.configFile)
if err != nil {
log.Fatalf("can not create configurator: %v", err)
}
var conf ylccConfig
err = cf.Load(&conf)
if err != nil {
log.Fatalf("can not load config: %v", err)
}
if conf.Processor == nil || conf.Collector == nil || conf.Server == nil {
log.Fatalf("invalid config")
}
if conf.Log != nil && conf.Log.UseSyslog {
logger, err := syslog.New(syslog.LOG_INFO|syslog.LOG_DAEMON, "ylcc")
if err != nil {
log.Fatalf("can not create syslog: %v", err)
}
log.SetOutput(logger)
}
verboseLoadedConfig(&conf)
apiKeys, err := configurator.LoadSecretFile(conf.Collector.ApiKeyFile)
if err != nil {
log.Fatalf("can not load secret file %v: %v", conf.Collector.ApiKeyFile, err)
}
if len(apiKeys) != 1 {
log.Fatalf("no api key")
}
cVerboseOpt := collector.Verbose(conf.Verbose)
newCollector, err := collector.NewCollector(
apiKeys,
conf.Collector.DatabasePath,
cVerboseOpt,
)
if err != nil {
log.Fatalf("can not create controller: %v", err)
}
pVerboseOpt := processor.Verbose(conf.Verbose)
newProcessor := processor.NewProcessor(
newCollector,
conf.Processor.Mecabrc,
conf.Processor.Font,
pVerboseOpt,
)
hVerboseOpt := handler.Verbose(conf.Verbose)
newHandler := handler.NewHandler(
newProcessor,
newCollector,
hVerboseOpt,
)
sVerboseOpt := server.Verbose(conf.Verbose)
tlsOpt := server.TLS(conf.Server.TlsCertPath, conf.Server.TlsKeyPath)
newServer, err := server.NewServer(conf.Server.AddrPort, newHandler, sVerboseOpt, tlsOpt)
if err != nil {
log.Fatalf("can not create server: %v", err)
}
err = newServer.Start()
if err != nil {
log.Fatalf("can not start server: %v", err)
}
signal.SignalWait(nil)
newServer.Stop()
}