-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (140 loc) · 3.68 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
/*
* Parvaeres API
*
* Parvaeres magic deployment API
*
* API version: 0.0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package main
import (
"flag"
"log"
"net/http"
"os"
"strconv"
parvaeres "github.com/parvaeres/parvaeres/pkg/api"
"github.com/parvaeres/parvaeres/pkg/argocd"
"github.com/parvaeres/parvaeres/pkg/email"
"github.com/pkg/errors"
)
func main() {
var (
kubeconfig string
argoCDNamespace string
publicURL string
mailgunDomain string
mailgunAPIKey string
mailgunSender string
emailEnabled bool
adminToken string
)
log.Printf("Server started")
flag.StringVar(
&kubeconfig,
"kubeconfig",
lookupEnvOrString("PARVAERES_KUBECONFIG", ""),
"path to Kubernetes config file",
)
flag.StringVar(
&argoCDNamespace,
"argocd-namespace",
lookupEnvOrString("PARVAERES_ARGOCD_NAMESPACE", "argocd"),
"argocd Namespace",
)
flag.StringVar(
&publicURL,
"public-url",
lookupEnvOrString("PARVAERES_PUBLIC_URL", "http://poc.parvaeres.io/"),
"external URL where parvaeres-server is reacheable",
)
flag.StringVar(
&mailgunDomain,
"mailgun-domain",
lookupEnvOrString("PARVAERES_MAILGUN_DOMAIN", "poc.parvaeres.io"),
"mailgun domain",
)
flag.StringVar(
&mailgunAPIKey,
"mailgun-apikey",
lookupEnvOrString("PARVAERES_MAILGUN_APIKEY", ""),
"mailgun API key",
)
flag.StringVar(
&mailgunSender,
"mailgun-sender",
lookupEnvOrString("PARVAERES_MAILGUN_SENDER", "Parvaeres Support <support@poc.parvaeres.io>"),
"mailgun Sender email address",
)
flag.BoolVar(
&emailEnabled,
"email-enabled",
lookupEnvOrBool("PARVAERES_EMAIL_ENABLED", false),
"enable email delivery",
)
flag.StringVar(
&adminToken,
"admin-token",
lookupEnvOrString("PARVAERES_ADMIN_TOKEN", "parvaeres"),
"token to perform admin operations, pass as 'admintoken' header in API requests",
)
flag.Parse()
kubernetesConfig, err := argocd.GetKubernetesConfig(kubeconfig)
if err != nil {
log.Fatalf(errors.Wrap(err, "Couldn't get kubernetes config. Bailing out.").Error())
}
kubernetesClient, err := argocd.GetKubernetesClientSet(kubernetesConfig)
if err != nil {
log.Fatalf(errors.Wrap(err, "Couldn't get kubernetes client. Bailing out.").Error())
}
argoCDClient, err := argocd.GetArgoCDClientSet(kubernetesConfig)
if err != nil {
log.Fatalf(errors.Wrap(err, "Couldn't get ArgoCD client. Bailing out.").Error())
}
DefaultApiService := &parvaeres.DefaultApiService{
EmailProvider: &email.MailGun{
Domain: mailgunDomain,
APIKey: mailgunAPIKey,
Sender: mailgunSender,
},
GitopsProvider: &argocd.ArgoCD{
ArgoCDNamespace: argoCDNamespace,
ArgoCDclient: argoCDClient,
KubernetesClient: kubernetesClient,
},
FeatureFlagEmail: emailEnabled,
PublicURL: publicURL,
AdminToken: adminToken,
}
DefaultApiController := parvaeres.NewDefaultApiController(DefaultApiService)
router := parvaeres.NewRouter(DefaultApiController)
log.Fatal(http.ListenAndServe(":8080", router))
}
// FIXME: this could be improved, maybe cobra
// See: https://www.gmarik.info/blog/2019/12-factor-golang-flag-package/
func lookupEnvOrString(key string, defaultVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultVal
}
func lookupEnvOrInt(key string, defaultVal int) int {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.Atoi(val)
if err != nil {
log.Fatalf("LookupEnvOrInt[%s]: %v", key, err)
}
return v
}
return defaultVal
}
func lookupEnvOrBool(key string, defaultVal bool) bool {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("LookupEnvOrBool[%s]: %v", key, err)
}
return v
}
return defaultVal
}