-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontman.go
159 lines (140 loc) · 4.18 KB
/
frontman.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
package frontman
import (
"context"
"crypto/tls"
"fmt"
"github.com/Frontman-Labs/frontman/api"
"github.com/julienschmidt/httprouter"
"net/http"
"github.com/Frontman-Labs/frontman/config"
"github.com/Frontman-Labs/frontman/gateway"
"github.com/Frontman-Labs/frontman/log"
"github.com/Frontman-Labs/frontman/plugins"
"github.com/Frontman-Labs/frontman/service"
"github.com/Frontman-Labs/frontman/ssl"
)
// Frontman contains the backend services and the router
type Frontman struct {
router *gateway.APIGateway
service *httprouter.Router
backendServices service.ServiceRegistry
conf *config.Config
log log.Logger
}
// NewFrontman creates a new Frontman instance with a Redis client connection factory
func NewFrontman(conf *config.Config, log log.Logger) (*Frontman, error) {
ctx := context.Background()
// Create a new serviceRegistry instance
serviceRegistry, err := service.NewServiceRegistry(ctx, conf.GlobalConfig.ServiceType, conf)
if err != nil {
return nil, err
}
// Create management API router
servicesRouter := api.NewServicesRouter(serviceRegistry)
// Load plugins
var plug []plugins.FrontmanPlugin
if conf.PluginConfig.Enabled {
plug, err = plugins.LoadPlugins(conf.PluginConfig.Order)
if err != nil {
return nil, err
}
}
// Create new APIGateway instance
apiGateway := gateway.NewAPIGateway(serviceRegistry, plug, conf, log)
// Create the Frontman instance
return &Frontman{
router: apiGateway,
service: servicesRouter,
backendServices: serviceRegistry,
conf: conf,
log: log,
}, nil
}
func (gw *Frontman) Start() error {
apiAddr := gw.conf.APIConfig.Addr
if apiAddr == "" {
apiAddr = "0.0.0.0:8080"
}
gatewayAddr := gw.conf.GatewayConfig.Addr
if gatewayAddr == "" {
gatewayAddr = "0.0.0.0:8000"
}
var apiHandler http.Handler
var gatewayHandler http.Handler
apiHandler = gw.service
var apicert *tls.Certificate
if gw.conf.APIConfig.SSL.Enabled {
cert, err := ssl.LoadCert(gw.conf.APIConfig.SSL.Cert, gw.conf.APIConfig.SSL.Key)
if err != nil {
return err
}
apicert = cert
}
apiHandler = gw.service
api := createServer(apiAddr, apiHandler, apicert)
go func() {
if err := startServer(api); err != nil {
gw.log.Fatal(err)
}
}()
gw.log.WithFields(log.InfoLevel, fmt.Sprintf("Started Frontman API on %s", apiAddr), log.Bool("tls_enabled", gw.conf.APIConfig.SSL.Enabled))
var gwcert *tls.Certificate
gatewayHandler = gw.router
if gw.conf.GatewayConfig.SSL.Enabled {
cert, err := ssl.LoadCert(gw.conf.GatewayConfig.SSL.Cert, gw.conf.GatewayConfig.SSL.Key)
if err != nil {
return err
}
gwcert = cert
// Redirect HTTP traffic to HTTPS
httpAddr := "0.0.0.0:80"
httpRedirect := createRedirectServer(httpAddr, gatewayAddr)
gw.log.Infof("Started HTTP redirect server on %s", httpAddr)
go func() {
if err := startServer(httpRedirect); err != nil {
gw.log.Fatal(err)
}
}()
}
gatewayHandler = gw.router
gateway := createServer(gatewayAddr, gatewayHandler, gwcert)
gw.log.WithFields(log.InfoLevel, fmt.Sprintf("Started Frontman Frontman on %s", gatewayAddr), log.Bool("tls_enabled", gw.conf.GatewayConfig.SSL.Enabled))
if err := startServer(gateway); err != nil {
return err
}
return nil
}
func createRedirectServer(addr string, redirectAddr string) *http.Server {
redirect := func(w http.ResponseWriter, req *http.Request) {
httpsURL := "https://" + req.Host + req.URL.Path
http.Redirect(w, req, httpsURL, http.StatusMovedPermanently)
}
return &http.Server{
Addr: addr,
Handler: http.HandlerFunc(redirect),
}
}
func createServer(addr string, handler http.Handler, cert *tls.Certificate) *http.Server {
server := &http.Server{
Addr: addr,
Handler: handler,
}
if cert != nil {
server.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{*cert},
}
}
return server
}
func startServer(server *http.Server) error {
if server.TLSConfig != nil {
if err := server.ListenAndServeTLS("", ""); err != nil {
return fmt.Errorf("Failed to start server with TLS: %w", err)
}
} else {
if err := server.ListenAndServe(); err != nil {
return fmt.Errorf("Failed to start server without TLS: %w", err)
}
}
return nil
}