feat(lib): valkey backed store

Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
Xe Iaso
2025-06-05 20:50:59 -04:00
parent c638653172
commit 21ee6e3406
7 changed files with 135 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ import (
"github.com/TecharoHQ/anubis/internal"
"github.com/TecharoHQ/anubis/internal/dnsbl"
"github.com/TecharoHQ/anubis/internal/ogtags"
"github.com/TecharoHQ/anubis/internal/store"
"github.com/TecharoHQ/anubis/lib/challenge"
"github.com/TecharoHQ/anubis/lib/policy"
"github.com/TecharoHQ/anubis/lib/policy/config"
@@ -70,6 +71,7 @@ type Server struct {
priv ed25519.PrivateKey
pub ed25519.PublicKey
opts Options
store store.Impl
}
func (s *Server) challengeFor(r *http.Request, difficulty int) string {

View File

@@ -1,6 +1,7 @@
package lib
import (
"context"
"crypto/ed25519"
"crypto/rand"
"errors"
@@ -18,10 +19,12 @@ import (
"github.com/TecharoHQ/anubis/internal"
"github.com/TecharoHQ/anubis/internal/dnsbl"
"github.com/TecharoHQ/anubis/internal/ogtags"
"github.com/TecharoHQ/anubis/internal/store/valkey"
"github.com/TecharoHQ/anubis/lib/challenge"
"github.com/TecharoHQ/anubis/lib/policy"
"github.com/TecharoHQ/anubis/web"
"github.com/TecharoHQ/anubis/xess"
"github.com/redis/go-redis/v9"
)
type Options struct {
@@ -40,6 +43,7 @@ type Options struct {
OGPassthrough bool
CookiePartitioned bool
ServeRobotsTXT bool
ValkeyURL string
}
func LoadPoliciesOrDefault(fname string, defaultDifficulty int) (*policy.ParsedConfig, error) {
@@ -111,6 +115,23 @@ func New(opts Options) (*Server, error) {
cookieName: cookieName,
}
if opts.ValkeyURL != "" {
vkOpts, err := redis.ParseURL(opts.ValkeyURL)
if err != nil {
return nil, fmt.Errorf("can't parse valkey URL: %q: %w", opts.ValkeyURL, err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
cli := redis.NewClient(vkOpts)
if _, err := cli.Ping(ctx).Result(); err != nil {
return nil, fmt.Errorf("can't ping valkey: %w", err)
}
result.store = valkey.New(cli)
}
mux := http.NewServeMux()
xess.Mount(mux)