forked from massivebox/ecodash
MassiveBox
7a1214d492
- The program will now be cross-compiled and released for arm as well as x86 - What we previously called "cache" is not actually a cache, as it holds content that can't be always retrieved. Now we're stopping calling it a cache. - Improved history merging - Fixed the README to include the database as a volume, and to fix some errors
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
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.updateHistory)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
cr.Start()
|
|
config.updateHistory()
|
|
}
|
|
|
|
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))
|
|
|
|
}
|