Cleanup in server app

This commit is contained in:
Peter 2021-01-18 18:41:07 +01:00
parent 0138b57782
commit a76d1d1c04
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
3 changed files with 14 additions and 23 deletions

View file

@ -57,7 +57,7 @@ func init() {
} }
func runGenerateCA(_ *cobra.Command, _ []string) { func runGenerateCA(_ *cobra.Command, _ []string) {
logger := server.Logger().Named("generate-ca") logger := serverApp.Logger().Named("generate-ca")
logger = logger.With( logger = logger.With(
zap.String(generateCACurveName, curveName), zap.String(generateCACurveName, curveName),

View file

@ -1,30 +1,20 @@
package main package main
import ( import (
"fmt"
"gitlab.com/inetmock/inetmock/internal/app" "gitlab.com/inetmock/inetmock/internal/app"
dns "gitlab.com/inetmock/inetmock/internal/endpoint/handler/dns/mock" dns "gitlab.com/inetmock/inetmock/internal/endpoint/handler/dns/mock"
http "gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/mock" http "gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/mock"
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/proxy" "gitlab.com/inetmock/inetmock/internal/endpoint/handler/http/proxy"
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/metrics" "gitlab.com/inetmock/inetmock/internal/endpoint/handler/metrics"
"gitlab.com/inetmock/inetmock/internal/endpoint/handler/tls/interceptor" "gitlab.com/inetmock/inetmock/internal/endpoint/handler/tls/interceptor"
"go.uber.org/zap"
) )
var ( var (
server app.App serverApp app.App
) )
func main() { func main() {
logger, _ := zap.NewProduction() serverApp = app.NewApp("inetmock", "INetMock is lightweight internet mock").
defer func() {
if err := logger.Sync(); err != nil {
fmt.Println(err.Error())
}
}()
app.NewApp("inetmock", "INetMock is lightweight internet mock").
WithHandlerRegistry( WithHandlerRegistry(
http.AddHTTPMock, http.AddHTTPMock,
dns.AddDNSMock, dns.AddDNSMock,
@ -37,6 +27,7 @@ func main() {
WithHealthChecker(). WithHealthChecker().
WithCertStore(). WithCertStore().
WithEventStream(). WithEventStream().
WithEndpointManager(). WithEndpointManager()
MustRun()
serverApp.MustRun()
} }

View file

@ -19,13 +19,13 @@ var (
) )
func startINetMock(_ *cobra.Command, _ []string) { func startINetMock(_ *cobra.Command, _ []string) {
rpcAPI := rpc.NewINetMockAPI(server) rpcAPI := rpc.NewINetMockAPI(serverApp)
logger := server.Logger().Named("inetmock").With(zap.String("command", "serve")) logger := serverApp.Logger().Named("inetmock").With(zap.String("command", "serve"))
for endpointName, endpointHandler := range server.Config().EndpointConfigs() { for endpointName, endpointHandler := range serverApp.Config().EndpointConfigs() {
handlerSubConfig := server.Config().Viper().Sub(strings.Join([]string{config.EndpointsKey, endpointName, config.OptionsKey}, ".")) handlerSubConfig := serverApp.Config().Viper().Sub(strings.Join([]string{config.EndpointsKey, endpointName, config.OptionsKey}, "."))
endpointHandler.Options = handlerSubConfig endpointHandler.Options = handlerSubConfig
if err := server.EndpointManager().CreateEndpoint(endpointName, endpointHandler); err != nil { if err := serverApp.EndpointManager().CreateEndpoint(endpointName, endpointHandler); err != nil {
logger.Warn( logger.Warn(
"error occurred while creating endpoint", "error occurred while creating endpoint",
zap.String("endpointName", endpointName), zap.String("endpointName", endpointName),
@ -35,7 +35,7 @@ func startINetMock(_ *cobra.Command, _ []string) {
} }
} }
server.EndpointManager().StartEndpoints() serverApp.EndpointManager().StartEndpoints()
if err := rpcAPI.StartServer(); err != nil { if err := rpcAPI.StartServer(); err != nil {
logger.Error( logger.Error(
"failed to start gRPC API", "failed to start gRPC API",
@ -43,12 +43,12 @@ func startINetMock(_ *cobra.Command, _ []string) {
) )
} }
<-server.Context().Done() <-serverApp.Context().Done()
logger.Info( logger.Info(
"App context canceled - shutting down", "App context canceled - shutting down",
) )
rpcAPI.StopServer() rpcAPI.StopServer()
server.EndpointManager().ShutdownEndpoints() serverApp.EndpointManager().ShutdownEndpoints()
} }