gitstack

grindlemire/gothem-stack code browser

940 B Go 43 lines 2025-11-29 · 98bbe10 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
package config

import (
	"context"

	"github.com/grindlemire/graft"
	"github.com/kelseyhightower/envconfig"
	"github.com/pkg/errors"
)

const ID = graft.ID("config")

type Output struct {
	Server ServerConfig
}

// 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"`
}

func init() {
	graft.Register(graft.Node[Output]{
		ID: ID,
		// cache the config so multiple nodes can use it without reloading
		Cacheable: true,
		Run:       run,
	})
}

// run will load the server config from the environment and return it.
func run(ctx context.Context) (Output, error) {
	var config ServerConfig
	err := envconfig.Process("", &config)
	if err != nil {
		return Output{}, errors.Wrap(err, "loading environment")
	}

	return Output{
		Server: config,
	}, nil
}