From 519796d3d107ae189d2490b8b74c5e65686e2fab Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 12 Apr 2024 21:40:22 +0200 Subject: [PATCH] Improve data location selection --- Dockerfile | 14 ++++++++------ src/ecodash/config.go | 12 ++++++++++-- src/ecodash/http.go | 7 ++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 47f94d0..f6f7e04 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,10 +9,9 @@ COPY src /app/src COPY go.mod /app/ COPY .golangci.yml /app/ -RUN go mod tidy -RUN golangci-lint run -RUN go test ./src/... - +RUN go mod tidy; \ + golangci-lint run; \ + go test ./src/... RUN CGO_ENABLED=1 go build -o app src/main/main.go FROM alpine:latest @@ -21,6 +20,9 @@ WORKDIR /app COPY --from=1 /app/app . COPY ./templates /app/templates -RUN touch config.json database.db -CMD ["./app"] \ No newline at end of file +RUN mkdir data +ENV DATABASE_PATH=./data/database.db +ENV CONFIG_PATH=./data/config.json + +CMD "./app" \ No newline at end of file diff --git a/src/ecodash/config.go b/src/ecodash/config.go index c598902..b183666 100644 --- a/src/ecodash/config.go +++ b/src/ecodash/config.go @@ -70,7 +70,11 @@ func formatURL(url string) (string, error) { } func LoadConfig() (config *Config, err error) { - db, err := sql.Open("sqlite", "./database.db") + var dbPath string + if dbPath = os.Getenv("DATABASE_PATH"); dbPath == "" { + dbPath = "./database.db" + } + db, err := sql.Open("sqlite", dbPath) if err != nil { return &Config{}, err } @@ -99,7 +103,11 @@ func LoadConfig() (config *Config, err error) { }) defaultConfig.db = db - data, err := os.ReadFile("config.json") + var confPath string + if confPath = os.Getenv("CONFIG_PATH"); confPath == "" { + confPath = "./config.json" + } + data, err := os.ReadFile(confPath) if err != nil { // if the data file doesn't exist, we consider it a first run if os.IsNotExist(err) { diff --git a/src/ecodash/http.go b/src/ecodash/http.go index 46b9030..02560ae 100644 --- a/src/ecodash/http.go +++ b/src/ecodash/http.go @@ -153,7 +153,12 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error { } *config = form - return os.WriteFile("config.json", js, 0o600) + + var confPath string + if confPath = os.Getenv("CONFIG_PATH"); confPath == "" { + confPath = "./config.json" + } + return os.WriteFile(confPath, js, 0o600) } func averageExcludingCurrentDay(data []float32) float32 {