-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (148 loc) · 3.84 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
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"crypto/tls"
"embed"
"flag"
"log"
"net/http"
"github.com/osm/flen"
"golang.org/x/crypto/acme/autocert"
"github.com/osm/go-stack/auth"
"github.com/osm/go-stack/graphql"
"github.com/osm/go-stack/mailgun"
"github.com/osm/go-stack/postgres"
"github.com/osm/go-stack/redis"
"github.com/osm/go-stack/router"
)
//go:embed migrations
var migrationsFS embed.FS
//go:embed frontend/dist
var frontendFS embed.FS
func main() {
jwtPrivateKey := flag.String(
"jwt-private-key",
"LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1DNENBUUF3QlFZREsyVndCQ0lFSU9HYnJoM1FRYlg4L3N6RVdvb21OSXdKVm5xUmxiNDB5cnVUQ3BlanhabWcKLS0tLS1FTkQgUFJJVkFURSBLRVktLS0tLQo=",
"JWT private Ed25519 key",
)
jwtPublicKey := flag.String(
"jwt-public-key",
"LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUNvd0JRWURLMlZ3QXlFQTIwWEtWTzI5aGFpcEpnN0VYN0cxM3BYRDBrelpQSFVYMTRjQmlUbVJtN2s9Ci0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQo=",
"Auth public Ed25519 key",
)
jwtIssuer := flag.String(
"jwt-issuer",
"go-stack",
"JWT issuer",
)
fileUploadDir := flag.String(
"file-upload-dir",
"/tmp",
"File upload dir",
)
databaseURL := flag.String(
"database-url",
"pg://localhost:5432/go-stack?sslmode=disable",
"Database URL",
)
port := flag.String(
"port",
"4000",
"Listen port",
)
redisURL := flag.String(
"redis-url",
"redis://localhost:6379",
"Redis URL",
)
mailgunDomain := flag.String(
"mailgun-domain",
"",
"Mailgun domain",
)
mailgunAPIKey := flag.String(
"mailgun-api-key",
"",
"Mailgun API key",
)
mailgunSender := flag.String(
"mailgun-sender",
"",
"Mailgun sender",
)
enablePlayground := flag.Bool(
"enable-playground",
false,
"Enable GraphQL playground",
)
letsEncryptDomain := flag.String("lets-encrypt-domain", "", "let's encrypt domain name")
letsEncryptCertDir := flag.String("lets-encrypt-cert-dir", "certs", "let's encrypt cert dir")
flen.Parse()
auth := auth.New(
auth.WithIssuer(*jwtIssuer),
auth.WithPrivateKey(*jwtPrivateKey),
auth.WithPublicKey(*jwtPublicKey),
)
pg := postgres.NewClient(
postgres.WithConn(*databaseURL, migrationsFS),
)
defer pg.Close()
redis := redis.NewClient(
redis.WithConn(*redisURL),
)
defer redis.Close()
resolverOpts := []graphql.ResolverOption{
graphql.WithAuth(auth),
graphql.WithPostgres(pg),
graphql.WithRedis(redis),
graphql.WithFileUploadDir(*fileUploadDir),
}
if *mailgunDomain != "" && *mailgunAPIKey != "" && *mailgunSender != "" {
mailgun := mailgun.NewClient(
mailgun.WithConn(*mailgunDomain, *mailgunAPIKey),
mailgun.WithSender(*mailgunSender),
mailgun.WithPostgres(pg),
)
resolverOpts = append(resolverOpts, graphql.WithMailgun(mailgun))
}
gql := graphql.NewServer(
graphql.WithResolver(
graphql.NewResolver(resolverOpts...),
),
graphql.WithIntrospection(),
)
routerOpts := []router.Option{
router.WithAuth(auth),
router.WithPostgres(pg),
router.WithGraphql(gql),
router.WithFrontend(frontendFS),
router.WithFileUploadDir(*fileUploadDir),
}
if *enablePlayground {
routerOpts = append(routerOpts, router.WithPlayground())
}
router := router.NewRouter(routerOpts...)
if *letsEncryptDomain == "" {
if err := http.ListenAndServe(":"+*port, router); err != nil {
log.Fatalf("fatal error: %v\n", err)
}
} else {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(*letsEncryptDomain),
Cache: autocert.DirCache(*letsEncryptCertDir),
}
tlsConfig := certManager.TLSConfig()
tlsConfig.GetCertificate = func(h *tls.ClientHelloInfo) (*tls.Certificate, error) {
return certManager.GetCertificate(h)
}
server := http.Server{
Addr: ":443",
Handler: router,
TLSConfig: tlsConfig,
}
go http.ListenAndServe(":80", certManager.HTTPHandler(nil))
if err := server.ListenAndServeTLS("", ""); err != nil {
log.Fatalf("fatal error: %v\n", err)
}
}
}