Improve data location selection

This commit is contained in:
MassiveBox 2024-04-12 21:40:22 +02:00
parent 1f9827505b
commit 519796d3d1
Signed by: massivebox
GPG key ID: 9B74D3A59181947D
3 changed files with 24 additions and 9 deletions

View file

@ -9,10 +9,9 @@ COPY src /app/src
COPY go.mod /app/ COPY go.mod /app/
COPY .golangci.yml /app/ COPY .golangci.yml /app/
RUN go mod tidy RUN go mod tidy; \
RUN golangci-lint run golangci-lint run; \
RUN go test ./src/... go test ./src/...
RUN CGO_ENABLED=1 go build -o app src/main/main.go RUN CGO_ENABLED=1 go build -o app src/main/main.go
FROM alpine:latest FROM alpine:latest
@ -21,6 +20,9 @@ WORKDIR /app
COPY --from=1 /app/app . COPY --from=1 /app/app .
COPY ./templates /app/templates COPY ./templates /app/templates
RUN touch config.json database.db
CMD ["./app"] RUN mkdir data
ENV DATABASE_PATH=./data/database.db
ENV CONFIG_PATH=./data/config.json
CMD "./app"

View file

@ -70,7 +70,11 @@ func formatURL(url string) (string, error) {
} }
func LoadConfig() (config *Config, err 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 { if err != nil {
return &Config{}, err return &Config{}, err
} }
@ -99,7 +103,11 @@ func LoadConfig() (config *Config, err error) {
}) })
defaultConfig.db = db 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 err != nil {
// if the data file doesn't exist, we consider it a first run // if the data file doesn't exist, we consider it a first run
if os.IsNotExist(err) { if os.IsNotExist(err) {

View file

@ -153,7 +153,12 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error {
} }
*config = form *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 { func averageExcludingCurrentDay(data []float32) float32 {