go/main.go

91 lines
2.1 KiB
Go

//go:generate go install github.com/valyala/quicktemplate/qtc@master
//go:generate qtc -dir=web/template
//go:generate go install golang.org/x/text/cmd/gotext@master
//go:generate gotext -srclang=en update -lang=en,ru -out=locales_gen.go
package main
import (
"flag"
"log"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"source.toby3d.me$REPO_LINK/internal/domain"
"github.com/caarlos0/env/v10"
)
var (
cpuProfilePath string
memProfilePath string
)
var (
config *domain.Config = new(domain.Config)
logger *log.Logger = log.New(os.Stdout, "$REPO_NAME_LOWER\t", log.LstdFlags)
)
func init() {
flag.StringVar(&cpuProfilePath, "cpuprofile", "", "set path to saving CPU memory profile")
flag.StringVar(&memProfilePath, "memprofile", "", "set path to saving pprof memory profile")
flag.Parse()
if err := env.ParseWithOptions(config, env.Options{
Environment: nil,
FuncMap: nil,
OnSet: nil,
Prefix: "${REPO_NAME_UPPER}_",
RequiredIfNoDef: false,
TagName: "env",
UseFieldNameByDefault: true,
}); err != nil {
logger.Fatalln(err)
}
}
func main() {
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
if cpuProfilePath != "" {
cpuProfile, err := os.Create(cpuProfilePath)
if err != nil {
logger.Fatalln("could not create CPU profile:", err)
}
defer cpuProfile.Close()
if err = pprof.StartCPUProfile(cpuProfile); err != nil {
logger.Fatalln("could not write CPU profile:", err)
}
defer pprof.StopCPUProfile()
}
go func() {
// TODO(toby3d): do something blocking here.
logger.Println("$REPO_NAME_LOWER binded to", config.BIND)
}()
<-done
// TODO(toby3d): graceful shutdown here.
if memProfilePath == "" {
return
}
memProfile, err := os.Create(memProfilePath)
if err != nil {
logger.Fatalln("could not create memory profile:", err)
}
defer memProfile.Close()
runtime.GC() // NOTE(toby3d): get up-to-date statistics
if err = pprof.WriteHeapProfile(memProfile); err != nil {
logger.Fatalln("could not write memory profile:", err)
}
}