ecodash/main.go

75 lines
1.5 KiB
Go
Raw Normal View History

2022-10-15 17:58:44 +00:00
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 {
return
}
cr.Start()
config.updateCache()
}
engine := html.New("./templates/"+config.Dashboard.Theme, ".html")
engine.AddFunc("divide", templateDivide)
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", fiber.Map{
"Defaults": config.getTemplateDefaults(),
}, "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", fiber.Map{
"Defaults": config.getTemplateDefaults(),
}, "base")
}
return c.Redirect("./", 307)
})
port := os.Getenv("PORT")
if port == "" {
port = "80"
}
log.Fatal(app.Listen(":" + port))
}