2022-04-20 16:41:20 +02:00
|
|
|
// Copyright 2022 The TVL Contributors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2019-08-14 18:20:01 +02:00
|
|
|
|
|
|
|
// Package config implements structures to store Nixery's configuration at
|
|
|
|
// runtime as well as the logic for instantiating this configuration from the
|
|
|
|
// environment.
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2019-10-04 23:17:11 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-08-14 18:20:01 +02:00
|
|
|
)
|
|
|
|
|
2019-09-03 00:32:36 +02:00
|
|
|
func getConfig(key, desc, def string) string {
|
2019-08-14 18:20:01 +02:00
|
|
|
value := os.Getenv(key)
|
2019-09-03 00:32:36 +02:00
|
|
|
if value == "" && def == "" {
|
2019-10-06 04:18:38 +02:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"option": key,
|
|
|
|
"description": desc,
|
|
|
|
}).Fatal("missing required configuration envvar")
|
2019-09-03 00:32:36 +02:00
|
|
|
} else if value == "" {
|
|
|
|
return def
|
2019-08-14 18:20:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:33:57 +01:00
|
|
|
// Backend represents the possible storage backend types
|
|
|
|
type Backend int
|
|
|
|
|
|
|
|
const (
|
|
|
|
GCS = iota
|
2019-10-28 18:19:06 +01:00
|
|
|
FileSystem
|
2019-10-27 18:33:57 +01:00
|
|
|
)
|
|
|
|
|
2019-09-30 18:40:01 +02:00
|
|
|
// Config holds the Nixery configuration options.
|
2019-08-14 18:20:01 +02:00
|
|
|
type Config struct {
|
2019-10-27 16:49:54 +01:00
|
|
|
Port string // Port on which to launch HTTP server
|
|
|
|
Pkgs PkgSource // Source for Nix package set
|
|
|
|
Timeout string // Timeout for a single Nix builder (seconds)
|
|
|
|
WebDir string // Directory with static web assets
|
|
|
|
PopUrl string // URL to the Nix package popularity count
|
2019-10-27 18:33:57 +01:00
|
|
|
Backend Backend // Storage backend to use for Nixery
|
2019-08-14 18:20:01 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 13:49:26 +02:00
|
|
|
func FromEnv() (Config, error) {
|
2019-09-08 22:53:22 +02:00
|
|
|
pkgs, err := pkgSourceFromEnv()
|
|
|
|
if err != nil {
|
2019-10-03 13:49:26 +02:00
|
|
|
return Config{}, err
|
2019-09-08 22:53:22 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 18:33:57 +01:00
|
|
|
var b Backend
|
|
|
|
switch os.Getenv("NIXERY_STORAGE_BACKEND") {
|
|
|
|
case "gcs":
|
|
|
|
b = GCS
|
2019-10-28 18:19:06 +01:00
|
|
|
case "filesystem":
|
|
|
|
b = FileSystem
|
2019-10-27 18:33:57 +01:00
|
|
|
default:
|
|
|
|
log.WithField("values", []string{
|
|
|
|
"gcs",
|
2021-12-23 12:14:49 +01:00
|
|
|
}).Fatal("NIXERY_STORAGE_BACKEND must be set to a supported value (gcs or filesystem)")
|
2019-10-27 18:33:57 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 13:49:26 +02:00
|
|
|
return Config{
|
2019-09-03 00:32:36 +02:00
|
|
|
Port: getConfig("PORT", "HTTP port", ""),
|
2019-09-08 22:53:22 +02:00
|
|
|
Pkgs: pkgs,
|
2019-09-03 00:32:36 +02:00
|
|
|
Timeout: getConfig("NIX_TIMEOUT", "Nix builder timeout", "60"),
|
|
|
|
WebDir: getConfig("WEB_DIR", "Static web file dir", ""),
|
2019-09-21 13:15:38 +02:00
|
|
|
PopUrl: os.Getenv("NIX_POPULARITY_URL"),
|
2019-10-27 18:33:57 +01:00
|
|
|
Backend: b,
|
2019-09-08 22:53:22 +02:00
|
|
|
}, nil
|
2019-08-14 18:20:01 +02:00
|
|
|
}
|