refactoring of code

This commit is contained in:
Jonas Kaninda
2024-11-15 15:42:02 +01:00
parent 5a6c30ec95
commit a5823890a8
5 changed files with 19 additions and 14 deletions

View File

@@ -37,13 +37,13 @@ var ServerCmd = &cobra.Command{
} }
ctx := context.Background() ctx := context.Background()
g := pkg.GatewayServer{} g := pkg.GatewayServer{}
gs, err := g.Config(configFile) gs, err := g.Config(configFile, ctx)
if err != nil { if err != nil {
fmt.Printf("Could not load configuration: %v\n", err) fmt.Printf("Could not load configuration: %v\n", err)
os.Exit(1) os.Exit(1)
} }
gs.SetEnv() gs.SetEnv()
if err := gs.Start(ctx); err != nil { if err := gs.Start(); err != nil {
fmt.Printf("Could not start server: %v\n", err) fmt.Printf("Could not start server: %v\n", err)
os.Exit(1) os.Exit(1)

View File

@@ -16,6 +16,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import ( import (
"context"
"fmt" "fmt"
"github.com/jkaninda/goma-gateway/internal/middlewares" "github.com/jkaninda/goma-gateway/internal/middlewares"
"github.com/jkaninda/goma-gateway/pkg/logger" "github.com/jkaninda/goma-gateway/pkg/logger"
@@ -31,7 +32,7 @@ import (
) )
// Config reads config file and returns Gateway // Config reads config file and returns Gateway
func (GatewayServer) Config(configFile string) (*GatewayServer, error) { func (GatewayServer) Config(configFile string, ctx context.Context) (*GatewayServer, error) {
if util.FileExists(configFile) { if util.FileExists(configFile) {
buf, err := os.ReadFile(configFile) buf, err := os.ReadFile(configFile)
if err != nil { if err != nil {
@@ -44,7 +45,8 @@ func (GatewayServer) Config(configFile string) (*GatewayServer, error) {
return nil, fmt.Errorf("parsing the configuration file %q: %w", configFile, err) return nil, fmt.Errorf("parsing the configuration file %q: %w", configFile, err)
} }
return &GatewayServer{ return &GatewayServer{
ctx: nil, ctx: ctx,
configFile: configFile,
version: c.Version, version: c.Version,
gateway: c.GatewayConfig, gateway: c.GatewayConfig,
middlewares: c.Middlewares, middlewares: c.Middlewares,
@@ -59,14 +61,15 @@ func (GatewayServer) Config(configFile string) (*GatewayServer, error) {
} }
logger.Info("Using configuration file: %s", ConfigFile) logger.Info("Using configuration file: %s", ConfigFile)
util.SetEnv("GOMA_CONFIG_FILE", configFile) util.SetEnv("GOMA_CONFIG_FILE", ConfigFile)
c := &GatewayConfig{} c := &GatewayConfig{}
err = yaml.Unmarshal(buf, c) err = yaml.Unmarshal(buf, c)
if err != nil { if err != nil {
return nil, fmt.Errorf("parsing the configuration file %q: %w", ConfigFile, err) return nil, fmt.Errorf("parsing the configuration file %q: %w", ConfigFile, err)
} }
return &GatewayServer{ return &GatewayServer{
ctx: nil, ctx: ctx,
configFile: ConfigFile,
gateway: c.GatewayConfig, gateway: c.GatewayConfig,
middlewares: c.Middlewares, middlewares: c.Middlewares,
}, nil }, nil
@@ -98,7 +101,8 @@ func (GatewayServer) Config(configFile string) (*GatewayServer, error) {
} }
logger.Info("Generating new configuration file...done") logger.Info("Generating new configuration file...done")
return &GatewayServer{ return &GatewayServer{
ctx: nil, ctx: ctx,
configFile: ConfigFile,
gateway: c.GatewayConfig, gateway: c.GatewayConfig,
middlewares: c.Middlewares, middlewares: c.Middlewares,
}, nil }, nil

View File

@@ -29,7 +29,7 @@ import (
) )
// Start / Start starts the server // Start / Start starts the server
func (gatewayServer GatewayServer) Start(ctx context.Context) error { func (gatewayServer GatewayServer) Start() 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.Debug("Routes count=%d, Middlewares count=%d", len(gatewayServer.gateway.Routes), len(gatewayServer.middlewares))
@@ -56,7 +56,7 @@ func (gatewayServer GatewayServer) Start(ctx context.Context) error {
} }
// Handle graceful shutdown // Handle graceful shutdown
return gatewayServer.shutdown(ctx, httpServer, httpsServer, listenWithTLS) return gatewayServer.shutdown(httpServer, httpsServer, listenWithTLS)
} }
func (gatewayServer GatewayServer) createServer(addr string, handler http.Handler, tlsConfig *tls.Config) *http.Server { func (gatewayServer GatewayServer) createServer(addr string, handler http.Handler, tlsConfig *tls.Config) *http.Server {
@@ -90,13 +90,13 @@ func (gatewayServer GatewayServer) startServers(httpServer, httpsServer *http.Se
return nil return nil
} }
func (gatewayServer GatewayServer) shutdown(ctx context.Context, httpServer, httpsServer *http.Server, listenWithTLS bool) error { func (gatewayServer GatewayServer) shutdown(httpServer, httpsServer *http.Server, listenWithTLS bool) error {
quit := make(chan os.Signal, 1) quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit <-quit
logger.Info("Shutting down Goma Gateway...") logger.Info("Shutting down Goma Gateway...")
shutdownCtx, cancel := context.WithTimeout(ctx, 10*time.Second) shutdownCtx, cancel := context.WithTimeout(gatewayServer.ctx, 10*time.Second)
defer cancel() defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil { if err := httpServer.Shutdown(shutdownCtx); err != nil {

View File

@@ -39,8 +39,9 @@ func TestStart(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error initializing config: %s", err.Error()) t.Fatalf("Error initializing config: %s", err.Error())
} }
ctx := context.Background()
g := GatewayServer{} g := GatewayServer{}
gatewayServer, err := g.Config(configFile) gatewayServer, err := g.Config(configFile, ctx)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -54,9 +55,8 @@ func TestStart(t *testing.T) {
t.Fatalf("expected a status code of 200, got %v", resp.StatusCode) t.Fatalf("expected a status code of 200, got %v", resp.StatusCode)
} }
} }
ctx := context.Background()
go func() { go func() {
err = gatewayServer.Start(ctx) err = gatewayServer.Start()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return

View File

@@ -113,6 +113,7 @@ type ErrorResponse struct {
} }
type GatewayServer struct { type GatewayServer struct {
ctx context.Context ctx context.Context
configFile string
version string version string
gateway Gateway gateway Gateway
middlewares []Middleware middlewares []Middleware