Browse Source

En cours de développement

master
mls-361 6 months ago
parent
commit
c2b5460e2b
  1. 81
      cmd/main.go
  2. 42
      internal/components/components.go
  3. 82
      internal/config/config.go
  4. 55
      internal/config/data.go
  5. 40
      internal/config/data/data.go
  6. 4
      internal/config/data/logger.go
  7. 23
      internal/config/data/server.go
  8. 42
      internal/config/json/json.go
  9. 19
      internal/config/mongodb.go
  10. 19
      internal/config/postgresql.go
  11. 90
      internal/engine/engine.go.afac
  12. 14
      internal/logger/logger.go
  13. 39
      internal/server/server.go
  14. 99
      internal/supervisor/supervisor.go

81
cmd/main.go

@ -8,10 +8,18 @@ package main
import (
"os"
"os/signal"
"sync"
"time"
"forge.chapril.org/armen/armen/internal/cmdline"
"forge.chapril.org/armen/armen/internal/supervisor"
"forge.chapril.org/mls-361/application"
"forge.chapril.org/mls-361/failure"
"forge.chapril.org/armen/armen/internal/cmdline"
"forge.chapril.org/armen/armen/internal/components"
"forge.chapril.org/armen/armen/internal/config"
"forge.chapril.org/armen/armen/internal/logger"
"forge.chapril.org/armen/armen/internal/server"
)
var (
@ -19,18 +27,81 @@ var (
_builtAt string
)
func appRun() error {
func createComponents(app components.Application) (components.Logger, components.Server, error) {
config, err := config.Load(app)
if err != nil {
return nil, nil, failure.Wrap(err, "config") ///////////////////////////////////////////////////////////////////
}
logger, err := logger.Build(app, config)
if err != nil {
return nil, nil, failure.Wrap(err, "logger") ///////////////////////////////////////////////////////////////////
}
server, err := server.Build(config, logger)
if err != nil {
return nil, nil, failure.Wrap(err, "server") ///////////////////////////////////////////////////////////////////
}
return logger, server, nil
}
func waitEnd(server components.Server) *sync.WaitGroup {
swg := new(sync.WaitGroup)
swg.Add(1)
go func() { //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
defer swg.Done()
defer server.Stop()
sigEnd := make(chan os.Signal, 1)
defer close(sigEnd)
signal.Notify(sigEnd, os.Interrupt)
<-sigEnd
}()
return swg
}
func run() error {
app := application.New("armen", _version, _builtAt)
if end, err := cmdline.Parse(app); end || err != nil {
return app.OnError(err)
}
return app.OnError(supervisor.New(app).Run())
logger, server, err := createComponents(app)
if err != nil {
return app.OnError(err)
}
logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"BEGIN",
"id", app.ID(),
"name", app.Name(),
"version", app.Version(),
"builtAt", app.BuiltAt().Format("2006-01-02 15:04:05"),
"pid", os.Getpid(),
)
swg := waitEnd(server)
err = server.Start()
swg.Wait()
logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"END",
"uptime", time.Since(app.StartedAt()).Round(time.Second).String(),
)
return app.OnError(err)
}
func main() {
if appRun() != nil {
if run() != nil {
os.Exit(1)
}
}

42
internal/components/components.go

@ -0,0 +1,42 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### components ####### Copyright (c) 2021 mls-361 ############################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package components
import (
"forge.chapril.org/mls-361/logger"
"forge.chapril.org/mls-361/uuid"
"forge.chapril.org/armen/armen/internal/config/data"
)
// Application AFAIRE.
type Application interface {
ID() uuid.UUID
Name() string
LookupEnv(suffix string) (string, bool)
}
// Config AFAIRE.
type Config interface {
Logger() *data.Logger
Server() *data.Server
}
// Logger AFAIRE.
type Logger interface {
logger.Logger
}
// Server AFAIRE.
type Server interface {
Start() error
Stop()
}
/*
######################################################################################################## @(°_°)@ #######
*/

82
internal/config/config.go

@ -7,67 +7,57 @@
package config
import (
"encoding/json"
"io/ioutil"
"forge.chapril.org/mls-361/failure"
"forge.chapril.org/mls-361/application"
"forge.chapril.org/armen/armen/internal/components"
"forge.chapril.org/armen/armen/internal/config/data"
"forge.chapril.org/armen/armen/internal/config/json"
)
const (
_sourceMongoDB = "mongodb"
_sourcePostgreSQL = "postgresql"
)
// Config AFAIRE.
type Config struct {
app *application.Application
data *Data
source string
type config struct {
data *data.Data
}
// New AFAIRE.
func New(app *application.Application) *Config {
return &Config{
app: app,
data: new(Data),
func newConfig() *config {
return &config{
data: new(data.Data),
}
}
// LoadFromSource AFAIRE.
func (c *Config) LoadFromSource() error {
switch c.source {
case _sourceMongoDB:
case _sourcePostgreSQL:
}
return nil
}
// Load AFAIRE.
func (c *Config) Load() (bool, error) {
file, ok := c.app.LookupEnv("CONFIG")
if !ok {
c.data.Logger = defaultLogger()
c.data.Engine = defaultEngine()
func Load(app components.Application) (*config, error) {
cc := newConfig()
return true, nil
}
config, ok := app.LookupEnv("CONFIG")
if !ok {
cc.data.Default()
} else {
switch config {
case "json":
if err := json.LoadConfig(app, cc.data); err != nil {
return nil, err
}
default:
return nil, failure.New( ///////////////////////////////////////////////////////////////////////////////////
"this configuration mode is not valid",
"mode", config,
)
}
buf, err := ioutil.ReadFile(file)
if err != nil {
return false, err
if err := cc.data.Validate(); err != nil {
return nil, err
}
}
if err := json.Unmarshal(buf, c.data); err != nil {
return false, err
}
return cc, nil
}
c.source, err = c.data.validate()
if err != nil {
return false, err
}
func (cc *config) Logger() *data.Logger {
return cc.data.Logger
}
return c.source == "", nil
func (cc *config) Server() *data.Server {
return cc.data.Server
}
/*

55
internal/config/data.go

@ -1,55 +0,0 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
// Data AFAIRE.
type Data struct {
Logger *Logger `json:"logger"`
MongoDB *MongoDB `json:"mongodb"`
PostgreSQL *PostgreSQL `json:"postgresql"`
Engine *Engine `json:"engine"`
}
func (d *Data) validate() (string, error) {
if d.Logger == nil {
d.Logger = defaultLogger()
} else if err := d.Logger.validate(); err != nil {
return "", err
}
if d.Engine != nil {
if err := d.Engine.validate(); err != nil {
return "", err
}
return "", nil
}
if d.MongoDB != nil {
if err := d.MongoDB.validate(); err != nil {
return "", err
}
return _sourceMongoDB, nil
}
if d.PostgreSQL != nil {
if err := d.PostgreSQL.validate(); err != nil {
return "", err
}
return _sourcePostgreSQL, nil
}
d.Engine = defaultEngine()
return "", nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

40
internal/config/data/data.go

@ -0,0 +1,40 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### data ####### Copyright (c) 2021 mls-361 #################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package data
// Data AFAIRE.
type Data struct {
Logger *Logger `json:"logger"`
Server *Server `json:"server"`
}
// Default AFAIRE.
func (d *Data) Default() {
d.Logger = defaultLogger()
d.Server = defaultServer()
}
// Validate AFAIRE.
func (d *Data) Validate() error {
if d.Logger == nil {
d.Logger = defaultLogger()
} else if err := d.Logger.validate(); err != nil {
return err
}
if d.Server == nil {
d.Server = defaultServer()
} else if err := d.Server.validate(); err != nil {
return err
}
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

4
internal/config/logger.go → internal/config/data/logger.go

@ -1,10 +1,10 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
####### data ####### Copyright (c) 2021 mls-361 #################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
package data
const (
_defaultLoggerLevel = "trace"

23
internal/config/engine.go → internal/config/data/server.go

@ -1,22 +1,27 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
####### data ####### Copyright (c) 2021 mls-361 #################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
package data
// Engine AFAIRE.
type Engine struct {
Version string `json:"version"`
UseAfter string `json:"use_after"`
const (
_defaultServerPort = 65535
)
// Server AFAIRE.
type Server struct {
Port int `json:"port"`
}
func defaultEngine() *Engine {
return &Engine{}
func defaultServer() *Server {
return &Server{
Port: _defaultServerPort,
}
}
func (e *Engine) validate() error {
func (s *Server) validate() error {
return nil
}

42
internal/config/json/json.go

@ -0,0 +1,42 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### json ####### Copyright (c) 2021 mls-361 #################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package json
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"forge.chapril.org/armen/armen/internal/components"
)
const (
_defaultConfigDir = "/etc"
)
// LoadConfig AFAIRE.
func LoadConfig(app components.Application, destination interface{}) error {
file, ok := app.LookupEnv("CONFIG_FILE")
if !ok {
file = filepath.Join(_defaultConfigDir, app.Name(), app.Name()+".json")
}
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
if err := json.Unmarshal(data, destination); err != nil {
return err
}
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

19
internal/config/mongodb.go

@ -1,19 +0,0 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
// MongoDB AFAIRE.
type MongoDB struct {
}
func (m *MongoDB) validate() error {
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

19
internal/config/postgresql.go

@ -1,19 +0,0 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
// PostgreSQL AFAIRE.
type PostgreSQL struct {
}
func (p *PostgreSQL) validate() error {
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

90
internal/engine/engine.go.afac

@ -1,90 +0,0 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### engine ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package engine
import (
"sync"
"forge.chapril.org/armen/armen/internal/config"
"forge.chapril.org/mls-361/application"
"forge.chapril.org/mls-361/logger"
"forge.chapril.org/mls-361/uuid"
)
// Engine AFAIRE.
type Engine struct {
app *application.Application
config *config.Config
configCh chan bool
stopCh chan struct{}
endCh chan error
swg sync.WaitGroup
logger logger.Logger
}
// New AFAIRE.
func New(app *application.Application, config *config.Config) *Engine {
return &Engine{
app: app,
config: config,
configCh: make(chan bool),
stopCh: make(chan struct{}),
endCh: make(chan error, 1),
}
}
// ConfigHasChanged AFAIRE.
func (e *Engine) ConfigHasChanged() {
e.configCh <- true
}
// EndCh AFAIRE.
func (e *Engine) EndCh() <-chan error {
return e.endCh
}
// Wait AFAIRE.
func (e *Engine) Wait() {
e.swg.Wait()
}
func (e *Engine) loop() {
loop:
for {
select {
case <-e.configCh:
case <-e.stopCh:
break loop
}
}
e.endCh <- nil
}
func (e *Engine) main(logger logger.Logger) {
defer e.swg.Done()
defer logger.Remove()
e.logger = logger
e.loop()
}
// Start AFAIRE.
func (e *Engine) Start(logger logger.Logger) {
e.swg.Add(1)
go e.main(logger.NewLogger(uuid.New(), "engine")) //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
}
// Stop AFAIRE.
func (e *Engine) Stop() {
close(e.stopCh)
}
/*
######################################################################################################## @(°_°)@ #######
*/

14
internal/supervisor/logger.go → internal/logger/logger.go

@ -1,15 +1,19 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### supervisor ####### Copyright (c) 2021 mls-361 ############################################## MIT License #######
####### logger ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package supervisor
package logger
import "forge.chapril.org/mls-361/logger"
import (
"forge.chapril.org/mls-361/logger"
func (s *Supervisor) createLogger() (logger.Logger, error) {
app := s.app
"forge.chapril.org/armen/armen/internal/components"
)
// AFAIRE.
func Build(app components.Application, _ components.Config) (logger.Logger, error) {
return logger.New(app.ID(), app.Name(), "trace", logger.NewStderrOutput())
}

39
internal/server/server.go

@ -0,0 +1,39 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### server ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package server
import (
"forge.chapril.org/mls-361/logger"
"forge.chapril.org/armen/armen/internal/components"
)
type server struct {
}
func newServer() *server {
return &server{}
}
func Build(config components.Config, logger logger.Logger) (*server, error) {
cs := newServer()
return cs, nil
}
// Start AFAIRE.
func (cs *server) Start() error {
return nil
}
// Stop AFAIRE.
func (cs *server) Stop() {
}
/*
######################################################################################################## @(°_°)@ #######
*/

99
internal/supervisor/supervisor.go

@ -1,99 +0,0 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### supervisor ####### Copyright (c) 2021 mls-361 ############################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package supervisor
import (
"os"
"time"
"forge.chapril.org/mls-361/application"
"forge.chapril.org/mls-361/failure"
"forge.chapril.org/mls-361/logger"
"forge.chapril.org/armen/armen/internal/config"
)
// Supervisor AFAIRE.
type Supervisor struct {
app *application.Application
config *config.Config
logger logger.Logger
}
// New AFAIRE.
func New(app *application.Application) *Supervisor {
return &Supervisor{
app: app,
config: config.New(app),
}
}
func (s *Supervisor) loadConfig() error {
complete, err := s.config.Load()
if err != nil {
return failure.Wrap(err, "unable to load configuration") ///////////////////////////////////////////////////////
}
if complete {
return nil
}
if err := s.config.LoadFromSource(); err != nil {
return failure.Wrap(err, "impossible to load the entire configuration") ////////////////////////////////////////
}
return nil
}
func (s *Supervisor) createComponents() error {
return nil
}
func (s *Supervisor) loop() {
}
// Run AFAIRE.
func (s *Supervisor) Run() error {
if err := s.loadConfig(); err != nil {
return failure.Wrap(err, "unable to load configuration") ///////////////////////////////////////////////////////
}
logger, err := s.createLogger()
if err != nil {
return failure.Wrap(err, "impossible to create the logger") ////////////////////////////////////////////////////
}
app := s.app
defer func() {
logger.Info("END", "uptime", time.Since(app.StartedAt()).Round(time.Second).String()) //::::::::::::::::::::::::
logger.Remove()
}()
logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"BEGIN",
"id", app.ID(),
"name", app.Name(),
"version", app.Version(),
"builtAt", app.BuiltAt().Format("2006-01-02 15:04:05"),
"pid", os.Getpid(),
)
s.logger = logger
if err := s.createComponents(); err != nil {
return failure.Wrap(err, "impossible to create the components") //::::::::::::::::::::::::::::::::::::::::::::::
}
s.loop()
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/
Loading…
Cancel
Save