From 35fcfc759aec8d3421cdfb437b951ac21e0a206c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Fri, 8 Jan 2021 22:37:38 +0100 Subject: [PATCH] Configuration structure * Configurable apps path --- common/config.go | 24 ++++++++++++++++++++++++ config.go | 4 ++-- docker/types.go | 10 +++++++--- go.mod | 1 + go.sum | 2 ++ 5 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 common/config.go diff --git a/common/config.go b/common/config.go new file mode 100644 index 0000000..aabec34 --- /dev/null +++ b/common/config.go @@ -0,0 +1,24 @@ +package common + +import ( + "log" + + "github.com/kelseyhightower/envconfig" +) + +// Config keeps info about configuration of this daemon +type Config struct { + AppsPath string `envconfig:"APPS_PATH" default:"/srv"` // Where applications are located +} + +// GetConfig return configuration created based on environment variables +func GetConfig() *Config { + var config Config + + err := envconfig.Process("", &config) + if err != nil { + log.Fatal(err.Error()) + } + + return &config +} diff --git a/config.go b/config.go index 23ddb22..ab6fa2f 100644 --- a/config.go +++ b/config.go @@ -2,10 +2,10 @@ package main import ( "io/ioutil" - "os" "log" + "os" - "github.com/satori/go.uuid" + uuid "github.com/satori/go.uuid" ) const configDirectory = "/var/lib/node-api" diff --git a/docker/types.go b/docker/types.go index 54cdd76..40f522e 100644 --- a/docker/types.go +++ b/docker/types.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/rosti-cz/node-api/apps" + "github.com/rosti-cz/node-api/common" ) // username in the containers under which all containers run @@ -30,7 +31,8 @@ func (c *Container) getDriver() *Driver { // volumeHostPath each container has one volume mounted into it, func (c *Container) volumeHostPath() string { - return path.Join("/srv", c.App.Name) + config := common.GetConfig() + return path.Join(config.AppsPath, c.App.Name) } // GetRawResourceStats returns RAW CPU and memory usage directly from Docker API @@ -73,7 +75,8 @@ func (c *Container) GetState() (*apps.AppState, error) { func (c *Container) Status() (string, error) { status := "unknown" - // if _, err := os.Stat(path.Join("/srv", c.App.Name)); !os.IsNotExist(err) { + // config := common.GetConfig() + // if _, err := os.Stat(path.Join(config.AppsPath, c.App.Name)); !os.IsNotExist(err) { // status = "data-only" // } @@ -176,7 +179,8 @@ func (c *Container) Delete() error { } } - volumePath := path.Join("/srv", c.App.Name) + config := common.GetConfig() + volumePath := path.Join(config.AppsPath, c.App.Name) err = removeDirectory(volumePath) if err != nil { log.Println(err) diff --git a/go.mod b/go.mod index 2bb47dc..f414968 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/docker/go-units v0.4.0 // indirect github.com/gobuffalo/packr v1.30.1 github.com/jinzhu/gorm v1.9.14 + github.com/kelseyhightower/envconfig v1.4.0 github.com/labstack/echo v3.3.10+incompatible github.com/labstack/gommon v0.3.0 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect diff --git a/go.sum b/go.sum index b5d14b3..acd4abf 100644 --- a/go.sum +++ b/go.sum @@ -83,6 +83,8 @@ github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqx github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/karrick/godirwalk v1.10.12/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=