2024-10-27 06:10:27 +01:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Copyright 2024 Jonas Kaninda
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/jkaninda/goma-gateway/internal/logger"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (gatewayServer GatewayServer) Start(ctx context.Context) error {
|
|
|
|
|
logger.Info("Initializing routes...")
|
|
|
|
|
route := gatewayServer.Initialize()
|
|
|
|
|
logger.Info("Initializing routes...done")
|
|
|
|
|
srv := &http.Server{
|
|
|
|
|
Addr: gatewayServer.gateway.ListenAddr,
|
|
|
|
|
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.
|
|
|
|
|
}
|
|
|
|
|
if !gatewayServer.gateway.DisableDisplayRouteOnStart {
|
|
|
|
|
printRoute(gatewayServer.gateway.Routes)
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
|
|
logger.Info("Started Goma Gateway server on %v", gatewayServer.gateway.ListenAddr)
|
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
|
|
|
logger.Error("Error starting Goma Gateway server: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
shutdownCtx := context.Background()
|
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
|
|
|
|
_, err := fmt.Fprintf(os.Stderr, "error shutting down Goma Gateway server: %s\n", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
wg.Wait()
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-27 07:24:50 +01:00
|
|
|
func waitForReady(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
timeout time.Duration,
|
|
|
|
|
endpoint string,
|
|
|
|
|
) error {
|
|
|
|
|
client := http.Client{}
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
for {
|
|
|
|
|
req, err := http.NewRequestWithContext(
|
|
|
|
|
ctx,
|
|
|
|
|
http.MethodGet,
|
|
|
|
|
endpoint,
|
|
|
|
|
nil,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error making request: %s\n", err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
|
fmt.Println("Endpoint is ready!")
|
|
|
|
|
resp.Body.Close()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
default:
|
|
|
|
|
if time.Since(startTime) >= timeout {
|
|
|
|
|
return fmt.Errorf("timeout reached while waiting for endpoint")
|
|
|
|
|
}
|
|
|
|
|
// wait a little while between checks
|
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|