/
endpoints.go
144 lines (123 loc) · 4.24 KB
/
endpoints.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package endpoints
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/iamcathal/booksbooksbooks/controller"
"github.com/iamcathal/booksbooksbooks/db"
"github.com/iamcathal/booksbooksbooks/dtos"
"go.uber.org/zap"
)
var (
logger *zap.Logger
appConfig dtos.AppConfig
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
func InitConfig(conf dtos.AppConfig, newLogger *zap.Logger) {
appConfig = conf
logger = newLogger
}
func SetupRouter() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/status", status).Methods("POST")
// Static content pages
r.HandleFunc("/", index).Methods("GET")
r.HandleFunc("/settings", settings).Methods("GET")
r.HandleFunc("/available", available).Methods("GET")
r.HandleFunc("/series", series).Methods("GET")
r.HandleFunc("/getrecentcrawlbreadcrumbs", getRecentCrawlBreadcrumbs).Methods("GET")
r.HandleFunc("/getavailablebooks", getAvailableBooks).Methods("GET")
r.HandleFunc("/ignorebook", ignoreBook).Methods("POST")
r.HandleFunc("/unignorebook", unignoreBook).Methods("POST")
r.HandleFunc("/resetavailablebooks", resetAvailableBooks).Methods("POST")
r.HandleFunc("/purgeignoredauthorsfromavailablebooks", purgeIgnoredAuthorsFromAvailableBooks).Methods("POST")
r.HandleFunc("/getseriescrawl", getSeriesCrawl).Methods("GET")
r.HandleFunc("/getrecentcrawlreports", getRecentCrawlReports).Methods("GET")
r.Use(logMiddleware)
setupWebsocketsRouter(r)
setupSettingsRouter(r)
r.Handle("/static", http.NotFoundHandler())
fs := http.FileServer(http.Dir(""))
r.PathPrefix("/").Handler(DisallowFileBrowsing(fs))
return r
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/index.html")
}
func available(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/available.html")
}
func series(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/series.html")
}
func settings(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/settings.html")
}
func getAvailableBooks(w http.ResponseWriter, r *http.Request) {
availableBooks := db.GetAvailableBooks()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(availableBooks)
}
func ignoreBook(w http.ResponseWriter, r *http.Request) {
bookURL := r.URL.Query().Get("bookurl")
db.IgnoreBook(bookURL)
w.WriteHeader(http.StatusOK)
}
func unignoreBook(w http.ResponseWriter, r *http.Request) {
bookURL := r.URL.Query().Get("bookurl")
db.UnignoreBook(bookURL)
w.WriteHeader(http.StatusOK)
}
func testDiscordWebook(w http.ResponseWriter, r *http.Request) {
discordWebhook := r.URL.Query().Get("webhookurl")
_, err := url.Parse(discordWebhook)
if err != nil {
errorMsg := fmt.Sprintf("Invalid webhookurl '%s' is not a valid URL", discordWebhook)
SendBasicInvalidResponse(w, r, errorMsg, http.StatusBadRequest)
return
}
db.SetDiscordWebhookURL(discordWebhook)
err = controller.Cnt.DeliverWebhook(dtos.DiscordMsg{
Username: "BooksBooksBooks",
Avatar_url: "https://cathaloc.dev/static/favicons/ms-icon-150x150.png",
Embed: []dtos.DiscordEmbed{
{
Title: "BooksBooksBooks is ready to send webhook updates to this channel",
},
},
})
if err != nil {
errorMsg := fmt.Sprintf("Invalid webhookurl '%s' given", discordWebhook)
SendBasicInvalidResponse(w, r, errorMsg, http.StatusBadRequest)
return
}
db.SetDiscordWebhookURL(discordWebhook)
w.WriteHeader(http.StatusOK)
}
func status(w http.ResponseWriter, r *http.Request) {
req := dtos.UptimeResponse{
Status: "operational",
Uptime: time.Duration(time.Since(appConfig.ApplicationStartUpTime).Milliseconds()),
StartUpTime: appConfig.ApplicationStartUpTime.Unix(),
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(req)
}
func logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// if isStaticContent := strings.HasPrefix(r.URL.Path, "/static/"); !isStaticContent {
// logger.Sugar().Infow(fmt.Sprintf("Served request to %v", r.URL.Path),
// zap.String("requestInfo", fmt.Sprintf("%+v", r)))
// }
next.ServeHTTP(w, r)
})
}