-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
130 lines (119 loc) · 3.11 KB
/
Copy pathrouter.go
File metadata and controls
130 lines (119 loc) · 3.11 KB
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
package main
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/charmbracelet/log"
"github.com/labstack/echo/v4"
)
type PostRec struct {
Post string `json:"post"`
}
type PostList struct {
Cursor string `json:"cursor"`
Feed []PostRec `json:"feed"`
}
func getDIDDoc(cfg *Feed) []byte {
txt := fmt.Sprintf(`{
"@context": [
"https://www.w3.org/ns/did/v1"
],
"id": "%s",
"service": [
{
"id": "#bsky_fg",
"type": "BskyFeedGenerator",
"serviceEndpoint": "https://%s"
}
]
}`, cfg.PublishConfig.ServiceDID, cfg.PublishConfig.ServiceHost)
return []byte(txt)
}
func startFeedService(ctx context.Context, cfg *Feed) {
r := echo.New()
r.GET("/.well-known/atproto-did", func(c echo.Context) error {
c.JSONBlob(http.StatusOK, getDIDDoc(cfg))
return nil
})
r.GET("/.well-known/did.json", func(c echo.Context) error {
c.JSONBlob(http.StatusOK, getDIDDoc(cfg))
return nil
})
r.GET("/xrpc/app.bsky.feed.getFeedSkeleton", func(c echo.Context) error {
feed := c.QueryParam("feed")
limit := c.QueryParam("limit")
cursor := c.QueryParam("cursor")
log.Info("getFeedSkeleton Params", "feed", feed, "limit", limit, "cursor", cursor)
if cfg.db == nil {
if db, err := openDatabase(cfg.DB); err == nil {
cfg.db = db
}
}
if cfg.db != nil {
if limit == "" {
limit = "25"
}
ts := ""
cid := ""
iLimit, err := strconv.ParseInt(limit, 10, 32)
if err != nil {
c.String(400, fmt.Sprintf("Bad request: malformed limit param"))
return nil
}
if cursor != "" && strings.Contains(cursor, "::") {
parts := strings.SplitN(cursor, "::", 2)
ts = parts[0]
cid = parts[1]
if ts == "" || cid == "" {
c.String(401, fmt.Sprintf("Bad request: malformed cursor"))
return nil
}
_, err = strconv.ParseInt(ts, 10, 64)
if err != nil {
c.String(402, fmt.Sprintf("Bad request: malformed cursor"))
return nil
}
}
var posts = []*Post{}
if ts != "" && cid != "" {
cfg.db.Limit(int(iLimit)).Where("c_id < ? and (indexed_at < ? or indexed_at = ?)", cid, ts, ts).Order("indexed_at desc, c_id desc").Find(&posts)
} else {
cfg.db.Limit(int(iLimit)).Order("indexed_at desc, c_id desc").Find(&posts)
}
// log.Printf("Got posts = %+v", posts)
if len(posts) > 0 {
last := posts[len(posts)-1]
list := &PostList{
Cursor: last.IndexedAt + "::" + last.CID,
Feed: []PostRec{},
}
if cid == "" && cfg.PinnedURI != "" {
list.Feed = append(list.Feed, PostRec{cfg.PinnedURI})
}
for _, p := range posts {
list.Feed = append(list.Feed, PostRec{p.URI})
}
c.JSON(http.StatusOK, list)
return nil
}
}
c.String(404, fmt.Sprintf("Posts not found"))
return nil
})
srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Handler: r,
}
go func() {
log.Info("Starting server...", "feed", cfg.ID, "port", cfg.Port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error("http server:", "feed", cfg.ID, "error", err)
}
}()
go func() {
<-ctx.Done()
srv.Shutdown(ctx)
}()
}