diff --git a/BUILD.md b/BUILD.md index b9d6e3f..260e9c9 100644 --- a/BUILD.md +++ b/BUILD.md @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index 0469ba7..ea9e6c6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 0b94e58..dac4bc1 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -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" diff --git a/src/ecodash/config.go b/src/ecodash/config.go index 91b7541..2856159 100644 --- a/src/ecodash/config.go +++ b/src/ecodash/config.go @@ -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 { diff --git a/src/ecodash/http.go b/src/ecodash/http.go index 7fbd1b3..bd17c71 100644 --- a/src/ecodash/http.go +++ b/src/ecodash/http.go @@ -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 restart EcoDash.
" + - "If you're running via Docker, click here to restart automatically.", + Header: "Settings applied", + Body: "Your settings have been tested and applied successfully.
" + + "You can continue using EcoDash on the Home.", 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) } diff --git a/src/main/main.go b/src/main/main.go index 4fcba7a..4b4c4aa 100644 --- a/src/main/main.go +++ b/src/main/main.go @@ -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" diff --git a/templates/default/restart.html b/templates/default/restart.html deleted file mode 100644 index 2a9ca60..0000000 --- a/templates/default/restart.html +++ /dev/null @@ -1,6 +0,0 @@ -

Restarting...

-

- You should be able to continue using EcoDash soon by clicking here.
- If you get an error like "Address Unreachable", make sure you've allowed your container to restart automatically.
- Check the error logs if the error persists. -

\ No newline at end of file