From e7585fd564374abb3d65581e2ef659edc1b59508 Mon Sep 17 00:00:00 2001 From: mls-361 Date: Fri, 21 May 2021 11:40:46 +0200 Subject: [PATCH] =?UTF-8?q?En=20cours=20de=20d=C3=A9veloppement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 56 +++++++++++-------------------- internal/components/components.go | 18 ++++++++++ internal/config/data/data.go | 33 ++++++++++++++---- internal/config/data/logger.go | 8 ++--- internal/config/data/server.go | 4 +-- internal/config/data/storage.go | 6 ++-- internal/logger/logger.go | 20 ++++++++++- internal/storage/storage.go | 6 ++-- 8 files changed, 94 insertions(+), 57 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 52d3683..b79886e 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -41,51 +41,41 @@ func init() { } func buildComponents(ccs *components.Components) error { - config, err := config.Load(ccs) + var err error + + ccs.Config, err = config.Load(ccs) if err != nil { return errors.WithMessage(err, "config") /////////////////////////////////////////////////////////////////////// } - ccs.Config = config - - logger, err := logger.Build(ccs) + ccs.Logger, err = logger.Build(ccs) if err != nil { return errors.WithMessage(err, "logger") /////////////////////////////////////////////////////////////////////// } - ccs.Logger = logger - - crypto, err := crypto.Build(ccs) + ccs.Crypto, err = crypto.Build(ccs) if err != nil { return errors.WithMessage(err, "crypto") /////////////////////////////////////////////////////////////////////// } - ccs.Crypto = crypto - - storage, err := storage.Build(ccs) + ccs.Storage, err = storage.Build(ccs) if err != nil { return errors.WithMessage(err, "storage") ////////////////////////////////////////////////////////////////////// } - ccs.Storage = storage - - model, err := model.Build(ccs) + ccs.Model, err = model.Build(ccs) if err != nil { return errors.WithMessage(err, "model") //////////////////////////////////////////////////////////////////////// } - ccs.Model = model - ccs.Router = router.Build(ccs) - server, err := server.Build(ccs) + ccs.Server, err = server.Build(ccs) if err != nil { return errors.WithMessage(err, "server") /////////////////////////////////////////////////////////////////////// } - ccs.Server = server - - if err := demo.Build(ccs); err != nil { + if err = demo.Build(ccs); err != nil { return errors.WithMessage(err, "demo") ///////////////////////////////////////////////////////////////////////// } @@ -131,23 +121,22 @@ func run() error { ccs := components.New(app) + defer ccs.Shutdown() + if err := buildComponents(ccs); err != nil { + if ccs.Logger != nil { + ccs.Logger.Fatal( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: + "Components build error", + "reason", err, + ) + } + return app.OnError(err) } - ccs.Logger.Info( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: - "APP", - "id", app.ID(), - "name", app.Name(), - "version", app.Version(), - "builtAt", app.BuiltAt().Format("2006-01-02 15:04:05"), - "pid", os.Getpid(), - ) - - ccs.Logger.Debug("Config", "data", ccs.Config.Data()) //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: - group, endCh := waitEnd(ccs.Server) + ccs.Startup() demo.Start() err := ccs.Server.Start() @@ -158,13 +147,6 @@ func run() error { group.Wait() - ccs.Logger.Info( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: - "END", - "uptime", time.Since(app.StartedAt()).Round(time.Second).String(), - ) - - _ = ccs.Logger.Close() - return app.OnError(err) } diff --git a/internal/components/components.go b/internal/components/components.go index df87fcc..ccc89da 100644 --- a/internal/components/components.go +++ b/internal/components/components.go @@ -8,6 +8,7 @@ package components import ( "net/http" + "time" "forge.chapril.org/armen/jw" "forge.chapril.org/mls-361/jsonapi" @@ -21,6 +22,8 @@ type Application interface { ID() string Name() string Version() string + BuiltAt() time.Time + StartedAt() time.Time Debug() int LookupEnv(suffix string) (string, bool) Host() string @@ -88,6 +91,21 @@ func New(app Application) *Components { } } +// Startup AFAIRE. +func (ccs *Components) Startup() {} + +// Shutdown AFAIRE. +func (ccs *Components) Shutdown() { + if ccs.Logger != nil { + ccs.Logger.Info( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: + "END", + "uptime", time.Since(ccs.Application.StartedAt()).Round(time.Second).String(), + ) + + _ = ccs.Logger.Close() + } +} + /* ######################################################################################################## @(°_°)@ ####### */ diff --git a/internal/config/data/data.go b/internal/config/data/data.go index 84ff7ff..bdb1c09 100644 --- a/internal/config/data/data.go +++ b/internal/config/data/data.go @@ -6,24 +6,45 @@ package data +import ( + "encoding/json" + "fmt" + + "forge.chapril.org/mls-361/errors" +) + // Data AFAIRE. type Data struct { - Logger Logger - Server Server - Storage Storage + Logger Logger `json:"logger"` + Server Server `json:"server"` + Storage Storage `json:"storage"` } // Validate AFAIRE. func (d *Data) Validate() error { if err := d.Logger.validate(); err != nil { - return err + return errors.WithMessage(err, "logger") /////////////////////////////////////////////////////////////////////// } if err := d.Server.validate(); err != nil { - return err + return errors.WithMessage(err, "server") /////////////////////////////////////////////////////////////////////// + } + + if err := d.Storage.validate(); err != nil { + return errors.WithMessage(err, "storage") ////////////////////////////////////////////////////////////////////// + } + + return nil +} + +// String AFAIRE. +func (d *Data) String() string { + data, err := json.Marshal(d) + if err != nil { + return fmt.Sprintf(`{"ERROR":"%s"}`, err) } - return d.Storage.validate() + return string(data) } /* diff --git a/internal/config/data/logger.go b/internal/config/data/logger.go index fc0bdec..05053be 100644 --- a/internal/config/data/logger.go +++ b/internal/config/data/logger.go @@ -7,15 +7,15 @@ package data const ( - _defaultLoggerLevel = "debug" + _defaultLoggerLevel = "trace" _defaultLoggerOutput = "stderr" ) // Logger AFAIRE. type Logger struct { - Level string - Output string - Facility string + Level string `json:"level"` + Output string `json:"output"` + Facility string `json:"facility"` } func (l *Logger) validate() error { diff --git a/internal/config/data/server.go b/internal/config/data/server.go index 9ca7c0e..e81095a 100644 --- a/internal/config/data/server.go +++ b/internal/config/data/server.go @@ -6,9 +6,7 @@ package data -const ( - _defaultServerPort = 65530 -) +const _defaultServerPort = 65530 // Server AFAIRE. type Server struct { diff --git a/internal/config/data/storage.go b/internal/config/data/storage.go index f2d9d9f..a950df5 100644 --- a/internal/config/data/storage.go +++ b/internal/config/data/storage.go @@ -10,12 +10,12 @@ const _defaultStorage = "memory" // Storage AFAIRE. type Storage struct { - Type string + Impl string `json:"impl"` } func (s *Storage) validate() error { - if s.Type == "" { - s.Type = _defaultStorage + if s.Impl == "" { + s.Impl = _defaultStorage } return nil diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 9ee9a1b..76a8038 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -7,6 +7,8 @@ package logger import ( + "os" + "forge.chapril.org/mls-361/logger" "forge.chapril.org/armen/armen/internal/components" @@ -39,7 +41,23 @@ func Build(ccs *components.Components) (components.Logger, error) { return nil, err } - return logger.New(app.ID(), app.Name(), level, output) + logger, err := logger.New(app.ID(), app.Name(), level, output) + if err != nil { + return nil, err + } + + logger.Info( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: + "APP", + "id", app.ID(), + "name", app.Name(), + "version", app.Version(), + "builtAt", app.BuiltAt().Format("2006-01-02 15:04:05"), + "pid", os.Getpid(), + ) + + logger.Debug("Config", "data", ccs.Config.Data().String()) //::::::::::::::::::::::::::::::::::::::::::::::::::::::: + + return logger, nil } /* diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 3ddf4e9..cdf358d 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -17,13 +17,13 @@ import ( func Build(ccs *components.Components) (components.Storage, error) { cfg := ccs.Config.Storage() - switch cfg.Type { + switch cfg.Impl { case "memory": return memory.New(), nil default: return nil, errors.New( //////////////////////////////////////////////////////////////////////////////////////// - "this storage type is unknown", - "type", cfg.Type, + "this implementation does not exist", + "impl", cfg.Impl, ) } }