/
main.go
executable file
·134 lines (121 loc) · 3.41 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
package main
import (
"fmt"
"os"
"github.com/astaxie/beego"
"github.com/sirupsen/logrus"
"github.com/snagles/docker-registry-manager/app/models"
_ "github.com/snagles/docker-registry-manager/app/routers"
"github.com/urfave/cli"
)
const (
appVersion = "3.0.2"
)
func main() {
app := cli.NewApp()
app.Name = "Docker Registry Manager"
app.Usage = "Connect to, view, and manage multiple private Docker registries"
var registriesFile, logLevel, keyPath, certPath string
var enableHTTPS bool
var appPort int
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "port, p",
Usage: "port to use for the registry manager `port`",
Value: 8080,
Destination: &appPort,
EnvVar: "MANAGER_PORT",
},
cli.StringFlag{
Name: "registries, r",
Usage: "file location of the registries.yml `/app/registries.yml`",
EnvVar: "MANAGER_REGISTRIES",
Destination: ®istriesFile,
},
cli.StringFlag{
Name: "log-level, l",
Usage: "log-level `warn`",
Value: "info",
EnvVar: "MANAGER_LOG_LEVEL",
Destination: &logLevel,
},
// Beego HTTPS options
cli.BoolFlag{
Name: "enable-https, e",
Usage: "enable https `true or false`",
EnvVar: "MANAGER_ENABLE_HTTPS",
Destination: &enableHTTPS,
},
cli.StringFlag{
Name: "tls-key, k",
Usage: "tls certificate path and name `/app/key.key`",
EnvVar: "MANAGER_KEY",
Destination: &keyPath,
},
cli.StringFlag{
Name: "tls-certificate, cert",
Usage: "tls certificate path and name `/app/certificate.crt`",
EnvVar: "MANAGER_CERTIFICATE",
Destination: &certPath,
},
}
app.Action = func(ctx *cli.Context) {
manager.AllRegistries.LoadConfig(registriesFile)
err := setlevel(logLevel)
if err != nil {
logrus.Fatal(err)
}
// Beego configuration
beego.BConfig.AppName = "docker-registry-manager"
beego.BConfig.RunMode = "dev"
beego.BConfig.Listen.EnableAdmin = true
beego.BConfig.CopyRequestBody = true
beego.BConfig.WebConfig.ViewsPath = "views"
// set http port
if enableHTTPS {
beego.BConfig.Listen.HTTPSPort = appPort
// make sure we have both key and cert
if keyPath == "" {
logrus.Fatal("HTTPS enabled, but no key file provided")
} else {
beego.BConfig.Listen.HTTPSKeyFile = keyPath
}
if certPath == "" {
logrus.Fatal("HTTPS enabled, but no certificate file provided")
} else {
beego.BConfig.Listen.HTTPSCertFile = certPath
}
// if we're not using https just use standard http
} else {
beego.BConfig.Listen.HTTPPort = appPort
}
// add template functions
beego.AddFuncMap("shortenDigest", DigestShortener)
beego.AddFuncMap("statToSeconds", StatToSeconds)
beego.AddFuncMap("bytefmt", ByteFmt)
beego.AddFuncMap("bytefmtdiff", ByteDiffFmt)
beego.AddFuncMap("timeAgo", TimeAgo)
beego.AddFuncMap("oneIndex", func(i int) int { return i + 1 })
beego.Run()
}
app.Run(os.Args)
}
func setlevel(level string) error {
switch {
case level == "panic":
logrus.SetLevel(logrus.PanicLevel)
case level == "fatal":
logrus.SetLevel(logrus.FatalLevel)
case level == "error":
logrus.SetLevel(logrus.ErrorLevel)
case level == "warn":
logrus.SetLevel(logrus.WarnLevel)
case level == "info":
logrus.SetLevel(logrus.InfoLevel)
case level == "debug":
logrus.SetLevel(logrus.DebugLevel)
default:
return fmt.Errorf("Unrecognized log level: %s", level)
}
return nil
}