Move config into package

This commit is contained in:
Christopher LaPointe
2023-05-22 21:51:15 -04:00
parent 86316b8ab3
commit ffa7ad7ca8
3 changed files with 20 additions and 17 deletions

19
main.go
View File

@@ -13,6 +13,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"traefik-lazyload/pkg/config"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
@@ -32,7 +33,7 @@ type SplashModel struct {
WaitForPath string WaitForPath string
} }
var splashTemplate = template.Must(template.ParseFS(httpAssets, "assets/splash.html")) var splashTemplate = template.Must(template.ParseFS(httpAssets, path.Join("assets", config.Model.Splash)))
var dockerClient *client.Client var dockerClient *client.Client
@@ -68,12 +69,12 @@ func main() {
logrus.Infof("Connected docker to %s", info.Name) logrus.Infof("Connected docker to %s", info.Name)
} }
if splash, err := httpAssets.ReadFile(path.Join("assets", Config.Splash)); err != nil || len(splash) == 0 { if splash, err := httpAssets.ReadFile(path.Join("assets", config.Model.Splash)); err != nil || len(splash) == 0 {
logrus.Fatal("Unable to open splash file %s", Config.Splash) logrus.Fatal("Unable to open splash file %s", config.Model.Splash)
} }
// Initial state // Initial state
if Config.StopAtBoot { if config.Model.StopAtBoot {
stopAllLazyContainers() stopAllLazyContainers()
} else { } else {
//TODO: Inventory currently running containers //TODO: Inventory currently running containers
@@ -85,8 +86,8 @@ func main() {
http.Handle(httpAssetPrefix, http.StripPrefix(httpAssetPrefix, http.FileServer(http.FS(subFs)))) http.Handle(httpAssetPrefix, http.StripPrefix(httpAssetPrefix, http.FileServer(http.FS(subFs))))
http.HandleFunc("/", ContainerHandler) http.HandleFunc("/", ContainerHandler)
logrus.Infof("Listening on %s...", Config.Listen) logrus.Infof("Listening on %s...", config.Model.Listen)
http.ListenAndServe(Config.Listen, nil) http.ListenAndServe(config.Model.Listen, nil)
} }
func stopAllLazyContainers() error { func stopAllLazyContainers() error {
@@ -213,9 +214,9 @@ func getOrCreateState(cid string) (ret *containerState) {
func parseContainerSettings(target *containerState, ct *types.Container) { func parseContainerSettings(target *containerState, ct *types.Container) {
{ // Parse stop delay { // Parse stop delay
stopDelay, _ := labelOrDefault(ct, "stopdelay", Config.StopDelay.String()) stopDelay, _ := labelOrDefault(ct, "stopdelay", config.Model.StopDelay.String())
if dur, stopErr := time.ParseDuration(stopDelay); stopErr != nil { if dur, stopErr := time.ParseDuration(stopDelay); stopErr != nil {
target.StopDelay = Config.StopDelay target.StopDelay = config.Model.StopDelay
logrus.Warnf("Unable to parse stopdelay of %s, defaulting to %s", stopDelay, target.StopDelay.String()) logrus.Warnf("Unable to parse stopdelay of %s, defaulting to %s", stopDelay, target.StopDelay.String())
} else { } else {
target.StopDelay = dur target.StopDelay = dur
@@ -254,7 +255,7 @@ func findContainerByHostname(ctx context.Context, hostname string) (*types.Conta
// Finds all containers on node that are labeled with lazyloader config // Finds all containers on node that are labeled with lazyloader config
func findAllLazyloadContainers(ctx context.Context, includeStopped bool) ([]types.Container, error) { func findAllLazyloadContainers(ctx context.Context, includeStopped bool) ([]types.Container, error) {
filters := filters.NewArgs() filters := filters.NewArgs()
filters.Add("label", Config.Labels.Prefix) filters.Add("label", config.Model.Labels.Prefix)
return dockerClient.ContainerList(ctx, types.ContainerListOptions{ return dockerClient.ContainerList(ctx, types.ContainerListOptions{
All: includeStopped, All: includeStopped,

View File

@@ -1,10 +1,11 @@
package main package config
import ( import (
_ "embed" _ "embed"
"strings" "strings"
"time" "time"
"github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@@ -22,7 +23,7 @@ type ConfigModel struct {
} `mapstructure:"labels"` } `mapstructure:"labels"`
} }
var Config *ConfigModel = new(ConfigModel) var Model *ConfigModel = new(ConfigModel)
func init() { func init() {
viper.AddConfigPath(".") viper.AddConfigPath(".")
@@ -34,14 +35,14 @@ func init() {
viper.AutomaticEnv() viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
panic(err) logrus.Fatal(err)
} }
if err := viper.Unmarshal(Config); err != nil { if err := viper.Unmarshal(Model); err != nil {
panic(err) logrus.Fatal(err)
} }
} }
func subLabel(name string) string { func SubLabel(name string) string {
return Config.Labels.Prefix + "." + name return Model.Labels.Prefix + "." + name
} }

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"traefik-lazyload/pkg/config"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
) )
@@ -15,7 +16,7 @@ func sumNetworkBytes(networks map[string]types.NetworkStats) (recv int64, send i
} }
func labelOrDefault(ct *types.Container, sublabel, dflt string) (string, bool) { func labelOrDefault(ct *types.Container, sublabel, dflt string) (string, bool) {
if val, ok := ct.Labels[subLabel(sublabel)]; ok { if val, ok := ct.Labels[config.SubLabel(sublabel)]; ok {
return val, true return val, true
} }
return dflt, false return dflt, false