package main import ( "fmt" "os" "os/signal" "sync" "website/internal/log" "website/internal/server" "github.com/ardanlabs/conf/v3" "github.com/pkg/errors" ) var ( CommitSHA string ShortSHA string ) func main() { runtimeConfig := server.Config{} help, err := conf.Parse("", &runtimeConfig) if err != nil { if errors.Is(err, conf.ErrHelpWanted) { fmt.Println(help) os.Exit(1) } panic("parsing runtime configuration" + err.Error()) } log.Configure(!runtimeConfig.Development) if runtimeConfig.Development { tmpdir, err := os.MkdirTemp("", "website") if err != nil { log.Fatal("could not create temporary directory to build website: %v", err) } runtimeConfig.Root = tmpdir defer os.RemoveAll(tmpdir) } c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt) sv, err := server.New(&runtimeConfig) if err != nil { log.Fatal("error setting up server", "error", err) } wg := &sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() sig := <-c log.Info("signal captured", "sig", sig) <-sv.Stop() log.Debug("server stopped") }() sErr := make(chan error) wg.Add(1) go func() { defer wg.Done() sErr <- sv.Start() }() log.Info("server listening", "address", sv.Addr) err = <-sErr if err != nil { // Error starting or closing listener: log.Fatal("error", "error", err) } wg.Wait() }