gitstack

grindlemire/gothem-stack code browser

2.0 KB Go 82 lines 2024-07-24 · e12b6f5 raw
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package server

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/kelseyhightower/envconfig"
	"github.com/pkg/errors"
	"go.uber.org/zap"
)

// ServerConfig is configuration for the server parsed from the env.
type ServerConfig struct {
	Port       int  `envconfig:"PORT"              default:"4433"`
	LocalCerts bool `envconfig:"LOCAL_CERTS"       default:"false" split_words:"true"`
}

// Run runs the server. The context will be cancelled if we receive a SIGTERM (ctrl-c)
func Run(ctx context.Context) error {
	// parse the env config
	var config ServerConfig
	err := envconfig.Process("", &config)
	if err != nil {
		return errors.Wrap(err, "loading environment")
	}
	addr := fmt.Sprintf(":%d", config.Port)

	if config.LocalCerts {
		if !hasCerts() {
			_, err := generateCerts()
			if err != nil {
				return err
			}
		}
	}

	// create the top level http router
	httpRouter := http.NewServeMux()

	// create our echo router and match all routes to it
	webMux, err := NewRouter(ctx)
	if err != nil {
		return err
	}
	httpRouter.Handle("/", webMux)
	server := &http.Server{Addr: addr, Handler: httpRouter}

	// run the listeners in their own goroutine, this is so we can properly propagate signals
	// and cleanup everything since there may be other signals that need to be cleaned up.
	errCh := make(chan error, 1)
	go func() {
		zap.S().Infof("started listening on %s", addr)
		if config.LocalCerts {
			zap.S().Debug(ctx, "listening with tls")
			err := server.ListenAndServeTLS(publicKeyFile, privateKeyFile)
			errCh <- errors.Wrap(err, "starting server")
			return
		}
		err := server.ListenAndServe()
		errCh <- errors.Wrap(err, "starting server")
	}()

	// wait for either the context to be cancelled indicating we should shutdown
	// or for the servers to fail
	for {
		select {
		case <-ctx.Done():
			shutdownCTX, cancel := context.WithTimeout(context.Background(), 5*time.Second)
			defer cancel()

			err = server.Shutdown(shutdownCTX)
			if err != nil {
				return err
			}
			return ctx.Err()
		case err := <-errCh:
			return err
		}
	}
}