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.
 
 
 

128 lines
3.3 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/router"
"forge.chapril.org/armen/armen/internal/server"
)
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") ///////////////////////////////////////////////////////////////////
}
_, err = crypto.Build(app)
if err != nil {
return nil, nil, failure.Wrap(err, "crypto") ///////////////////////////////////////////////////////////////////
}
router := router.Build(logger)
server, err := server.Build(config, logger, router)
if err != nil {
return nil, nil, failure.Wrap(err, "server") ///////////////////////////////////////////////////////////////////
}
api.Build(router)
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, syscall.SIGABRT, syscall.SIGQUIT, syscall.SIGTERM)
<-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)
}
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", syscall.Getpid(),
)
swg := waitEnd(server)
err = server.Start()
if err != nil {
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}
swg.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)
}
}
/*
######################################################################################################## @(°_°)@ #######
*/