-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
62 lines (56 loc) · 1.31 KB
/
config.go
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
package setup
import (
"context"
"io"
"os"
"github.com/awlsring/texit/internal/app/api/config"
"github.com/awlsring/texit/internal/pkg/runtime"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
const (
configEnvVar = "CONFIG_PATH"
defaultConfigLocation = "/etc/texit/config.yaml"
)
func LoadConfig() (*config.Config, error) {
if runtime.IsLambda() {
return loadConfigFromS3()
}
return config.LoadFromFile(getConfigPath())
}
func getConfigPath() string {
path := os.Getenv(configEnvVar)
if path == "" {
return defaultConfigLocation
}
return path
}
func loadConfigFromS3() (*config.Config, error) {
acfg, err := awsconfig.LoadDefaultConfig(context.Background())
if err != nil {
return nil, err
}
client := s3.NewFromConfig(acfg)
bucketName := os.Getenv("CONFIG_BUCKET")
if bucketName == "" {
panic("CONFIG_BUCKET environment variable not set")
}
resp, err := client.GetObject(context.Background(), &s3.GetObjectInput{
Bucket: &bucketName,
Key: aws.String("config.yaml"),
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
cfg, err := config.LoadFromData(bytes)
if err != nil {
return nil, err
}
return cfg, nil
}