-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (73 loc) · 1.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"os"
"path"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/django/v3"
"github.com/ngn13/statpage/lib"
)
func CheckTimePassed(t time.Time) string {
diff := time.Since(t)
res := fmt.Sprintf(
"%ds ago",
int(diff.Seconds()),
)
if diff.Minutes() > 1 {
res = fmt.Sprintf(
"%dm %ds ago",
int(diff.Minutes()), int(diff.Seconds())-(int(diff.Minutes())*60),
)
}
if diff.Hours() > 1 {
res = fmt.Sprintf("%dh %dm ago",
int(diff.Hours()),
int(diff.Minutes())-(int(diff.Hours())*60),
)
}
return res
}
func main(){
lib.LoadData()
go lib.Loop()
engine := django.New("./views", ".html")
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
Views: engine,
})
app.Use(logger.New())
app.Static("/", "./public")
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"cfg": lib.GetConfig(),
"services": lib.Services,
"lastcheck": lib.LastChecked,
"checktime": CheckTimePassed,
})
})
app.Get("/theme.css", func(c *fiber.Ctx) error {
theme, err := os.ReadFile(path.Join("config", "theme.css"))
if err != nil{
if !os.IsNotExist(err) {
log.Errorf("Error reading theme: %s", err)
}
return c.Status(404).Send([]byte(""))
}
c.Set("Content-Type", "text/css; charset=utf-8")
return c.Send(theme)
})
app.Get("/favicon.ico", func(c *fiber.Ctx) error {
return c.Status(404).SendString("I don't have an icon :/")
})
app.Get("*", func(c *fiber.Ctx) error {
return c.Redirect("/")
})
addr := lib.GetConfig().Address
log.Infof("Starting the application on %s", addr)
log.Fatal(app.Listen(addr))
log.Info("Stopped the application")
close(lib.Checkchan)
}