mirror of
https://github.com/psviderski/uncloud.git
synced 2026-10-06 13:18:58 +00:00
feat(caddystorage): init Caddy storage module backed by an Uncloud cluster
This commit is contained in:
1 parent
992b819f5b
commit
9a4098694c
3 files changed
+461
No files matched your search
@@ -0,0 +1,84 @@
|
||||
package caddystorage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
"github.com/psviderski/uncloud/api/pb"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// Store writes a value to the cluster store.
|
||||
func (s *Storage) Store(ctx context.Context, key string, value []byte) error {
|
||||
if _, err := s.client.CaddyStorage.Store(ctx, &pb.StoreCaddyStorageRequest{Key: key, Value: value}); err != nil {
|
||||
return storageError("store", key, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load reads a value from the cluster store.
|
||||
func (s *Storage) Load(ctx context.Context, key string) ([]byte, error) {
|
||||
resp, err := s.client.CaddyStorage.Load(ctx, &pb.LoadCaddyStorageRequest{Key: key})
|
||||
if err != nil {
|
||||
return nil, storageError("load", key, err)
|
||||
}
|
||||
return resp.Value, nil
|
||||
}
|
||||
|
||||
// Delete removes a key and its descendants from the cluster store.
|
||||
func (s *Storage) Delete(ctx context.Context, key string) error {
|
||||
if _, err := s.client.CaddyStorage.Delete(ctx, &pb.DeleteCaddyStorageRequest{Key: key}); err != nil {
|
||||
return storageError("delete", key, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Exists reports whether a key exists in the cluster store.
|
||||
func (s *Storage) Exists(ctx context.Context, key string) bool {
|
||||
_, err := s.Stat(ctx, key)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// List returns keys under prefix from the cluster store.
|
||||
func (s *Storage) List(ctx context.Context, prefix string, recursive bool) ([]string, error) {
|
||||
resp, err := s.client.CaddyStorage.List(ctx, &pb.ListCaddyStorageRequest{
|
||||
Prefix: prefix,
|
||||
Recursive: recursive,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, storageError("list", prefix, err)
|
||||
}
|
||||
return resp.Keys, nil
|
||||
}
|
||||
|
||||
// Stat returns information about a key in the cluster store.
|
||||
func (s *Storage) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
|
||||
resp, err := s.client.CaddyStorage.Stat(ctx, &pb.StatCaddyStorageRequest{Key: key})
|
||||
if err != nil {
|
||||
return certmagic.KeyInfo{}, storageError("stat", key, err)
|
||||
}
|
||||
|
||||
info := certmagic.KeyInfo{
|
||||
Key: resp.Key,
|
||||
Size: resp.Size,
|
||||
IsTerminal: resp.IsTerminal,
|
||||
}
|
||||
if resp.UpdatedAt != nil {
|
||||
if err := resp.UpdatedAt.CheckValid(); err != nil {
|
||||
return certmagic.KeyInfo{}, storageError("stat", key, fmt.Errorf("invalid updated_at timestamp: %w", err))
|
||||
}
|
||||
info.Modified = resp.UpdatedAt.AsTime()
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func storageError(operation, key string, err error) error {
|
||||
if status.Code(err) == codes.NotFound {
|
||||
err = fs.ErrNotExist
|
||||
}
|
||||
// TODO: figure out what the appropriate way of calling the storage in caddy logs is, e.g. include uncloud or redundant if logger includes the name?
|
||||
return fmt.Errorf("%s Caddy storage key '%s': %w", operation, key, err)
|
||||
}
|
||||
Reference in new issue
Block a user