-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
60 lines (51 loc) · 1.28 KB
/
server.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
package main
import (
_ "embed"
"encoding/json"
"fmt"
"html/template"
"log"
"main/feedpage"
"net/http"
"os"
)
//go:embed index.html
var templateStr string
var store feedpage.CurrentFeeds
func feedHandler(w http.ResponseWriter, r *http.Request) {
store.Mux.Lock()
info, _ := json.Marshal(store.Sections)
store.Mux.Unlock()
fmt.Fprint(w, string(info))
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
store.Mux.Lock()
info, _ := json.Marshal(store.Failures)
if store.Failures.IsInFailureState() {
w.WriteHeader(http.StatusInternalServerError)
}
store.Mux.Unlock()
fmt.Fprint(w, string(info))
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
temp, err := template.New("t").Parse(templateStr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
store.Mux.Lock()
temp.Execute(w, &store)
store.Mux.Unlock()
}
func main() {
store = feedpage.CurrentFeeds{
Sections: make([]feedpage.FeedSection, 0),
Failures: &feedpage.FailureBuffer{},
}
go feedpage.FeedEngine(os.Getenv("OPML_URL"), &store)
http.HandleFunc("/", indexHandler)
http.HandleFunc("/api/feed", feedHandler)
http.HandleFunc("/api/status", statusHandler)
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}