Turns out HomeAssistant only returns 10 days'data byault. This is a problem that we will havsoon. Now the cache doesn't reset eh time it only if some data is missing.
72 lines
1.5 KiB
Go
Executable file
72 lines
1.5 KiB
Go
Executable file
package main
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/template/html"
|
|
"github.com/robfig/cron/v3"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
|
|
config, err, isFirstRun := loadConfig()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if !isFirstRun {
|
|
cr := cron.New()
|
|
_, err = cr.AddFunc("@hourly", config.updateCache)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
cr.Start()
|
|
config.updateCache()
|
|
}
|
|
|
|
engine := html.New("./templates/"+config.Dashboard.Theme, ".html")
|
|
engine.AddFunc("divide", templateDivide)
|
|
engine.AddFunc("HTMLDateFormat", templateHTMLDateFormat)
|
|
|
|
app := fiber.New(fiber.Config{
|
|
Views: engine,
|
|
})
|
|
|
|
app.Static("/assets", "./templates/"+config.Dashboard.Theme+"/assets")
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
if isFirstRun {
|
|
c.Cookie(&fiber.Cookie{Name: "admin_username", Value: ""})
|
|
c.Cookie(&fiber.Cookie{Name: "admin_password_hash", Value: hash("")})
|
|
return config.renderAdminPanel(c)
|
|
}
|
|
return config.renderIndex(c)
|
|
})
|
|
|
|
app.Get("/accuracy-notice", func(c *fiber.Ctx) error {
|
|
return c.Render("accuracy-notice", config.templateDefaultsMap(), "base")
|
|
})
|
|
|
|
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("./", 307)
|
|
})
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "80"
|
|
}
|
|
log.Fatal(app.Listen(":" + port))
|
|
|
|
}
|