Remove need for restart on settings change
Some checks failed
ecodash/pipeline/head There was a failure building this commit

This commit is contained in:
MassiveBox 2023-10-29 18:32:35 +01:00
parent 75423645ff
commit 4bf1455ba4
Signed by: massivebox
GPG key ID: 9B74D3A59181947D
7 changed files with 28 additions and 50 deletions

View file

@ -61,10 +61,10 @@ func formatURL(url string) (string, error) {
return url, nil
}
func LoadConfig() (config *Config, isFirstRun bool, err error) {
func LoadConfig() (config *Config, err error) {
db, err := sql.Open("sqlite", "./database.db")
if err != nil {
return &Config{}, false, err
return &Config{}, err
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS "cache" (
@ -74,7 +74,7 @@ func LoadConfig() (config *Config, isFirstRun bool, err error) {
PRIMARY KEY("time")
);`)
if err != nil {
return &Config{}, false, err
return &Config{}, err
}
defaultConfig := &Config{}
@ -95,24 +95,24 @@ func LoadConfig() (config *Config, isFirstRun bool, err error) {
if err != nil {
// if the data file doesn't exist, we consider it a first run
if os.IsNotExist(err) {
return defaultConfig, true, nil
return defaultConfig, nil
}
return &Config{}, false, err
return &Config{}, err
}
// if the data file is empty, we consider it as a first run
if len(data) == 0 {
return defaultConfig, true, nil
return defaultConfig, nil
}
conf := &Config{}
err = json.Unmarshal(data, &conf)
if err != nil {
return &Config{}, false, err
return &Config{}, err
}
conf.db = db
return conf, false, nil
return conf, nil
}
func (config *Config) IsAuthorized(c *fiber.Ctx) bool {

View file

@ -53,9 +53,9 @@ func (config *Config) AdminEndpoint(c *fiber.Ctx) error {
})
}
return config.RenderAdminPanel(c, Warning{
Header: "Restart needed",
Body: "In order to apply changes, please <b>restart EcoDash</b>.<br>" +
"If you're running via Docker, click <a href='./restart'>here</a> to restart automatically.",
Header: "Settings applied",
Body: "Your settings have been tested and <b>applied successfully</b>.<br>" +
"You can continue using EcoDash on the <a href='./'>Home</a>.",
IsSuccess: true,
})
}
@ -117,7 +117,7 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error {
return err
}
form := &Config{
form := Config{
HomeAssistant: HomeAssistant{ /*BaseURL to be filled later*/ APIKey: c.FormValue("api_key"), InstallationDate: dayStart(parsedTime)},
Sensors: Sensors{PolledSmartEnergySummation: c.FormValue("polled_smart_energy_summation"), FossilPercentage: c.FormValue("fossil_percentage")},
Administrator: Administrator{Username: c.FormValue("username") /*PasswordHash to be filled later*/},
@ -151,6 +151,7 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error {
return err
}
*config = form
return os.WriteFile("config.json", js, 0o600)
}

View file

@ -1,33 +1,28 @@
package main
import (
"log"
"net/http"
"os"
"time"
"git.massivebox.net/ecodash/ecodash/src/ecodash"
"git.massivebox.net/ecodash/ecodash/src/tools"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
"github.com/robfig/cron/v3"
"log"
"os"
)
func main() {
config, isFirstRun, err := ecodash.LoadConfig()
config, err := ecodash.LoadConfig()
if err != nil {
log.Fatal(err)
}
if !isFirstRun {
cr := cron.New()
_, err = cr.AddFunc("@hourly", config.UpdateHistory)
if err != nil {
log.Fatal(err)
}
cr.Start()
config.UpdateHistory()
cr := cron.New()
_, err = cr.AddFunc("@hourly", config.UpdateHistory)
if err != nil {
log.Fatal(err)
}
cr.Start()
config.UpdateHistory()
engine := html.New("./templates/"+config.Dashboard.Theme, ".html")
engine.AddFunc("divide", tools.TemplateDivide)
@ -40,7 +35,7 @@ func main() {
app.Static("/assets", "./templates/"+config.Dashboard.Theme+"/assets")
app.Get("/", func(c *fiber.Ctx) error {
if isFirstRun {
if config.Administrator.Username == "" || config.Administrator.PasswordHash == "" {
c.Cookie(&fiber.Cookie{Name: "admin_username", Value: ""})
c.Cookie(&fiber.Cookie{Name: "admin_password_hash", Value: tools.Hash("")})
return config.RenderAdminPanel(c)
@ -54,17 +49,6 @@ func main() {
app.All("/admin", config.AdminEndpoint)
app.Get("/restart", func(c *fiber.Ctx) error {
if config.IsAuthorized(c) {
go func() {
time.Sleep(time.Second)
os.Exit(1)
}()
return c.Render("restart", config.TemplateDefaultsMap(), "base")
}
return c.Redirect("./", http.StatusTemporaryRedirect)
})
port := os.Getenv("PORT")
if port == "" {
port = "80"