about summary refs log tree commit diff stats
path: root/cmd/server/main.go
blob: 464c4384c40a11ea6312e3f92bc16b28d0668f62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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.Production)

	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()
	}()
	if !runtimeConfig.InDevServer {
		log.Info("server listening", "address", sv.Addr)
	}

	err = <-sErr
	if err != nil {
		// Error starting or closing listener:
		log.Fatal("error", "error", err)
	}
	wg.Wait()
}