Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/openvsx-proxy/pkg/config.go
2498 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package pkg
6
7
import (
8
"encoding/json"
9
"os"
10
11
"github.com/gitpod-io/gitpod/common-go/util"
12
validation "github.com/go-ozzo/ozzo-validation"
13
"github.com/go-ozzo/ozzo-validation/is"
14
"golang.org/x/xerrors"
15
)
16
17
type Config struct {
18
LogDebug bool `json:"log_debug"`
19
CacheDurationRegular util.Duration `json:"cache_duration_regular"`
20
CacheDurationBackup util.Duration `json:"cache_duration_backup"`
21
URLUpstream string `json:"url_upstream"`
22
MaxIdleConns int `json:"max_idle_conns"`
23
MaxIdleConnsPerHost int `json:"max_idle_conns_per_host"`
24
RedisAddr string `json:"redis_addr"`
25
PrometheusAddr string `json:"prometheusAddr"`
26
AllowCacheDomain []string `json:"allow_cache_domain"`
27
}
28
29
// Validate validates the configuration to catch issues during startup and not at runtime
30
func (c *Config) Validate() error {
31
if c == nil {
32
return xerrors.Errorf("config is missing")
33
}
34
35
return validation.ValidateStruct(c,
36
validation.Field(&c.CacheDurationRegular, validation.Required),
37
validation.Field(&c.CacheDurationBackup, validation.Required),
38
validation.Field(&c.URLUpstream, validation.Required, is.URL),
39
)
40
}
41
42
// ReadConfig loads and validates the configuration
43
func ReadConfig(fn string) (*Config, error) {
44
fc, err := os.ReadFile(fn)
45
if err != nil {
46
return nil, err
47
}
48
49
var cfg Config
50
err = json.Unmarshal(fc, &cfg)
51
if err != nil {
52
return nil, err
53
}
54
55
err = cfg.Validate()
56
if err != nil {
57
return nil, xerrors.Errorf("config validation error: %w", err)
58
}
59
60
return &cfg, nil
61
}
62
63
func (cfg *Config) ToJson() []byte {
64
b, err := json.Marshal(cfg)
65
if err != nil {
66
return nil
67
}
68
return b
69
}
70
71
func (cfg *Config) RedisEnabled() bool {
72
return len(cfg.RedisAddr) > 0
73
}
74
75