Add Package Config :

- major dependancies are :
  - golib/context config
  - golib/viper
- interface config extend :
  - golib/context config interface
  - component list
- interface component list :
  this interface implement all function to manage a collection of component. All component are registred with they config key.
  A component must implement basic function like start, stop, reload, defaultConfig...
  The main config json is set by calling all component config with the config key attached
  Each component have some status status like isStarted, isRunning, ...
  Each component must also declare his dependencies with other component. As that when start or reload is called,
  the component is sure that dependencies are still started or reloaded.
- They are 4 component for now : log, tls, headers and http server
- The config package will start a new context / cancelfunc on init to be sure to stop cleanly all component and process

Add Package Viper :
- this package is an helper to the config package with the spf13 viper lib
- this package can watch any change of a config file or can be connected to a remote config cluster like ETCD

Add Package Cobra :
- this package is an helper to make a CLI with flag / command
- this package is based on spf13 cobra has all method to be connected to viper golib
This commit is contained in:
Nicolas JUHEL
2022-02-12 14:41:57 +01:00
parent adc69db2d4
commit 49db5e2afb
43 changed files with 4417 additions and 87 deletions

View File

@@ -283,7 +283,7 @@ func (c *ServerConfig) SetParentContext(f func() context.Context) {
c.getParentContext = f
}
func (c ServerConfig) GetTLS() (libtls.TLSConfig, liberr.Error) {
func (c *ServerConfig) GetTLS() (libtls.TLSConfig, liberr.Error) {
var def libtls.TLSConfig
if c.TLS.InheritDefault && c.getTLSDefault != nil {
@@ -293,7 +293,7 @@ func (c ServerConfig) GetTLS() (libtls.TLSConfig, liberr.Error) {
return c.TLS.NewFrom(def)
}
func (c ServerConfig) IsTLS() bool {
func (c *ServerConfig) IsTLS() bool {
if ssl, err := c.GetTLS(); err == nil && ssl != nil && ssl.LenCertificatePair() > 0 {
return true
}
@@ -301,7 +301,7 @@ func (c ServerConfig) IsTLS() bool {
return false
}
func (c ServerConfig) getContext() context.Context {
func (c *ServerConfig) getContext() context.Context {
var ctx context.Context
if c.getParentContext != nil {
@@ -315,7 +315,7 @@ func (c ServerConfig) getContext() context.Context {
return ctx
}
func (c ServerConfig) GetListen() *url.URL {
func (c *ServerConfig) GetListen() *url.URL {
var (
err error
add *url.URL
@@ -342,7 +342,7 @@ func (c ServerConfig) GetListen() *url.URL {
return add
}
func (c ServerConfig) GetExpose() *url.URL {
func (c *ServerConfig) GetExpose() *url.URL {
var (
err error
add *url.URL
@@ -367,14 +367,18 @@ func (c ServerConfig) GetExpose() *url.URL {
return add
}
func (c ServerConfig) GetHandlerKey() string {
func (c *ServerConfig) GetHandlerKey() string {
return c.HandlerKeys
}
func (c ServerConfig) Validate() liberr.Error {
func (c *ServerConfig) Validate() liberr.Error {
val := validator.New()
err := val.Struct(c)
if err == nil {
return nil
}
if e, ok := err.(*validator.InvalidValidationError); ok {
return ErrorServerValidate.ErrorParent(e)
}
@@ -394,6 +398,6 @@ func (c ServerConfig) Validate() liberr.Error {
}
func (c ServerConfig) Server() Server {
return NewServer(&c)
func (c *ServerConfig) Server() Server {
return NewServer(c)
}

View File

@@ -36,6 +36,7 @@ import (
"net/http"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
@@ -48,6 +49,8 @@ import (
const _TimeoutWaitingPortFreeing = 500 * time.Microsecond
type srvRun struct {
m sync.Mutex
log liblog.FuncLog // return golib logger interface
err *atomic.Value // last err occured
run *atomic.Value // is running
@@ -70,6 +73,7 @@ type run interface {
func newRun(log liblog.FuncLog) run {
return &srvRun{
m: sync.Mutex{},
log: log,
err: new(atomic.Value),
run: new(atomic.Value),
@@ -270,7 +274,10 @@ func (s *srvRun) Listen(cfg *ServerConfig, handler http.Handler) liberr.Error {
}
s.ctx, s.cnl = context.WithCancel(cfg.getContext())
s.m.Lock()
s.srv = srv
s.m.Unlock()
go func(ctx context.Context, cnl context.CancelFunc, name, host string, tlsMandatory bool) {
var _log = s.getLogger()
@@ -284,9 +291,14 @@ func (s *srvRun) Listen(cfg *ServerConfig, handler http.Handler) liberr.Error {
s.setRunning(false)
}()
s.m.Lock()
if s.srv == nil {
return
}
s.srv.BaseContext = func(listener net.Listener) context.Context {
return s.ctx
}
s.m.Unlock()
var er error
_log.Entry(liblog.InfoLevel, "Server is starting").Log()
@@ -337,7 +349,11 @@ func (s *srvRun) srvShutdown() {
cancel()
if s.srv != nil {
err := s.srv.Close()
s.m.Lock()
s.srv = nil
s.m.Unlock()
_log.Entry(liblog.ErrorLevel, "closing server").ErrorAdd(true, err).Check(liblog.InfoLevel)
}
}()