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.
 
 

165 lines
3.9 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### main ####### Copyright (c) 2021 mls-361 #################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package main
import (
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
"time"
"forge.chapril.org/mls-361/application"
"forge.chapril.org/mls-361/errors"
"forge.chapril.org/armen/armen/internal/api"
"forge.chapril.org/armen/armen/internal/cli"
"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/demo"
"forge.chapril.org/armen/armen/internal/gui"
"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 init() {
rand.Seed(time.Now().UnixNano())
}
func buildComponents(ccs *components.Components) error {
var err error
ccs.Config, err = config.Load(ccs)
if err != nil {
return errors.WithMessage(err, "config") ///////////////////////////////////////////////////////////////////////
}
ccs.Logger, err = logger.Build(ccs)
if err != nil {
return errors.WithMessage(err, "logger") ///////////////////////////////////////////////////////////////////////
}
ccs.Crypto, err = crypto.Build(ccs)
if err != nil {
return errors.WithMessage(err, "crypto") ///////////////////////////////////////////////////////////////////////
}
ccs.Storage, err = storage.Build(ccs)
if err != nil {
return errors.WithMessage(err, "storage") //////////////////////////////////////////////////////////////////////
}
ccs.Model, err = model.Build(ccs)
if err != nil {
return errors.WithMessage(err, "model") ////////////////////////////////////////////////////////////////////////
}
if len(os.Args) > 1 {
return nil
}
ccs.Router = router.Build(ccs)
ccs.Server, err = server.Build(ccs)
if err != nil {
return errors.WithMessage(err, "server") ///////////////////////////////////////////////////////////////////////
}
if err = demo.Build(ccs); err != nil {
return errors.WithMessage(err, "demo") /////////////////////////////////////////////////////////////////////////
}
api.Build(ccs)
gui.Build(ccs)
return 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 err := app.Initialize(); err != nil {
return app.OnError(err)
}
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)
}
if len(os.Args) > 1 {
return app.OnError(cli.Run(ccs))
}
group, endCh := waitEnd(ccs.Server)
ccs.Startup()
demo.Start()
err := ccs.Server.Start()
close(endCh)
demo.Stop()
group.Wait()
return app.OnError(err)
}
func main() {
if run() != nil {
os.Exit(1)
}
}
/*
######################################################################################################## @(°_°)@ #######
*/