aboutsummaryrefslogtreecommitdiff
path: root/forged/internal/config/config.go
blob: 182588258d08ebae9e90288b53ef2d1c79e755cb (plain) (blame)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

package config

import (
	"bufio"
	"fmt"
	"log/slog"
	"os"

	"go.lindenii.runxiyu.org/forge/forged/internal/common/scfg"
)

type Config struct {
	DB      DB      `scfg:"db"`
	Web     Web     `scfg:"web"`
	Hooks   Hooks   `scfg:"hooks"`
	LMTP    LMTP    `scfg:"lmtp"`
	SSH     SSH     `scfg:"ssh"`
	IRC     IRC     `scfg:"irc"`
	Git     Git     `scfg:"git"`
	General General `scfg:"general"`
	Pprof   Pprof   `scfg:"pprof"`
}

type DB struct {
	Conn string `scfg:"conn"`
}

type Web struct {
	Net             string `scfg:"net"`
	Addr            string `scfg:"addr"`
	Root            string `scfg:"root"`
	CookieExpiry    int    `scfg:"cookie_expiry"`
	ReadTimeout     uint32 `scfg:"read_timeout"`
	WriteTimeout    uint32 `scfg:"write_timeout"`
	IdleTimeout     uint32 `scfg:"idle_timeout"`
	MaxHeaderBytes  int    `scfg:"max_header_bytes"`
	ReverseProxy    bool   `scfg:"reverse_proxy"`
	ShutdownTimeout uint32 `scfg:"shutdown_timeout"`
	TemplatesPath   string `scfg:"templates_path"`
	StaticPath      string `scfg:"static_path"`
}

type Hooks struct {
	Socket string `scfg:"socket"`
	Execs  string `scfg:"execs"`
}

type LMTP struct {
	Socket       string `scfg:"socket"`
	Domain       string `scfg:"domain"`
	MaxSize      int64  `scfg:"max_size"`
	WriteTimeout uint32 `scfg:"write_timeout"`
	ReadTimeout  uint32 `scfg:"read_timeout"`
}

type SSH struct {
	Net             string `scfg:"net"`
	Addr            string `scfg:"addr"`
	Key             string `scfg:"key"`
	Root            string `scfg:"root"`
	ShutdownTimeout uint32 `scfg:"shutdown_timeout"`
}

type IRC struct {
	Net   string `scfg:"net"`
	Addr  string `scfg:"addr"`
	TLS   bool   `scfg:"tls"`
	SendQ uint   `scfg:"sendq"`
	Nick  string `scfg:"nick"`
	User  string `scfg:"user"`
	Gecos string `scfg:"gecos"`
}

type Git struct {
	RepoDir string `scfg:"repo_dir"`
	Socket  string `scfg:"socket"`
}

type General struct {
	Title string `scfg:"title"`
}

type Pprof struct {
	Net  string `scfg:"net"`
	Addr string `scfg:"addr"`
}

func Open(path string) (config Config, err error) {
	var configFile *os.File

	configFile, err = os.Open(path) //#nosec G304
	if err != nil {
		err = fmt.Errorf("open config file: %w", err)
		return config, err
	}
	defer func() {
		_ = configFile.Close()
	}()

	decoder := scfg.NewDecoder(bufio.NewReader(configFile))
	err = decoder.Decode(&config)
	if err != nil {
		err = fmt.Errorf("decode config file: %w", err)
		return config, err
	}
	for _, u := range decoder.UnknownDirectives() {
		slog.Warn("unknown configuration directive", "directive", u)
	}

	return config, err
}