Configuration structure

* Configurable apps path
This commit is contained in:
Adam Štrauch 2021-01-08 22:37:38 +01:00
parent be1ef03328
commit 35fcfc759a
Signed by: cx
GPG Key ID: 018304FFA8988F8D
5 changed files with 36 additions and 5 deletions

24
common/config.go Normal file
View File

@ -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
}

View File

@ -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"

View File

@ -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)

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=