2022-10-15 17:58:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-03 21:53:54 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2023-05-01 18:34:50 +00:00
|
|
|
"git.massivebox.net/ecodash/ecodash/src/ecodash"
|
|
|
|
"git.massivebox.net/ecodash/ecodash/src/tools"
|
2023-05-01 16:33:15 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/template/html"
|
|
|
|
"github.com/robfig/cron/v3"
|
2022-10-15 17:58:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-10-29 17:32:35 +00:00
|
|
|
config, err := ecodash.LoadConfig()
|
2022-10-15 17:58:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-10-29 17:32:35 +00:00
|
|
|
cr := cron.New()
|
|
|
|
_, err = cr.AddFunc("@hourly", config.UpdateHistory)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2022-10-15 17:58:44 +00:00
|
|
|
}
|
2023-10-29 17:32:35 +00:00
|
|
|
cr.Start()
|
|
|
|
config.UpdateHistory()
|
2022-10-15 17:58:44 +00:00
|
|
|
|
|
|
|
engine := html.New("./templates/"+config.Dashboard.Theme, ".html")
|
2023-05-01 17:39:12 +00:00
|
|
|
engine.AddFunc("divide", tools.TemplateDivide)
|
|
|
|
engine.AddFunc("HTMLDateFormat", tools.TemplateHTMLDateFormat)
|
2022-10-15 17:58:44 +00:00
|
|
|
|
|
|
|
app := fiber.New(fiber.Config{
|
|
|
|
Views: engine,
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Static("/assets", "./templates/"+config.Dashboard.Theme+"/assets")
|
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
2023-10-29 17:32:35 +00:00
|
|
|
if config.Administrator.Username == "" || config.Administrator.PasswordHash == "" {
|
2022-10-15 17:58:44 +00:00
|
|
|
c.Cookie(&fiber.Cookie{Name: "admin_username", Value: ""})
|
2023-05-01 17:39:12 +00:00
|
|
|
c.Cookie(&fiber.Cookie{Name: "admin_password_hash", Value: tools.Hash("")})
|
2023-10-31 22:59:09 +00:00
|
|
|
return config.RenderAdminPanel(c, nil)
|
2022-10-15 17:58:44 +00:00
|
|
|
}
|
2023-05-01 17:39:12 +00:00
|
|
|
return config.RenderIndex(c)
|
2022-10-15 17:58:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/accuracy-notice", func(c *fiber.Ctx) error {
|
2023-05-01 17:39:12 +00:00
|
|
|
return c.Render("accuracy-notice", config.TemplateDefaultsMap(), "base")
|
2022-10-15 17:58:44 +00:00
|
|
|
})
|
|
|
|
|
2023-05-01 17:39:12 +00:00
|
|
|
app.All("/admin", config.AdminEndpoint)
|
2022-10-15 17:58:44 +00:00
|
|
|
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "80"
|
|
|
|
}
|
|
|
|
log.Fatal(app.Listen(":" + port))
|
|
|
|
}
|