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

@ -12,7 +12,7 @@ If you really have to build it yourself, we recommend you Docker over binaries.
1. Download the Go Compiler from https://go.dev/dl/ or from your repository's package manager (it's usually called `go` or `golang`)
2. Download the Git SCM from https://git-scm.com/download/linux or from your package manager (it's always called `git`)
3. Download `golangci-lint` from https://golangci-lint.run/
4. Clone the repository by running `git clone https://gitea.massivebox.net/ecodash/ecodash.git ` inside a command prompt
4. Clone the repository by running `git clone https://git.massivebox.net/ecodash/ecodash.git ` inside a command prompt
5. Switch to the project directory with `cd ecodash`
6. Run `golangci-lint run` to lint all project files
7. Build with `go build src/main/main.go -o ecodash`. This will generate an executable, `ecodash`, in the same directory.
@ -22,9 +22,7 @@ If you really have to build it yourself, we recommend you Docker over binaries.
1. Install the latest release of the Go Compiler for Windows from https://go.dev/dl/
2. Install the Git SCM from https://git-scm.com/download/win. The "Standalone installer" is recommended. All the default settings will work fine.
3. Download `golangci-lint` from https://golangci-lint.run/
4. Clone the repository by running `git clone https://gitea.massivebox.net/ecodash/ecodash.git ` inside a command prompt
4. Clone the repository by running `git clone https://git.massivebox.net/ecodash/ecodash.git ` inside a command prompt
5. Switch to the project directory with `cd ecodash`
6. Run `golangci-lint run` to lint all project files
7. Build with `go build src/main/main.go -o ecodash`. This will generate an executable, `ecodash.exe`, in the same directory.
## Docker

View file

@ -1,6 +1,6 @@
# 🌿 EcoDash
[![status-badge](https://woodpecker.massivebox.net/api/badges/ecodash/ecodash/status.svg)](https://woodpecker.massivebox.net/ecodash/ecohttpsdash) [![Visit our website](https://cloud.massivebox.net/api/public/dl/yEzoZyW8?inline=true)](https://ecodash.xyz) [![Support the project](https://cloud.massivebox.net/api/public/dl/dthbBylL?inline=true)](https://ecodash.xyz/contribute)
[![status-badge](https://jenkins.massivebox.net/job/ecodash/job/master/badge/icon)](https://jenkins.massivebox.net/job/ecodash/) [![Visit our website](https://cloud.massivebox.net/s/DP3ZSaDS84wQ6jp/download?path=%2FEcoDash&files=visit-website.svg)](https://ecodash.xyz) [![Support the project](https://cloud.massivebox.net/s/DP3ZSaDS84wQ6jp/download?path=%2FEcoDash&files=support-the%20project.svg)](https://ecodash.xyz/contribute)
EcoDash is a simple way to show your users how much your server consumes.
It's intended as a medium of transparency, that gives your users an idea about the consumption of your machine. It's not meant to be 100% accurate.

3
jenkins/Jenkinsfile vendored
View file

@ -66,7 +66,8 @@ pipeline {
}
}
stage('Publish built files') {
stage('Publish build artifacts on tag') {
when { buildingTag() }
steps {
sh 'mv ecodash_x86 ecodash; zip -r ecodash-x86.zip templates ecodash'
archiveArtifacts artifacts: "ecodash-x86.zip"

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,25 +1,21 @@
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 {
@ -27,7 +23,6 @@ func main() {
}
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"

View file

@ -1,6 +0,0 @@
<h1>Restarting...</h1>
<p>
You should be able to continue using EcoDash soon by clicking <a href="/">here</a>.<br>
If you get an error like "Address Unreachable", make sure you've allowed your container to restart automatically.<br>
Check the error logs if the error persists.
</p>