-
Notifications
You must be signed in to change notification settings - Fork 6
/
todolist.go
147 lines (131 loc) · 4.19 KB
/
todolist.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 (
"encoding/base64"
"fmt"
"github.com/Sfeir/golang-200/dao"
"github.com/Sfeir/golang-200/utils"
"github.com/Sfeir/golang-200/web"
logger "github.com/sirupsen/logrus"
"gopkg.in/urfave/cli.v1"
"os"
"strconv"
"time"
)
var (
// Version is the version of the software
Version string
// BuildStmp is the build date
BuildStmp string
// GitHash is the git build hash
GitHash string
port = 8020
logLevel = "warning"
db = ""
dbType = dao.DAOMockStr
migrationPath = "migration"
logFormat = utils.TextFormatter
statisticsDuration = 20 * time.Second
header, _ = base64.StdEncoding.DecodeString(
"ICAgICAgICAgLF8tLS1+fn5+fi0tLS0uXyAgICAgICAgIAogIF8sLF8sKl5fX19fICAgICAgX19fX19gYCpnKlwiKi" +
"wgCiAvIF9fLyAvJyAgICAgXi4gIC8gICAgICBcIF5AcSAgIGYgClsgIEBmIHwgQCkpICAgIHwgIHwgQCkpICAgbCA" +
"gMCBfLyAgCiBcYC8gICBcfl9fX18gLyBfXyBcX19fX18vICAgIFwgICAKICB8ICAgICAgICAgICBfbF9fbF8gICAg" +
"ICAgICAgIEkgICAKICB9ICAgICAgICAgIFtfX19fX19dICAgICAgICAgICBJICAKICBdICAgICAgICAgICAgfCB8I" +
"HwgICAgICAgICAgICB8ICAKICBdICAgICAgICAgICAgIH4gfiAgICAgICAgICAgICB8ICAKICB8ICAgICAgICAgIC" +
"AgICAgICAgICAgICAgICAgIHwgICAKICAgfCAgICAgICAgICAgICAgICAgICAgICAgICAgIHwg")
)
func main() {
// new app
app := cli.NewApp()
app.Name = utils.AppName
app.Usage = "todolist service launcher"
timeStmp, err := strconv.Atoi(BuildStmp)
if err != nil {
timeStmp = 0
}
app.Version = Version + ", build on " + time.Unix(int64(timeStmp), 0).String() + ", git hash " + GitHash
app.Authors = []cli.Author{{Name: "sfr"}}
app.Copyright = "Sfeir " + strconv.Itoa(time.Now().Year())
// command line flags
app.Flags = []cli.Flag{
cli.IntFlag{
Value: port,
Name: "port, p",
Usage: "Set the listening port of the webserver",
Destination: &port,
},
cli.StringFlag{
Value: db,
Name: "db, d",
Usage: "Set the database connection string (mongodb or postgresql)",
Destination: &db,
},
cli.StringFlag{
Value: dbType,
Name: "dbt, dt",
Usage: "Set the database type to use for the service (mongodb, postgresql or mock)",
Destination: &dbType,
},
cli.StringFlag{
Value: migrationPath,
Name: "mp, m",
Usage: "Set the database migration folder path",
Destination: &migrationPath,
},
cli.StringFlag{
Value: logLevel,
Name: "logl, l",
Usage: "Set the output log level (debug, info, warning, error)",
Destination: &logLevel,
},
cli.StringFlag{
Value: logFormat,
Name: "logf, f",
Usage: "Set the log formatter (logstash or text)",
Destination: &logFormat,
},
cli.DurationFlag{
Value: statisticsDuration,
Name: "statd, s",
Usage: "Set the statistics accumulation duration (ex : 1h, 2h30m, 30s, 300ms)",
Destination: &statisticsDuration,
},
}
// main action
// sub action are also possible
app.Action = func(c *cli.Context) error {
// print header
fmt.Println(string(header))
fmt.Print("* --------------------------------------------------- *\n")
fmt.Printf("| port : %d\n", port)
fmt.Printf("| db : %s\n", db)
fmt.Printf("| dbt : %s\n", dbType)
fmt.Printf("| mp : %s\n", migrationPath)
fmt.Printf("| logger level : %s\n", logLevel)
fmt.Printf("| logger format : %s\n", logFormat)
fmt.Printf("| statistic duration(s) : %0.f\n", statisticsDuration.Seconds())
fmt.Print("* --------------------------------------------------- *\n")
// init log options from command line params
err := utils.InitLog(logLevel, logFormat)
if err != nil {
logger.Warn("error setting log level, using debug as default")
}
// parse the database type
dbt, err := dao.ParseDBType(dbType)
if err != nil {
return err
}
// build the web server
webServer, err := web.BuildWebServer(db, migrationPath, dbt, statisticsDuration)
if err != nil {
return err
}
// serve
webServer.Run(":" + strconv.Itoa(port))
return nil
}
// run the app
err = app.Run(os.Args)
if err != nil {
logger.Fatalf("Run error %q\n", err)
}
}