summaryrefslogtreecommitdiff
path: root/internal/config/server/server.go
blob: 8454c341bb897afaa48d15919d5d44a55262af39 (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
package server

import (
	"fmt"
	"log"
	"os"

	"codeberg.org/snonux/gos/internal/config"
)

type ServerConfig struct {
	ListenAddr        string   `json:"ListenAddr,omitempty"`
	Partners          []string `json:"Partners,omitempty"`
	APIKey            string   `json:"APIKey,omitempty"`
	DataDir           string   `json:"StateDir,omitempty"`
	EmailTo           string   `json:"EmailTo,omitempty"`
	EmailFrom         string   `json:"EmailFrom,omitempty"`
	SMTPServer        string   `json:"SMTPServer,omitempty"`
	MergeIntervalS    int      `json:"MergeInterval,omitempty"`
	ScheduleIntervalS int      `json:"ScheduleInterval,omitempty"`
	// SocialPlatformsEnable []string      `json:"SocialPlatformsEnable,omitempty"`
	Secrets SecretsConfig `json:"Secrets,omitempty"`
}

func New(configFile, secretsFile string) (ServerConfig, error) {
	conf, err := config.FromFile[ServerConfig](configFile)
	if err != nil {
		if _, ok := err.(*os.PathError); !ok {
			return conf, err
		}
		log.Println("Skipping config file:", err)
	}

	if conf.Secrets, err = newSecretsConfig(secretsFile); err != nil {
		return conf, err
	}

	conf.ListenAddr = config.Str("GOS_LISTEN_ADDR", conf.ListenAddr, "localhost:8080")
	conf.Partners = config.StrSlice("GOS_PARTNERS", conf.Partners)
	conf.APIKey = config.Str("GOS_API_KEY", conf.APIKey)
	conf.DataDir = config.Str("GOS_DATA_DIR", conf.DataDir, "data")
	conf.EmailTo = config.Str("GOS_EMAIL_TO", conf.EmailTo)
	conf.EmailFrom = config.Str("GOS_EMAIL_FROM", conf.EmailFrom)

	conf.SMTPServer = config.Str("GOS_SMTP_SERVER", conf.SMTPServer, func() string {
		hostname, err := os.Hostname()
		if err != nil {
			log.Fatal(err)
		}
		return fmt.Sprintf("%s:25", hostname)
	})

	const oneHour = 3600
	conf.MergeIntervalS = config.Int("GOS_MERGE_INTERVAL", oneHour)
	conf.ScheduleIntervalS = config.Int("GOS_SCHEDULER_INTERVAL", oneHour*6)

	return conf, nil
}