about summary refs log tree commit diff stats
path: root/cmd/server/main.go
blob: 26dbe3232b46cdfa0481985734300e5d0d7d094b (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
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"website/internal/log"
	"website/internal/server"

	"github.com/ardanlabs/conf/v3"
	"github.com/pkg/errors"
)

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)
	}

	sv, err := server.New(&runtimeConfig)
	if err != nil {
		log.Error("could not create server", "error", err)

		return
	}

	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	go func() {
		err = sv.Start()
		if err != nil {
			// Error starting or closing listener:
			log.Fatal("error %v", err)
		}
	}()

	<-ctx.Done()
	log.Debug("calling stop")
	sv.Stop()
	log.Debug("done")
}