83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
0
|
// Copyright (C) 2023, Jakob Wakeling |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
1
|
// All rights reserved. |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
2
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
3
|
package admin |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
4
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
5
|
import ( |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
6
|
"fmt" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
7
|
"log" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
8
|
"net/http" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
9
|
"runtime" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
10
|
"strings" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
11
|
"time" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
12
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
13
|
"github.com/Jamozed/Goit/res" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
14
|
"github.com/Jamozed/Goit/src/goit" |
c8b452e |
Jakob Wakeling |
2024-01-12 17:15:27 |
15
|
"github.com/Jamozed/Goit/src/util" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
16
|
"github.com/dustin/go-humanize" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
17
|
) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
18
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
19
|
func HandleStatus(w http.ResponseWriter, r *http.Request) { |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
20
|
auth, user, err := goit.Auth(w, r, true) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
21
|
if err != nil { |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
22
|
log.Println("[admin]", err.Error()) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
23
|
goit.HttpError(w, http.StatusInternalServerError) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
24
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
25
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
26
|
if !auth || !user.IsAdmin { |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
27
|
goit.HttpError(w, http.StatusNotFound) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
28
|
return |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
29
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
30
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
31
|
mem := runtime.MemStats{} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
32
|
runtime.ReadMemStats(&mem) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
33
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
34
|
data := struct { |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
35
|
Title, Version, Uptime string |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
36
|
Goroutines int |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
37
|
Memory, Stack, Heap string |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
38
|
}{ |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
39
|
Title: "Admin - Status", |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
40
|
Version: res.Version, |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
41
|
Uptime: formatUptime(time.Since(goit.StartTime)), |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
42
|
Goroutines: runtime.NumGoroutine(), |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
43
|
Memory: humanize.Bytes(mem.Sys), |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
44
|
Stack: humanize.Bytes(mem.StackInuse), |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
45
|
Heap: humanize.Bytes(mem.HeapInuse), |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
46
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
47
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
48
|
if err := goit.Tmpl.ExecuteTemplate(w, "admin/status", data); err != nil { |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
49
|
log.Println("[/admin/status]", err.Error()) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
50
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
51
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
52
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
53
|
func formatUptime(uptime time.Duration) string { |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
54
|
const ( |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
55
|
day = time.Hour * 24 |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
56
|
week = day * 7 |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
57
|
) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
58
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
59
|
weeks := int64(uptime / week) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
60
|
uptime -= time.Duration(weeks) * week |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
61
|
days := int64(uptime / day) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
62
|
uptime -= time.Duration(days) * day |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
63
|
hours := int64(uptime / time.Hour) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
64
|
uptime -= time.Duration(hours) * time.Hour |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
65
|
minutes := int64(uptime / time.Minute) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
66
|
uptime -= time.Duration(minutes) * time.Minute |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
67
|
seconds := int64(uptime / time.Second) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
68
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
69
|
var parts []string |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
70
|
if weeks > 0 { |
c8b452e |
Jakob Wakeling |
2024-01-12 17:15:27 |
71
|
parts = append(parts, fmt.Sprintf(util.If(weeks == 1, "%d week", "%d weeks"), weeks)) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
72
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
73
|
if days > 0 { |
c8b452e |
Jakob Wakeling |
2024-01-12 17:15:27 |
74
|
parts = append(parts, fmt.Sprintf(util.If(days == 1, "%d day", "%d days"), days)) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
75
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
76
|
if hours > 0 { |
c8b452e |
Jakob Wakeling |
2024-01-12 17:15:27 |
77
|
parts = append(parts, fmt.Sprintf(util.If(days == 1, "%d hour", "%d hours"), hours)) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
78
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
79
|
if minutes > 0 { |
c8b452e |
Jakob Wakeling |
2024-01-12 17:15:27 |
80
|
parts = append(parts, fmt.Sprintf(util.If(days == 1, "%d minute", "%d minutes"), minutes)) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
81
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
82
|
if seconds > 0 { |
c8b452e |
Jakob Wakeling |
2024-01-12 17:15:27 |
83
|
parts = append(parts, fmt.Sprintf(util.If(days == 1, "%d second", "%d seconds"), seconds)) |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
84
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
85
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
86
|
if len(parts) == 0 { |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
87
|
return "0 seconds" |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
88
|
} |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
89
|
|
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
90
|
return strings.Join(parts, ", ") |
83800a4 |
Jakob Wakeling |
2023-12-25 23:04:58 |
91
|
} |
|
|
|
92
|
|