forked from massivebox/ecodash
First commit
This commit is contained in:
commit
82bba66fd6
30 changed files with 2176 additions and 0 deletions
74
main.go
Normal file
74
main.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
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))
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue