A FAIRE.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

143 lines
3.8 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### main ####### Copyright (c) 2021 mls-361 #################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package main
import (
"os"
"os/signal"
"sync"
"syscall"
"time"
"forge.chapril.org/mls-361/application"
"forge.chapril.org/mls-361/failure"
"forge.chapril.org/armen/armen/internal/api"
"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/crypto"
"forge.chapril.org/armen/armen/internal/logger"
"forge.chapril.org/armen/armen/internal/model"
"forge.chapril.org/armen/armen/internal/router"
"forge.chapril.org/armen/armen/internal/server"
"forge.chapril.org/armen/armen/internal/storage"
)
var (
_version string
_builtAt string
)
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") ///////////////////////////////////////////////////////////////////
}
crypto, err := crypto.Build(app)
if err != nil {
return nil, nil, failure.Wrap(err, "crypto") ///////////////////////////////////////////////////////////////////
}
storage, err := storage.Build(app, config, logger, crypto)
if err != nil {
return nil, nil, failure.Wrap(err, "storage") //////////////////////////////////////////////////////////////////
}
model, err := model.Build(logger, storage)
if err != nil {
return nil, nil, failure.Wrap(err, "model") ////////////////////////////////////////////////////////////////////
}
router := router.Build(logger)
server, err := server.Build(config, logger, router)
if err != nil {
return nil, nil, failure.Wrap(err, "server") ///////////////////////////////////////////////////////////////////
}
api.Build(logger, router, model)
return logger, server, nil
}
func waitEnd(server components.Server) (*sync.WaitGroup, chan struct{}) {
endCh := make(chan struct{})
group := new(sync.WaitGroup)
group.Add(1)
go func() { //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
defer group.Done()
defer server.Stop()
sigCh := make(chan os.Signal, 1)
defer close(sigCh)
signal.Notify(sigCh, os.Interrupt, syscall.SIGABRT, syscall.SIGQUIT, syscall.SIGTERM)
select {
case <-sigCh:
case <-endCh:
}
}()
return group, endCh
}
func run() error {
app := application.New("armen", _version, _builtAt)
if end, err := cmdline.Parse(app); end || err != nil {
return app.OnError(err)
}
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(),
)
group, endCh := waitEnd(server)
err = server.Start()
close(endCh)
group.Wait()
logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"END",
"uptime", time.Since(app.StartedAt()).Round(time.Second).String(),
)
return app.OnError(err)
}
func main() {
if run() != nil {
os.Exit(1)
}
}
/*
######################################################################################################## @(°_°)@ #######
*/