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) {
logger := server.Logger().Named("generate-ca")
logger := serverApp.Logger().Named("generate-ca")
logger = logger.With(
zap.String(generateCACurveName, curveName),

View file

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

View file

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