Skip to content

Commit

Permalink
expose logger to more generated code (#63)
Browse files Browse the repository at this point in the history
experimental -- remove serverParams.Serve abstraction so core.Serve can access logger
  • Loading branch information
anz-rfc authored and andrewemeryanz committed Jun 2, 2021
1 parent 5df0e3b commit 9a66700
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
5 changes: 3 additions & 2 deletions codegen/arrai/auto/svc_app.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let sysl = //{./sysl};
}}

// BuildDownstreamClients ...
func BuildDownstreamClients(hooks *core.Hooks, cfg *config.DefaultConfig) (*DownstreamClients, error) {
func BuildDownstreamClients(ctx context.Context, hooks *core.Hooks, cfg *config.DefaultConfig) (*DownstreamClients, error) {
downstreamConfig := cfg.GenCode.Downstream.(*DownstreamConfig)
if downstreamConfig == nil {
downstreamConfig = &DownstreamConfig{}
Expand Down Expand Up @@ -117,6 +117,7 @@ let sysl = //{./sysl};
ctx,
&DownstreamConfig{}, createService, &${serviceInterface}{},
func(
ctx context.Context,
cfg *config.DefaultConfig,
serviceIntf interface{},
hooks *core.Hooks,
Expand All @@ -130,7 +131,7 @@ let sysl = //{./sysl};
)

${cond {clientDeps: $`
clients, err := BuildDownstreamClients(hooks, cfg)
clients, err := BuildDownstreamClients(ctx, hooks, cfg)
if err != nil {
return nil, err
}
Expand Down
101 changes: 92 additions & 9 deletions core/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand All @@ -13,6 +14,10 @@ import (
"github.com/anz-bank/sysl-go/config"
"github.com/spf13/afero"
"gopkg.in/yaml.v2"

"github.com/anz-bank/pkg/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)

type serveContextKey int
Expand All @@ -31,7 +36,7 @@ func ConfigFileSystemOnto(ctx context.Context, fs afero.Fs) context.Context {
func Serve(
ctx context.Context,
downstreamConfig, createService, serviceInterface interface{},
newManager func(cfg *config.DefaultConfig, serviceIntf interface{}, hooks *Hooks) (interface{}, error),
newManager func(ctx context.Context, cfg *config.DefaultConfig, serviceIntf interface{}, hooks *Hooks) (interface{}, error),
) error {
MustTypeCheckCreateService(createService, serviceInterface)
customConfig := NewZeroCustomConfig(reflect.TypeOf(downstreamConfig), GetAppConfigType(createService))
Expand Down Expand Up @@ -64,25 +69,31 @@ func Serve(
serviceIntf := createServiceResult[0].Interface()
hooksIntf := createServiceResult[1].Interface()

manager, err := newManager(defaultConfig, serviceIntf, hooksIntf.(*Hooks))
server := &autogenServer{
name: "nameless-autogenerated-app", // TODO source the application name from somewhere
}

var pkgLoggerConfigs []log.Config = nil // TODO do we need to expose this?
var logrusLogger *logrus.Logger = nil // TODO do we need to expose this or can we delete it?

ctx = InitialiseLogging(ctx, pkgLoggerConfigs, logrusLogger)
// OK, we have a ctx that contains a logger now!

manager, err := newManager(ctx, defaultConfig, serviceIntf, hooksIntf.(*Hooks))
if err != nil {
return err
}

opts := make([]ServerOption, 0)

switch manager := manager.(type) {
case Manager: // aka RESTful service manager
opts = append(opts, WithRestManager(manager))
server.restManager = manager
case GrpcServerManager:
opts = append(opts, WithGrpcServerManager(manager))
server.grpcServerManager = &manager
default:
panic(fmt.Errorf("Wrong type returned from newManager()"))
}

applicationName := "nameless-autogenerated-app" // TODO source the application name from somewhere
serverParams := NewServerParams(ctx, applicationName, opts...)
return serverParams.Start()
return server.start(ctx)
}

// LoadCustomConfig populates the given zero customConfig value with configuration data.
Expand Down Expand Up @@ -273,3 +284,75 @@ func describeYAMLForType(w io.Writer, t reflect.Type, commonTypes map[reflect.Ty
panic(fmt.Errorf("describeYAMLForType: Unhandled type: %v", t))
}
}

// experimental fork of core.ServerParams

type autogenServer struct {
name string
restManager Manager
grpcServerManager *GrpcServerManager
prometheusRegistry *prometheus.Registry
}

func (s *autogenServer) start(ctx context.Context) error {
// precondition: ctx must have been threaded through InitialiseLogging and hence contain a logger

// prepare the middleware
mWare := prepareMiddleware(ctx, s.name, s.prometheusRegistry)

var restIsRunning, grpcIsRunning bool

listeners := make([]func() error, 0)

// Make the listener function for the REST Admin server
if s.restManager != nil && s.restManager.AdminServerConfig() != nil {
log.Info(ctx, "found AdminServerConfig for REST")
listenAdmin, err := configureAdminServerListener(ctx, s.restManager, s.prometheusRegistry, mWare.admin)
if err != nil {
return err
}
listeners = append(listeners, listenAdmin)
} else {
log.Info(ctx, "no AdminServerConfig for REST was found")
}

// Make the listener function for the REST Public server
if s.restManager != nil && s.restManager.PublicServerConfig() != nil {
log.Info(ctx, "found PublicServerConfig for REST")
listenPublic, err := configurePublicServerListener(ctx, s.restManager, mWare.public)
if err != nil {
return err
}
listeners = append(listeners, listenPublic)
restIsRunning = true
} else {
log.Info(ctx, "no PublicServerConfig for REST was found")
}

// Make the listener function for the gRPC Public server.
if s.grpcServerManager != nil && s.grpcServerManager.GrpcPublicServerConfig != nil {
log.Info(ctx, "found GrpcPublicServerConfig for gRPC")
listenPublicGrpc := configurePublicGrpcServerListener(ctx, *s.grpcServerManager)
listeners = append(listeners, listenPublicGrpc)
grpcIsRunning = true
} else {
log.Info(ctx, "no GrpcPublicServerConfig for gRPC was found")
}

// Refuse to start and panic if neither of the public servers are enabled.
if !restIsRunning && !grpcIsRunning {
err := errors.New("REST and gRPC servers cannot both be nil")
log.Error(ctx, err)
panic(err)
}

// Run all listeners for all configured servers and block until the first one terminates.
errChan := make(chan error, 1)
for i := range listeners {
listener := listeners[i]
go func() {
errChan <- listener()
}()
}
return <-errChan
}
2 changes: 1 addition & 1 deletion core/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestServe(t *testing.T) {
return &TestServiceInterface{}, nil, nil
},
&TestServiceInterface{},
func(cfg *config.DefaultConfig, serviceIntf interface{}, _ *Hooks) (interface{}, error) {
func(ctx context.Context, cfg *config.DefaultConfig, serviceIntf interface{}, _ *Hooks) (interface{}, error) {
return nil, fmt.Errorf("not happening")
},
))
Expand Down

0 comments on commit 9a66700

Please sign in to comment.