-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
147 lines (120 loc) · 3.92 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
package main
import (
"errors"
"fmt"
"github.com/alexflint/go-arg"
"github.com/gorilla/mux"
"github.com/khyurri/speedlog/engine"
"github.com/khyurri/speedlog/engine/mongo"
"github.com/khyurri/speedlog/plugins"
"github.com/khyurri/speedlog/utils"
"log"
"net/http"
"sync"
"time"
)
const defaultTimezone = "UTC-0"
type params struct {
Mode string `arg:"positional" help:"Available modes: runserver, adduser, addproject. Default: runserver"`
Mongo string `arg:"-d" help:"Mode runserver. Mongodb url. Default 127.0.0.1:27017"`
JWTKey string `arg:"-j" help:"Mode runserver. JWT secret key."`
AllowOrigin string `arg:"-o" help:"Mode runserver. Add Access-Control-Allow-Origin header with passed by param value"`
TZ string `arg:"-t" help:"Mode runserver. Timezone. Default UTC±00:00."`
Graphite string `arg:"-g" help:"Mode runserver. Graphite host:port"`
EventsTTL int `arg:"--ttl" help:"Mode runserver. Time in seconds after which events are deleted. Default 0 — never"`
Project string `arg:"-r" help:"Modes runserver, addproject. Project title."`
Login string `arg:"-l" help:"Mode adduser. Login for new user"`
Password string `arg:"-p" help:"Mode adduser. Password for new user"`
Verbose bool `arg:"-v" help:"All modes. Add debug messages"`
}
func parseTZ(timezone string) (*time.Location, error) {
if timezone == defaultTimezone {
return time.FixedZone(timezone, 0), nil
}
return time.LoadLocation(timezone)
}
func addProjectMode(cliParams *params, dbEngine mongo.DataStore) (err error) {
if len(cliParams.Project) > 0 {
err = dbEngine.AddProject(cliParams.Project)
} else {
err = errors.New("--project param not found")
}
return
}
// init logger
func initLogger(verbose bool) {
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Llongfile)
if verbose {
utils.Level = utils.LG_DEBUG
}
}
func main() {
cliParams := ¶ms{}
////////////////////////////////////////
//
// DEFAULTS
cliParams.Mode = "runserver"
cliParams.Mongo = "127.0.0.1:27017"
cliParams.TZ = defaultTimezone
//
////////////////////////////////////////
arg.MustParse(cliParams)
initLogger(cliParams.Verbose)
dbEngine, err := mongo.New("speedlog", cliParams.Mongo)
utils.Ok(err)
defer dbEngine.Session.Close()
location, err := parseTZ(cliParams.TZ)
utils.Ok(err)
env := engine.NewEnv(dbEngine, cliParams.JWTKey, location)
if len(cliParams.AllowOrigin) > 0 {
env.AllowOrigin = cliParams.AllowOrigin
}
switch cliParams.Mode {
case "runserver":
if len(cliParams.JWTKey) == 0 {
fmt.Println("Cannot start server. Required jwtkey")
return
}
////////////////////////////////////////////////////////////////////////////////
// LOAD PLUGINS
var plgns []plugins.Plugin
var stopped sync.WaitGroup
sigStop := make(plugins.SigChan)
if len(cliParams.Graphite) > 0 {
graphite := plugins.NewGraphite(cliParams.Graphite, time.Minute*1)
plgns = append(plgns, graphite)
}
if cliParams.EventsTTL > 0 {
cleaner := plugins.NewCleaner(cliParams.EventsTTL, time.Minute*1)
plgns = append(plgns, cleaner)
}
go plugins.LoadPlugins(plgns, sigStop, &stopped, dbEngine)
// END LOAD PLUGINS
////////////////////////////////////////////////////////////////////////////////
if len(cliParams.Project) > 0 {
_ = dbEngine.AddProject(cliParams.Project)
}
r := mux.NewRouter()
env.ExportEventRoutes(r)
env.ExportUserRoutes(r)
env.ExportProjectRoutes(r)
srv := &http.Server{
Handler: r,
Addr: ":8012",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
err = srv.ListenAndServe()
// UNLOAD PLUGINS
sigStop <- struct{}{}
stopped.Wait() // TODO: add timeout
case "adduser":
err := env.DBEngine.AddUser(cliParams.Login, cliParams.Password)
utils.Ok(err)
case "addproject":
err = addProjectMode(cliParams, dbEngine)
utils.Ok(err)
default:
utils.Ok(errors.New(fmt.Sprintf("unknown mode '%s'", cliParams.Mode)))
}
}