Jobs & Workflows https://armen.surge.sh
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.

170 lines
4.1 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/errors"
"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/demo"
"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 buildComponents(ccs *components.Components) error {
config, err := config.Load(ccs)
if err != nil {
return errors.WithMessage(err, "config") ///////////////////////////////////////////////////////////////////////
}
ccs.Config = config
logger, err := logger.Build(ccs)
if err != nil {
return errors.WithMessage(err, "logger") ///////////////////////////////////////////////////////////////////////
}
ccs.Logger = logger
crypto, err := crypto.Build(ccs)
if err != nil {
return errors.WithMessage(err, "crypto") ///////////////////////////////////////////////////////////////////////
}
ccs.Crypto = crypto
storage, err := storage.Build(ccs)
if err != nil {
return errors.WithMessage(err, "storage") //////////////////////////////////////////////////////////////////////
}
ccs.Storage = storage
model, err := model.Build(ccs)
if err != nil {
return errors.WithMessage(err, "model") ////////////////////////////////////////////////////////////////////////
}
ccs.Model = model
if err := demo.Build(ccs); err != nil {
return errors.WithMessage(err, "demo") /////////////////////////////////////////////////////////////////////////
}
ccs.Router = router.Build(ccs)
server, err := server.Build(ccs)
if err != nil {
return errors.WithMessage(err, "server") ///////////////////////////////////////////////////////////////////////
}
ccs.Server = server
api.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)
}
if end, err := cmdline.Parse(app); end || err != nil {
return app.OnError(err)
}
ccs := components.New(app)
err := buildComponents(ccs)
if err != nil {
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(),
)
group, endCh := waitEnd(ccs.Server)
demo.Start()
err = ccs.Server.Start()
close(endCh)
demo.Stop()
group.Wait()
ccs.Logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"END",
"uptime", time.Since(app.StartedAt()).Round(time.Second).String(),
)
return app.OnError(err)
}
func main() {
if run() != nil {
os.Exit(1)
}
}
/*
######################################################################################################## @(°_°)@ #######
*/