-
Notifications
You must be signed in to change notification settings - Fork 11
/
routes.go
83 lines (74 loc) · 2.55 KB
/
routes.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
package readas
import (
"github.com/gorilla/mux"
"github.com/writeas/go-nodeinfo"
"github.com/writeas/go-webfinger"
"net/http"
)
func initRoutes(app *app) {
app.router = mux.NewRouter()
// Federation endpoints
wf := webfinger.Default(wfResolver{app})
wf.NoTLSHandler = nil
// host-meta
app.router.HandleFunc("/.well-known/host-meta", app.handler(handleViewHostMeta))
// webfinger
app.router.HandleFunc(webfinger.WebFingerPath, http.HandlerFunc(wf.Webfinger))
// nodeinfo
niCfg := nodeInfoConfig(app.cfg)
ni := nodeinfo.NewService(*niCfg, nodeInfoResolver{app})
app.router.HandleFunc(nodeinfo.NodeInfoPath, http.HandlerFunc(ni.NodeInfoDiscover))
app.router.HandleFunc(niCfg.InfoURL, http.HandlerFunc(ni.NodeInfo))
api := app.router.PathPrefix("/api/").Subrouter()
api.HandleFunc("/auth/login", app.handler(handleLogin)).Methods("POST")
api.HandleFunc("/collections/{alias}", app.handler(handleFetchUser)).Methods("GET")
collectionsAPI := api.PathPrefix("/collections/{alias}").Subrouter()
collectionsAPI.HandleFunc("/", app.handler(handleFetchUser)).Methods("GET")
collectionsAPI.HandleFunc("/inbox", app.handler(handleFetchInbox)).Methods("POST")
collectionsAPI.HandleFunc("/outbox", app.handler(handleFetchOutbox)).Methods("GET")
collectionsAPI.HandleFunc("/following", app.handler(handleFetchFollowing)).Methods("GET")
collectionsAPI.HandleFunc("/followers", app.handler(handleFetchFollowers)).Methods("GET")
api.HandleFunc("/follow", app.handler(handleFollowUser))
api.HandleFunc("/inbox", app.handler(handleFetchInbox))
app.router.HandleFunc("/logout", app.handler(handleLogout))
app.router.HandleFunc("/p/{id}", app.handler(handleViewPost))
app.router.HandleFunc("/", app.handler(handleViewHome))
app.router.PathPrefix("/").Handler(http.FileServer(http.Dir("static/")))
}
func handleViewHome(app *app, w http.ResponseWriter, r *http.Request) error {
cu := getUserSession(app, r)
var u *LocalUser
var err error
if cu != nil {
u, err = app.getLocalUser(cu.PreferredUsername)
if err != nil {
return err
}
}
p := struct {
User *LocalUser
Version string
InstanceName string
Username string
Flash string
To string
Posts *[]Post
}{
User: u,
Version: softwareVersion,
InstanceName: app.cfg.Name,
Username: r.FormValue("username"),
To: r.FormValue("to"),
Posts: &[]Post{},
}
if u != nil {
p.Posts, err = app.getUserFeed(u.ID, 1)
if err != nil {
return err
}
}
if err := renderTemplate(w, "index", p); err != nil {
return err
}
return nil
}