feat: add server handle both HTTP and HTTPS
This commit is contained in:
@@ -92,8 +92,8 @@ docker run --rm --name goma-gateway \
|
|||||||
```
|
```
|
||||||
### 4. Healthcheck
|
### 4. Healthcheck
|
||||||
|
|
||||||
- Goma Gateway readiness: `/readyz`
|
- Goma Gateway health check: `/health/live`
|
||||||
- Routes health check: `/healthz`
|
- Routes health check: `health/live`
|
||||||
|
|
||||||
### 5. Simple deployment in docker compose file
|
### 5. Simple deployment in docker compose file
|
||||||
|
|
||||||
@@ -103,7 +103,7 @@ services:
|
|||||||
image: jkaninda/goma-gateway
|
image: jkaninda/goma-gateway
|
||||||
command: server
|
command: server
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: curl -f http://localhost/readyz || exit 1
|
test: curl -f http://localhost/heath/live || exit 1
|
||||||
interval: 30s
|
interval: 30s
|
||||||
retries: 5
|
retries: 5
|
||||||
start_period: 20s
|
start_period: 20s
|
||||||
@@ -121,8 +121,6 @@ Example of a configuration file
|
|||||||
```yaml
|
```yaml
|
||||||
# Goma Gateway configurations
|
# Goma Gateway configurations
|
||||||
gateway:
|
gateway:
|
||||||
########## Global settings
|
|
||||||
listenAddr: :80 #:443 SSL
|
|
||||||
# Proxy write timeout
|
# Proxy write timeout
|
||||||
writeTimeout: 15
|
writeTimeout: 15
|
||||||
# Proxy read timeout
|
# Proxy read timeout
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ docker run --rm --name goma-gateway \
|
|||||||
```
|
```
|
||||||
### 4. Healthcheck
|
### 4. Healthcheck
|
||||||
|
|
||||||
- Goma Gateway readiness: `/readyz`
|
- Goma Gateway health check: `/health/live`
|
||||||
- Routes health check: `/healthz`
|
- Routes health check: `health/live`
|
||||||
|
|
||||||
### 5. Simple deployment in docker compose file
|
### 5. Simple deployment in docker compose file
|
||||||
|
|
||||||
@@ -46,6 +46,7 @@ services:
|
|||||||
timeout: 10s
|
timeout: 10s
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "80:80"
|
||||||
|
- "443:443"
|
||||||
volumes:
|
volumes:
|
||||||
- ./config:/config/
|
- ./config:/config/
|
||||||
```
|
```
|
||||||
@@ -56,8 +57,6 @@ Example of a configuration file
|
|||||||
```yaml
|
```yaml
|
||||||
# Goma Gateway configurations
|
# Goma Gateway configurations
|
||||||
gateway:
|
gateway:
|
||||||
########## Global settings
|
|
||||||
listenAddr: :80 #:443 SSL
|
|
||||||
# Proxy write timeout
|
# Proxy write timeout
|
||||||
writeTimeout: 15
|
writeTimeout: 15
|
||||||
# Proxy read timeout
|
# Proxy read timeout
|
||||||
|
|||||||
@@ -10,5 +10,6 @@ services:
|
|||||||
timeout: 10s
|
timeout: 10s
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "80:80"
|
||||||
|
- "443:443"
|
||||||
volumes:
|
volumes:
|
||||||
- ./config:/config/
|
- ./config:/config/
|
||||||
@@ -23,14 +23,14 @@ spec:
|
|||||||
- containerPort: 80
|
- containerPort: 80
|
||||||
livenessProbe:
|
livenessProbe:
|
||||||
httpGet:
|
httpGet:
|
||||||
path: /healthz
|
path: /health/live
|
||||||
port: 80
|
port: 80
|
||||||
initialDelaySeconds: 15
|
initialDelaySeconds: 15
|
||||||
periodSeconds: 30
|
periodSeconds: 30
|
||||||
timeoutSeconds: 10
|
timeoutSeconds: 10
|
||||||
readinessProbe:
|
readinessProbe:
|
||||||
httpGet:
|
httpGet:
|
||||||
path: /readyz
|
path: /health/live
|
||||||
port: 80
|
port: 80
|
||||||
initialDelaySeconds: 15
|
initialDelaySeconds: 15
|
||||||
periodSeconds: 40
|
periodSeconds: 40
|
||||||
|
|||||||
2
goma.yml
2
goma.yml
@@ -1,7 +1,5 @@
|
|||||||
# Goma Gateway configurations
|
# Goma Gateway configurations
|
||||||
gateway:
|
gateway:
|
||||||
########## Global settings
|
|
||||||
listenAddr: :80 #:443 SSL
|
|
||||||
# Proxy write timeout
|
# Proxy write timeout
|
||||||
writeTimeout: 15
|
writeTimeout: 15
|
||||||
# Proxy read timeout
|
# Proxy read timeout
|
||||||
|
|||||||
@@ -85,7 +85,6 @@ func initConfig(configFile string) {
|
|||||||
}
|
}
|
||||||
conf := &GatewayConfig{
|
conf := &GatewayConfig{
|
||||||
GatewayConfig: Gateway{
|
GatewayConfig: Gateway{
|
||||||
ListenAddr: ":80",
|
|
||||||
WriteTimeout: 15,
|
WriteTimeout: 15,
|
||||||
ReadTimeout: 15,
|
ReadTimeout: 15,
|
||||||
IdleTimeout: 60,
|
IdleTimeout: 60,
|
||||||
|
|||||||
@@ -35,8 +35,10 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
|||||||
// Routes health check
|
// Routes health check
|
||||||
if !gateway.DisableHealthCheckStatus {
|
if !gateway.DisableHealthCheckStatus {
|
||||||
r.HandleFunc("/healthz", heath.HealthCheckHandler).Methods("GET")
|
r.HandleFunc("/healthz", heath.HealthCheckHandler).Methods("GET")
|
||||||
|
r.HandleFunc("/health/routes", heath.HealthCheckHandler).Methods("GET")
|
||||||
}
|
}
|
||||||
// Readiness
|
// Health check
|
||||||
|
r.HandleFunc("/health/live", heath.HealthReadyHandler).Methods("GET")
|
||||||
r.HandleFunc("/readyz", heath.HealthReadyHandler).Methods("GET")
|
r.HandleFunc("/readyz", heath.HealthReadyHandler).Methods("GET")
|
||||||
|
|
||||||
if gateway.RateLimiter != 0 {
|
if gateway.RateLimiter != 0 {
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import (
|
|||||||
func (gatewayServer GatewayServer) Start(ctx context.Context) error {
|
func (gatewayServer GatewayServer) Start(ctx context.Context) error {
|
||||||
logger.Info("Initializing routes...")
|
logger.Info("Initializing routes...")
|
||||||
route := gatewayServer.Initialize()
|
route := gatewayServer.Initialize()
|
||||||
|
logger.Debug("Routes count=%d Middlewares count=%d", len(gatewayServer.gateway.Routes), len(gatewayServer.middlewares))
|
||||||
logger.Info("Initializing routes...done")
|
logger.Info("Initializing routes...done")
|
||||||
tlsConfig := &tls.Config{}
|
tlsConfig := &tls.Config{}
|
||||||
var listenWithTLS = false
|
var listenWithTLS = false
|
||||||
@@ -41,8 +42,17 @@ func (gatewayServer GatewayServer) Start(ctx context.Context) error {
|
|||||||
listenWithTLS = true
|
listenWithTLS = true
|
||||||
|
|
||||||
}
|
}
|
||||||
srv := &http.Server{
|
// HTTP Server
|
||||||
Addr: gatewayServer.gateway.ListenAddr,
|
httpServer := &http.Server{
|
||||||
|
Addr: ":80",
|
||||||
|
WriteTimeout: time.Second * time.Duration(gatewayServer.gateway.WriteTimeout),
|
||||||
|
ReadTimeout: time.Second * time.Duration(gatewayServer.gateway.ReadTimeout),
|
||||||
|
IdleTimeout: time.Second * time.Duration(gatewayServer.gateway.IdleTimeout),
|
||||||
|
Handler: route, // Pass our instance of gorilla/mux in.
|
||||||
|
}
|
||||||
|
// HTTPS Server
|
||||||
|
httpsServer := &http.Server{
|
||||||
|
Addr: ":443",
|
||||||
WriteTimeout: time.Second * time.Duration(gatewayServer.gateway.WriteTimeout),
|
WriteTimeout: time.Second * time.Duration(gatewayServer.gateway.WriteTimeout),
|
||||||
ReadTimeout: time.Second * time.Duration(gatewayServer.gateway.ReadTimeout),
|
ReadTimeout: time.Second * time.Duration(gatewayServer.gateway.ReadTimeout),
|
||||||
IdleTimeout: time.Second * time.Duration(gatewayServer.gateway.IdleTimeout),
|
IdleTimeout: time.Second * time.Duration(gatewayServer.gateway.IdleTimeout),
|
||||||
@@ -53,36 +63,52 @@ func (gatewayServer GatewayServer) Start(ctx context.Context) error {
|
|||||||
printRoute(gatewayServer.gateway.Routes)
|
printRoute(gatewayServer.gateway.Routes)
|
||||||
}
|
}
|
||||||
// Set KeepAlive
|
// Set KeepAlive
|
||||||
srv.SetKeepAlivesEnabled(!gatewayServer.gateway.DisableKeepAlive)
|
httpServer.SetKeepAlivesEnabled(!gatewayServer.gateway.DisableKeepAlive)
|
||||||
|
httpsServer.SetKeepAlivesEnabled(!gatewayServer.gateway.DisableKeepAlive)
|
||||||
|
go func() {
|
||||||
|
logger.Info("Starting HTTP server listen=0.0.0.0:80")
|
||||||
|
if err := httpServer.ListenAndServe(); err != nil {
|
||||||
|
logger.Fatal("Error starting Goma Gateway HTTP server: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
logger.Info("Started Goma Gateway server on %v", gatewayServer.gateway.ListenAddr)
|
|
||||||
if listenWithTLS {
|
if listenWithTLS {
|
||||||
logger.Info("Server is running securely over HTTPS on %v ", gatewayServer.gateway.ListenAddr)
|
logger.Info("Starting HTTPS server listen=0.0.0.0:443")
|
||||||
if err := srv.ListenAndServeTLS("", ""); err != nil {
|
if err := httpsServer.ListenAndServeTLS("", ""); err != nil {
|
||||||
logger.Fatal("Error starting Goma Gateway server: %v", err)
|
logger.Fatal("Error starting Goma Gateway HTTPS server: %v", err)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err := srv.ListenAndServe(); err != nil {
|
|
||||||
logger.Fatal("Error starting Goma Gateway server: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(1)
|
wg.Add(2)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
shutdownCtx := context.Background()
|
shutdownCtx := context.Background()
|
||||||
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
|
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
||||||
_, err := fmt.Fprintf(os.Stderr, "error shutting down Goma Gateway server: %s\n", err)
|
_, err := fmt.Fprintf(os.Stderr, "error shutting down HTTP server: %s\n", err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
<-ctx.Done()
|
||||||
|
shutdownCtx := context.Background()
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if listenWithTLS {
|
||||||
|
if err := httpsServer.Shutdown(shutdownCtx); err != nil {
|
||||||
|
_, err := fmt.Fprintf(os.Stderr, "error shutting HTTPS server: %s\n", err)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
|||||||
@@ -133,12 +133,10 @@ type Route struct {
|
|||||||
|
|
||||||
// Gateway contains Goma Proxy Gateway's configs
|
// Gateway contains Goma Proxy Gateway's configs
|
||||||
type Gateway struct {
|
type Gateway struct {
|
||||||
// ListenAddr Defines the server listenAddr
|
// SSLCertFile SSL Certificate file
|
||||||
//
|
|
||||||
//e.g: localhost:8080
|
|
||||||
ListenAddr string `yaml:"listenAddr" env:"GOMA_LISTEN_ADDR, overwrite"`
|
|
||||||
SSLCertFile string `yaml:"sslCertFile" env:"GOMA_SSL_CERT_FILE, overwrite"`
|
SSLCertFile string `yaml:"sslCertFile" env:"GOMA_SSL_CERT_FILE, overwrite"`
|
||||||
SSLKeyFile string `yaml:"sslKeyFile" env:"GOMA_SSL_KEY_FILE, overwrite"`
|
// SSLKeyFile SSL Private key file
|
||||||
|
SSLKeyFile string `yaml:"sslKeyFile" env:"GOMA_SSL_KEY_FILE, overwrite"`
|
||||||
// WriteTimeout defines proxy write timeout
|
// WriteTimeout defines proxy write timeout
|
||||||
WriteTimeout int `yaml:"writeTimeout" env:"GOMA_WRITE_TIMEOUT, overwrite"`
|
WriteTimeout int `yaml:"writeTimeout" env:"GOMA_WRITE_TIMEOUT, overwrite"`
|
||||||
// ReadTimeout defines proxy read timeout
|
// ReadTimeout defines proxy read timeout
|
||||||
|
|||||||
Reference in New Issue
Block a user