GoBlog/nodeinfo.go

65 lines
1.6 KiB
Go
Raw Normal View History

2020-11-17 16:10:14 +00:00
package main
2021-01-21 16:59:47 +00:00
import (
"encoding/json"
"io"
2021-01-21 16:59:47 +00:00
"net/http"
2022-02-25 15:29:42 +00:00
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/contenttype"
2020-11-17 16:10:14 +00:00
)
func (a *goBlog) serveNodeInfoDiscover(w http.ResponseWriter, r *http.Request) {
2022-02-25 15:29:42 +00:00
buf := bufferpool.Get()
defer bufferpool.Put(buf)
2022-03-16 07:28:03 +00:00
err := json.NewEncoder(buf).Encode(map[string]any{
"links": []map[string]any{
2021-01-21 16:59:47 +00:00
{
"href": a.getFullAddress("/nodeinfo"),
2021-01-21 16:59:47 +00:00
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
2020-11-17 16:10:14 +00:00
},
},
})
2022-02-25 15:29:42 +00:00
if err != nil {
a.serveError(w, r, "", http.StatusInternalServerError)
return
}
w.Header().Set(contentType, contenttype.JSONUTF8)
2022-04-10 09:46:35 +00:00
mw := a.min.Get().Writer(contenttype.JSON, w)
_, _ = io.Copy(mw, buf)
2022-02-25 15:29:42 +00:00
_ = mw.Close()
2020-11-17 16:10:14 +00:00
}
func (a *goBlog) serveNodeInfo(w http.ResponseWriter, r *http.Request) {
localPosts, _ := a.db.countPosts(&postsRequestConfig{
status: []postStatus{statusPublished},
visibility: []postVisibility{visibilityPublic},
2021-01-15 20:56:46 +00:00
})
2022-02-25 15:29:42 +00:00
buf := bufferpool.Get()
defer bufferpool.Put(buf)
2022-04-10 09:46:35 +00:00
if err := json.NewEncoder(buf).Encode(map[string]any{
2021-01-21 16:59:47 +00:00
"version": "2.1",
2022-03-16 07:28:03 +00:00
"software": map[string]any{
2021-01-21 16:59:47 +00:00
"name": "goblog",
"repository": "https://go.goblog.app/app",
2021-01-21 16:59:47 +00:00
},
2022-03-16 07:28:03 +00:00
"usage": map[string]any{
"users": map[string]any{
"total": len(a.cfg.Blogs),
2021-01-21 16:59:47 +00:00
},
"localPosts": localPosts,
},
"protocols": []string{
"activitypub",
"micropub",
"webmention",
2020-11-17 16:10:14 +00:00
},
2022-03-16 07:28:03 +00:00
"metadata": map[string]any{},
2022-04-10 09:46:35 +00:00
}); err != nil {
2022-02-25 15:29:42 +00:00
a.serveError(w, r, "", http.StatusInternalServerError)
return
}
w.Header().Set(contentType, contenttype.JSONUTF8)
2022-04-10 09:46:35 +00:00
_ = a.min.Get().Minify(contenttype.JSON, w, buf)
2020-11-17 16:10:14 +00:00
}