This repository has been archived by the owner on Dec 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
61 lines (49 loc) · 1.38 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
61
package ostatus
import (
"net/http"
"github.com/emersion/go-ostatus/pubsubhubbub"
"github.com/emersion/go-ostatus/salmon"
"github.com/emersion/go-ostatus/xrd"
"github.com/emersion/go-ostatus/xrd/hostmeta"
"github.com/emersion/go-ostatus/xrd/webfinger"
)
// Default endpoints.
var (
HubPath = "/hub"
SalmonPath = "/salmon"
)
// Handler handles OStatus requests.
type Handler struct {
http.Handler
Publisher *pubsubhubbub.Publisher
}
// NewHandler creates a new OStatus endpoint.
func NewHandler(be Backend, hostmetaResource *xrd.Resource) *Handler {
mux := http.NewServeMux()
h := &Handler{Handler: mux}
p := pubsubhubbub.NewPublisher(be)
h.Publisher = p
mux.Handle(hostmeta.WellKnownPath, hostmeta.NewHandler(hostmetaResource))
mux.Handle(webfinger.WellKnownPath, webfinger.NewHandler(be))
mux.Handle(HubPath, p)
mux.Handle(SalmonPath, salmon.NewHandler(be))
mux.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) {
topic := req.URL.String()
feed, err := be.Feed(topic)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
resp.Header().Set("Content-Type", "application/atom+xml")
if feed.ID == "" && len(feed.Entry) == 1 {
err = feed.Entry[0].WriteTo(resp)
} else {
err = feed.WriteTo(resp)
}
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
})
return h
}