-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.go
82 lines (70 loc) 路 1.95 KB
/
dashboard.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
package server
import (
"fmt"
"net/http"
"github.com/hacdias/eagle/v3/eagle"
"github.com/hacdias/eagle/v3/entry"
"github.com/hacdias/indieauth/v2"
)
func (s *Server) dashboardGet(w http.ResponseWriter, r *http.Request) {
s.serveDashboard(w, r, map[string]interface{}{})
}
func (s *Server) dashboardPost(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
s.serveErrorHTML(w, r, http.StatusBadRequest, err)
return
}
actions := r.Form["action"]
data := map[string]interface{}{}
for _, action := range actions {
switch action {
case "clear-cache":
s.ResetCache()
data["Message"] = "Success!"
case "sync-storage":
go s.SyncStorage()
data["Message"] = "Success!"
case "update-blogroll":
err = s.UpdateBlogroll()
data["Message"] = "Success!"
case "update-reads-statistics":
err = s.UpdateReadsSummary()
data["Message"] = "Success!"
case "update-watches-statistics":
err = s.UpdateWatchesSummary()
data["Message"] = "Success!"
case "token":
clientID := r.Form.Get("client_id")
scope := r.Form.Get("scope")
expiry, err := handleExpiry(r.Form.Get("expiry"))
if err != nil {
s.serveErrorHTML(w, r, http.StatusBadRequest, fmt.Errorf("expiry param is invalid: %w", err))
}
if err := indieauth.IsValidClientIdentifier(clientID); err != nil {
s.serveErrorHTML(w, r, http.StatusBadRequest, fmt.Errorf("invalid client_id: %w", err))
return
}
signed, err := s.generateToken(clientID, scope, expiry)
if err == nil {
data["Token"] = signed
}
}
}
if err != nil {
s.serveErrorHTML(w, r, http.StatusInternalServerError, err)
return
}
s.serveDashboard(w, r, data)
}
func (s *Server) serveDashboard(w http.ResponseWriter, r *http.Request, data interface{}) {
s.serveHTML(w, r, &eagle.RenderData{
Entry: &entry.Entry{
Frontmatter: entry.Frontmatter{
Title: "Dashboard",
},
},
Data: data,
NoIndex: true,
}, []string{eagle.TemplateDashboard})
}