Path: blob/main/components/openvsx-proxy/pkg/config.go
2498 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package pkg56import (7"encoding/json"8"os"910"github.com/gitpod-io/gitpod/common-go/util"11validation "github.com/go-ozzo/ozzo-validation"12"github.com/go-ozzo/ozzo-validation/is"13"golang.org/x/xerrors"14)1516type Config struct {17LogDebug bool `json:"log_debug"`18CacheDurationRegular util.Duration `json:"cache_duration_regular"`19CacheDurationBackup util.Duration `json:"cache_duration_backup"`20URLUpstream string `json:"url_upstream"`21MaxIdleConns int `json:"max_idle_conns"`22MaxIdleConnsPerHost int `json:"max_idle_conns_per_host"`23RedisAddr string `json:"redis_addr"`24PrometheusAddr string `json:"prometheusAddr"`25AllowCacheDomain []string `json:"allow_cache_domain"`26}2728// Validate validates the configuration to catch issues during startup and not at runtime29func (c *Config) Validate() error {30if c == nil {31return xerrors.Errorf("config is missing")32}3334return validation.ValidateStruct(c,35validation.Field(&c.CacheDurationRegular, validation.Required),36validation.Field(&c.CacheDurationBackup, validation.Required),37validation.Field(&c.URLUpstream, validation.Required, is.URL),38)39}4041// ReadConfig loads and validates the configuration42func ReadConfig(fn string) (*Config, error) {43fc, err := os.ReadFile(fn)44if err != nil {45return nil, err46}4748var cfg Config49err = json.Unmarshal(fc, &cfg)50if err != nil {51return nil, err52}5354err = cfg.Validate()55if err != nil {56return nil, xerrors.Errorf("config validation error: %w", err)57}5859return &cfg, nil60}6162func (cfg *Config) ToJson() []byte {63b, err := json.Marshal(cfg)64if err != nil {65return nil66}67return b68}6970func (cfg *Config) RedisEnabled() bool {71return len(cfg.RedisAddr) > 072}737475