Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
description = "";
inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1.0.tar.gz";
inputs.flake-utils.url = "github:numtide/flake-utils";

outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
inherit system;
config = { allowUnfree = true; };
};
in {
devShell = pkgs.mkShell {
name = "anytype-publish-server";
nativeBuildInputs = [
pkgs.go_1_24
pkgs.gox
pkgs.protobuf3_21
pkgs.pkg-config
pkgs.pre-commit
# todo: govvv, not packaged
];
};
});
}
65 changes: 58 additions & 7 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (g *gateway) handlePage(ctx context.Context, w http.ResponseWriter, identit
func (g *gateway) cacheGet(ctx context.Context, key cacheId) (res *pageObject, err error) {
var results = make([]*redis.StringCmd, 3)
_, err = g.redisClient.Pipelined(ctx, func(pipe redis.Pipeliner) error {
redisKey := "{" + string(key) + "}"
redisKey := string(key)
results[0] = pipe.GetEx(ctx, redisKey+":rver", time.Hour)
results[1] = pipe.GetEx(ctx, redisKey+":notfound", time.Hour)
results[2] = pipe.GetEx(ctx, redisKey+":body", time.Hour)
Expand Down Expand Up @@ -208,7 +208,7 @@ func (g *gateway) cacheSet(ctx context.Context, key cacheId, data *pageObject) (
if data.IsNotFound {
isNotFound = "1"
}
redisKey := "{" + string(key) + "}"
redisKey := string(key)
pipe.SetEx(ctx, redisKey+":rver", data.RenderVer, time.Hour)
pipe.SetEx(ctx, redisKey+":notfound", isNotFound, time.Hour)

Expand Down Expand Up @@ -280,6 +280,12 @@ func (g *gateway) renderPage(ctx context.Context, id cacheId) (*pageObject, erro
return nil, err
}

linkObjectIds := rend.GetLinkObjectIds()
objectIdToUrl, err := g.getObjectIdToUrl(ctx, linkObjectIds)
if err == nil {
rend.SetUrlRewriteMap(objectIdToUrl)
}

var buf = bytes.NewBuffer(make([]byte, 0, 5*1024))
if err = rend.Render(buf); err != nil {
return nil, err
Expand All @@ -290,10 +296,52 @@ func (g *gateway) renderPage(ctx context.Context, id cacheId) (*pageObject, erro
}, nil
}

func (g *gateway) invalidateCache(identity, uri string) {
withName := "{" + string(newCacheId(identity, uri, true)) + "}"
withoutName := "{" + string(newCacheId(identity, uri, false)) + "}"
for _, key := range []string{withName, withoutName} {
func (g *gateway) getObjectIdToUrl(ctx context.Context, linkObjectIds []string) (map[string]string, error) {
var objectIdToUrl map[string]string
if len(linkObjectIds) > 0 {
publishes, err := g.publish.GetPublishesByObjectIds(ctx, linkObjectIds)
if err != nil {
return nil, err
}
objectIdToUrl = make(map[string]string, len(publishes))
for _, publish := range publishes {
var url string
anyname, err := g.nameService.ResolveIdentity(ctx, publish.Identity)
// TODO: move to config
if err != nil {
url = fmt.Sprintf("%s/%s/%s", g.config.SelfURL, publish.Identity, publish.Uri)
} else {
url = fmt.Sprintf("https://%s.org/%s", anyname, publish.Uri)
}
objectIdToUrl[publish.ObjectId] = url
}
}
return objectIdToUrl, nil

}
func (g *gateway) invalidateCache(identity, uri string, backlinks []string) {
var keysToDel []string

publishedObjects, err := g.publish.GetPublishesByObjectIds(context.Background(), backlinks)
if err != nil {
keysToDel = make([]string, 2)
log.Error("failed to GetPublishesByObjectIds:", zap.Error(err))
} else {
keysToDel = make([]string, len(backlinks)+2)
for _, publishedObject := range publishedObjects {
identity := publishedObject.Identity
uri := publishedObject.Uri
withName := string(newCacheId(identity, uri, true))
withoutName := string(newCacheId(identity, uri, false))
keysToDel = append(keysToDel, withName, withoutName)
}
}

withName := string(newCacheId(identity, uri, true))
withoutName := string(newCacheId(identity, uri, false))
keysToDel = append(keysToDel, withName, withoutName)

for _, key := range keysToDel {
err := g.redisClient.Del(
context.Background(),
key+":rver",
Expand All @@ -316,7 +364,9 @@ var cacheIdSep = string([]byte{0})

func newCacheId(identity, uri string, withName bool) cacheId {
var res strings.Builder
res.WriteString("{")
res.WriteString(identity)
res.WriteString("}")
res.WriteString(cacheIdSep)
res.WriteString(uri)
res.WriteString(cacheIdSep)
Expand All @@ -331,7 +381,8 @@ func newCacheId(identity, uri string, withName bool) cacheId {
type cacheId string

func (c cacheId) Identity() string {
return c.getElement(1)
id := c.getElement(1)
return id[1 : len(id)-1]
}

func (c cacheId) Uri() string {
Expand Down
2 changes: 1 addition & 1 deletion gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ func Test_cacheId_getElement(t *testing.T) {
assert.Equal(t, "identity", id.Identity())
assert.Equal(t, "uri/a/b", id.Uri())
assert.True(t, id.WithName())
assert.Equal(t, "identity/uri/a/b/1", id.String())
assert.Equal(t, "{identity}/uri/a/b/1", id.String())
}
1 change: 1 addition & 0 deletions gateway/gatewayconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Config struct {
Addr string `yaml:"addr"`
Domain string `yaml:"domain"`
StaticFilesURL string `yaml:"staticFilesUrl"`
SelfURL string `yaml:"selfUrl"`
PublishFilesURL string `yaml:"publishFilesUrl"`
ServeStatic bool `yaml:"serveStatic"`
ServePublish bool `yaml:"servePublish"`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.24.0
require (
github.com/ahmetb/govvv v0.3.0
github.com/anyproto/any-sync v0.9.2
github.com/anyproto/anytype-publish-renderer v0.3.22
github.com/anyproto/anytype-publish-renderer v0.4.0-alpha
github.com/anyproto/anytype-publish-server/publishclient v0.0.0-20250716122732-cdcfe3a126bb
github.com/aws/aws-sdk-go-v2 v1.36.5
github.com/aws/aws-sdk-go-v2/config v1.29.14
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/anyproto/any-sync v0.9.2 h1:nqKatxhHVnnKYk05dEYTaKbW2+oiMvZxpYvnZtwHc
github.com/anyproto/any-sync v0.9.2/go.mod h1:egr+ItXzDs4awlDbNA8UGxWJZWiBnuBA8cUYazAkERk=
github.com/anyproto/anytype-heart v0.42.0-rc30 h1:cCDzDmbPxgtuPc5qzTiz2QRZ/UCIfBoeRj/Q55dOgD4=
github.com/anyproto/anytype-heart v0.42.0-rc30/go.mod h1:dlMltJFcvGrJUQssEhV3Ffx3MndWny+aYFlN/j9RgPU=
github.com/anyproto/anytype-publish-renderer v0.3.22 h1:fPY8OFjg7JoCJvkQfD3vAFQRlJX92UtAp/hJhJm+EdI=
github.com/anyproto/anytype-publish-renderer v0.3.22/go.mod h1:Gk8WeVz7ZiLkktvzAmqog6uRt9ztovAH2q9PwDAkwj8=
github.com/anyproto/anytype-publish-renderer v0.4.0-alpha h1:Nbe3RrAhJ1WLWJbMKK9SfKb6gXX3MGNKxMhMajMwZks=
github.com/anyproto/anytype-publish-renderer v0.4.0-alpha/go.mod h1:Gk8WeVz7ZiLkktvzAmqog6uRt9ztovAH2q9PwDAkwj8=
github.com/anyproto/go-chash v0.1.0 h1:I9meTPjXFRfXZHRJzjOHC/XF7Q5vzysKkiT/grsogXY=
github.com/anyproto/go-chash v0.1.0/go.mod h1:0UjNQi3PDazP0fINpFYu6VKhuna+W/V+1vpXHAfNgLY=
github.com/anyproto/go-gelf v0.0.0-20210418191311-774bd5b016e7 h1:SyEu5uxZ5nKHEJ6TPKQqjM+T00SYi0MW1VaLzqZtZ9E=
Expand Down
2 changes: 1 addition & 1 deletion publish/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (r rpcHandler) Publish(ctx context.Context, req *publishapi.PublishRequest)
)
}()

uploadUrl, err := r.s.Publish(ctx, domain.Object{SpaceId: req.SpaceId, ObjectId: req.ObjectId, Uri: req.Uri}, req.Version)
uploadUrl, err := r.s.Publish(ctx, domain.Object{SpaceId: req.SpaceId, ObjectId: req.ObjectId, Uri: req.Uri}, req.Version, req.Backlinks)
if err != nil {
return nil, err
}
Expand Down
33 changes: 33 additions & 0 deletions publish/publishrepo/publishrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type PublishRepo interface {
DeletePublish(ctx context.Context, id primitive.ObjectID) (err error)
DeleteOutdatedPublishes(ctx context.Context, before time.Time) (deletedCount int, err error)
DeleteOutdatedObjects(ctx context.Context, before time.Time) (deletedCount int, err error)
GetPublishesByObjectIds(ctx context.Context, objectIds []string) (publishes []domain.ObjectWithPublish, err error)
app.ComponentRunnable
}

Expand Down Expand Up @@ -377,6 +378,38 @@ func (p *publishRepo) DeleteOutdatedObjects(ctx context.Context, before time.Tim
return int(res.DeletedCount), nil
}

func (p *publishRepo) GetPublishesByObjectIds(ctx context.Context, objectIds []string) ([]domain.ObjectWithPublish, error) {
filter := bson.D{
{"objectId", bson.D{
{"$in", objectIds},
}},
}

cur, err := p.objectsColl.Find(ctx, filter)
if err != nil {
return nil, err
}
defer func() {
_ = cur.Close(ctx)
}()
var publishes []domain.ObjectWithPublish
for cur.Next(ctx) {
var publish domain.ObjectWithPublish
if err = cur.Decode(&publish.Object); err != nil {
return nil, err
}
if publish.ActivePublishId != nil {
_ = p.publishColl.FindOne(ctx, bson.D{{"_id", *publish.ActivePublishId}}).Decode(&publish.Publish)
}
if publish.Publish.Status == domain.PublishStatusPublished {
publishes = append(publishes, publish)
}

}
return publishes, nil

}

func (p *publishRepo) Close(ctx context.Context) (err error) {
return
}
27 changes: 27 additions & 0 deletions publish/publishrepo/publishrepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@ func TestPublishRepo_ObjectPublishStatus(t *testing.T) {
})
}

func TestPublishRepo_GetPublishes(t *testing.T) {
t.Run("publishes by objectid", func(t *testing.T) {
fx := newFixture(t)
obj := newTestObj()
publishObj, _, err := fx.ObjectCreate(ctx, obj, "v1")
require.NoError(t, err)
uploadKey := publishObj.Publish.UploadKey
publish, err := fx.GetPublish(ctx, publishObj.Publish.Id)
require.NoError(t, err)
assert.Equal(t, publish.Publish.UploadKey, uploadKey)
publish.Publish.Size = 123
publish.Publish.Status = domain.PublishStatusPublished
require.NoError(t, fx.FinalizePublish(ctx, publish))
publishObj, err = fx.ObjectPublishStatus(ctx, obj)
require.NoError(t, err)
require.NotNil(t, publishObj.Publish)
assert.Equal(t, domain.PublishStatusPublished, publishObj.Publish.Status)
assert.Equal(t, int64(123), publishObj.Publish.Size)

publishes, err := fx.GetPublishesByObjectIds(ctx, []string{obj.ObjectId})
require.NoError(t, err)
require.Len(t, publishes, 1)
assert.Equal(t, "o1", publishes[0].ObjectId)
assert.Equal(t, domain.PublishStatusPublished, publishes[0].Publish.Status)
})
}

func TestPublishRepo_IterateReadyToDeleteIds(t *testing.T) {
fx := newFixture(t)
docs := []any{
Expand Down
Loading