Skip to content

Commit

Permalink
refactor: improve main tests (#399)
Browse files Browse the repository at this point in the history
* refactor: improve main tests

* update

* remove commented tests

* update

* boot state

* improve tests

* update close fn

* update status routes
  • Loading branch information
davidebianchi authored Oct 25, 2024
1 parent fd8e073 commit 699ec10
Show file tree
Hide file tree
Showing 5 changed files with 445 additions and 446 deletions.
84 changes: 58 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"syscall"
"time"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/rond-authz/rond/core"
Expand Down Expand Up @@ -58,12 +59,52 @@ func entrypoint(shutdown chan os.Signal, env config.EnvironmentVariables) {
panic(err.Error())
}

app, err := setupService(env, log)
if err != nil {
panic(err.Error())
}
defer app.close()

srv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", env.HTTPPort),
Handler: app.router,
ReadHeaderTimeout: time.Second,
}
go func() {
log.WithField("port", env.HTTPPort).Info("Starting server")
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()

// sigterm signal sent from kubernetes
signal.Notify(shutdown, syscall.SIGTERM)
// We'll accept graceful shutdowns when quit via and SIGTERM (Ctrl+/)
// SIGINT (Ctrl+C), SIGKILL or SIGQUIT will not be caught.
helpers.GracefulShutdown(srv, shutdown, log, env.DelayShutdownSeconds)
}

type app struct {
router *mux.Router
sdkBootState *service.SDKBootState

closeFn []func()
}

func (a *app) close() {
for _, fn := range a.closeFn {
fn()
}
}

func setupService(env config.EnvironmentVariables, log *logrus.Logger) (*app, error) {
var closeFn []func()
if _, err := os.Stat(env.OPAModulesDirectory); err != nil {
log.WithFields(logrus.Fields{
"error": logrus.Fields{"message": err.Error()},
"opaDirectory": env.OPAModulesDirectory,
}).Errorf("load OPA modules failed")
return
return nil, err
}

opaModuleConfig, err := core.LoadRegoModule(env.OPAModulesDirectory)
Expand All @@ -72,7 +113,7 @@ func entrypoint(shutdown chan os.Signal, env config.EnvironmentVariables) {
"error": logrus.Fields{"message": err.Error()},
"opaDirectory": env.OPAModulesDirectory,
}).Errorf("failed rego file read")
return
return nil, err
}
log.WithField("opaModuleFileName", opaModuleConfig.Name).Trace("rego module successfully loaded")

Expand All @@ -88,7 +129,7 @@ func entrypoint(shutdown chan os.Signal, env config.EnvironmentVariables) {
"oasFilePath": env.APIPermissionsFilePath,
"oasApiPath": env.TargetServiceOASPath,
}).Errorf("failed to load oas")
return
return nil, err
}
log.WithFields(logrus.Fields{
"oasFilePath": env.APIPermissionsFilePath,
Expand All @@ -104,15 +145,15 @@ func entrypoint(shutdown chan os.Signal, env config.EnvironmentVariables) {
log.WithFields(logrus.Fields{
"error": logrus.Fields{"message": err.Error()},
}).Errorf("MongoDB setup failed")
return
return nil, err
}
defer func() {
closeFn = append(closeFn, func() {
if err := client.Disconnect(); err != nil {
log.WithFields(logrus.Fields{
"error": logrus.Fields{"message": err.Error()},
}).Errorf("MongoDB disconnection failed")
}
}()
})
mongoDriver = client
}

Expand All @@ -127,7 +168,7 @@ func entrypoint(shutdown chan os.Signal, env config.EnvironmentVariables) {
log.WithFields(logrus.Fields{
"error": logrus.Fields{"message": err.Error()},
}).Errorf("MongoDB setup failed")
return
return nil, err
}
mongoClientForUserBindings = client

Expand All @@ -136,7 +177,7 @@ func entrypoint(shutdown chan os.Signal, env config.EnvironmentVariables) {
log.WithFields(logrus.Fields{
"error": logrus.Fields{"message": err.Error()},
}).Errorf("MongoDB for builtin setup failed")
return
return nil, err
}
mongoClientForBuiltin = clientForBuiltin
}
Expand All @@ -160,26 +201,17 @@ func entrypoint(shutdown chan os.Signal, env config.EnvironmentVariables) {

// Routing
log.Trace("router setup initialization")
router, _ := service.SetupRouter(log, env, opaModuleConfig, oas, sdkBoot, mongoClientForUserBindings, registry)
log.Trace("router setup initialization done")

srv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", env.HTTPPort),
Handler: router,
ReadHeaderTimeout: time.Second,
router, err := service.SetupRouter(log, env, opaModuleConfig, oas, sdkBoot, mongoClientForUserBindings, registry)
if err != nil {
return nil, err
}
go func() {
log.WithField("port", env.HTTPPort).Info("Starting server")
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()
log.Trace("router setup initialization done")

// sigterm signal sent from kubernetes
signal.Notify(shutdown, syscall.SIGTERM)
// We'll accept graceful shutdowns when quit via and SIGTERM (Ctrl+/)
// SIGINT (Ctrl+C), SIGKILL or SIGQUIT will not be caught.
helpers.GracefulShutdown(srv, shutdown, log, env.DelayShutdownSeconds)
return &app{
router: router,
sdkBootState: sdkBoot,
closeFn: closeFn,
}, nil
}

func prepSDKOrDie(
Expand Down
Loading

0 comments on commit 699ec10

Please sign in to comment.