forked from massivebox/ecodash
Refactoring
This commit is contained in:
parent
9bd3f35cc7
commit
345adcf479
7 changed files with 51 additions and 28 deletions
71
src/main/main.go
Normal file
71
src/main/main.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/template/html"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config, isFirstRun, err := 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("./", http.StatusTemporaryRedirect)
|
||||
})
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "80"
|
||||
}
|
||||
log.Fatal(app.Listen(":" + port))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue