mirror of
https://github.com/zix99/traefik-lazyload.git
synced 2025-12-21 13:23:04 +01:00
Move config into package
This commit is contained in:
19
main.go
19
main.go
@@ -13,6 +13,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"traefik-lazyload/pkg/config"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
@@ -32,7 +33,7 @@ type SplashModel struct {
|
||||
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
|
||||
|
||||
@@ -68,12 +69,12 @@ func main() {
|
||||
logrus.Infof("Connected docker to %s", info.Name)
|
||||
}
|
||||
|
||||
if splash, err := httpAssets.ReadFile(path.Join("assets", Config.Splash)); err != nil || len(splash) == 0 {
|
||||
logrus.Fatal("Unable to open splash file %s", Config.Splash)
|
||||
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.Model.Splash)
|
||||
}
|
||||
|
||||
// Initial state
|
||||
if Config.StopAtBoot {
|
||||
if config.Model.StopAtBoot {
|
||||
stopAllLazyContainers()
|
||||
} else {
|
||||
//TODO: Inventory currently running containers
|
||||
@@ -85,8 +86,8 @@ func main() {
|
||||
http.Handle(httpAssetPrefix, http.StripPrefix(httpAssetPrefix, http.FileServer(http.FS(subFs))))
|
||||
http.HandleFunc("/", ContainerHandler)
|
||||
|
||||
logrus.Infof("Listening on %s...", Config.Listen)
|
||||
http.ListenAndServe(Config.Listen, nil)
|
||||
logrus.Infof("Listening on %s...", config.Model.Listen)
|
||||
http.ListenAndServe(config.Model.Listen, nil)
|
||||
}
|
||||
|
||||
func stopAllLazyContainers() error {
|
||||
@@ -213,9 +214,9 @@ func getOrCreateState(cid string) (ret *containerState) {
|
||||
|
||||
func parseContainerSettings(target *containerState, ct *types.Container) {
|
||||
{ // 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 {
|
||||
target.StopDelay = Config.StopDelay
|
||||
target.StopDelay = config.Model.StopDelay
|
||||
logrus.Warnf("Unable to parse stopdelay of %s, defaulting to %s", stopDelay, target.StopDelay.String())
|
||||
} else {
|
||||
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
|
||||
func findAllLazyloadContainers(ctx context.Context, includeStopped bool) ([]types.Container, error) {
|
||||
filters := filters.NewArgs()
|
||||
filters.Add("label", Config.Labels.Prefix)
|
||||
filters.Add("label", config.Model.Labels.Prefix)
|
||||
|
||||
return dockerClient.ContainerList(ctx, types.ContainerListOptions{
|
||||
All: includeStopped,
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -22,7 +23,7 @@ type ConfigModel struct {
|
||||
} `mapstructure:"labels"`
|
||||
}
|
||||
|
||||
var Config *ConfigModel = new(ConfigModel)
|
||||
var Model *ConfigModel = new(ConfigModel)
|
||||
|
||||
func init() {
|
||||
viper.AddConfigPath(".")
|
||||
@@ -34,14 +35,14 @@ func init() {
|
||||
viper.AutomaticEnv()
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
panic(err)
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(Config); err != nil {
|
||||
panic(err)
|
||||
if err := viper.Unmarshal(Model); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func subLabel(name string) string {
|
||||
return Config.Labels.Prefix + "." + name
|
||||
func SubLabel(name string) string {
|
||||
return Model.Labels.Prefix + "." + name
|
||||
}
|
||||
3
util.go
3
util.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"traefik-lazyload/pkg/config"
|
||||
|
||||
"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) {
|
||||
if val, ok := ct.Labels[subLabel(sublabel)]; ok {
|
||||
if val, ok := ct.Labels[config.SubLabel(sublabel)]; ok {
|
||||
return val, true
|
||||
}
|
||||
return dflt, false
|
||||
|
||||
Reference in New Issue
Block a user