feat: redis configuration & client

This commit is contained in:
2025-06-07 01:32:22 +02:00
parent 6666b20464
commit 8902f4d187
4 changed files with 46 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import (
"gitea.local/admin/hspguard/internal/admin"
"gitea.local/admin/hspguard/internal/auth"
"gitea.local/admin/hspguard/internal/cache"
"gitea.local/admin/hspguard/internal/config"
"gitea.local/admin/hspguard/internal/oauth"
"gitea.local/admin/hspguard/internal/repository"
@ -21,14 +22,16 @@ type APIServer struct {
addr string
repo *repository.Queries
storage *storage.FileStorage
cache *cache.Client
cfg *config.AppConfig
}
func NewAPIServer(addr string, db *repository.Queries, minio *storage.FileStorage, cfg *config.AppConfig) *APIServer {
func NewAPIServer(addr string, db *repository.Queries, minio *storage.FileStorage, cache *cache.Client, cfg *config.AppConfig) *APIServer {
return &APIServer{
addr: addr,
repo: db,
storage: minio,
cache: cache,
cfg: cfg,
}
}
@ -47,7 +50,7 @@ func (s *APIServer) Run() error {
userHandler := user.NewUserHandler(s.repo, s.storage, s.cfg)
userHandler.RegisterRoutes(r)
authHandler := auth.NewAuthHandler(s.repo, s.cfg)
authHandler := auth.NewAuthHandler(s.repo, s.cache, s.cfg)
authHandler.RegisterRoutes(r)
oauthHandler.RegisterRoutes(r)

View File

@ -7,6 +7,7 @@ import (
"os"
"gitea.local/admin/hspguard/cmd/hspguard/api"
"gitea.local/admin/hspguard/internal/cache"
"gitea.local/admin/hspguard/internal/config"
"gitea.local/admin/hspguard/internal/repository"
"gitea.local/admin/hspguard/internal/storage"
@ -41,9 +42,11 @@ func main() {
fStorage := storage.New(&cfg)
cache := cache.NewClient(&cfg)
user.EnsureAdminUser(ctx, &cfg, repo)
server := api.NewAPIServer(fmt.Sprintf("%s:%s", cfg.Host, cfg.Port), repo, fStorage, &cfg)
server := api.NewAPIServer(fmt.Sprintf("%s:%s", cfg.Host, cfg.Port), repo, fStorage, cache, &cfg)
if err := server.Run(); err != nil {
log.Fatalln("ERR: Failed to start server:", err)
}

36
internal/cache/mod.go vendored Normal file
View File

@ -0,0 +1,36 @@
package cache
import (
"log"
"time"
"gitea.local/admin/hspguard/internal/config"
"github.com/redis/go-redis/v9"
"golang.org/x/net/context"
)
type Client struct {
rClient *redis.Client
}
func NewClient(cfg *config.AppConfig) *Client {
opts, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
log.Fatalln("ERR: Failed to get redis options:", err)
return nil
}
client := redis.NewClient(opts)
return &Client{
rClient: client,
}
}
func (c *Client) Set(ctx context.Context, key string, value any, expiration time.Duration) *redis.StatusCmd {
return c.rClient.Set(ctx, key, value, expiration)
}
func (c *Client) Get(ctx context.Context, key string) *redis.StringCmd {
return c.rClient.Get(ctx, key)
}

View File

@ -14,6 +14,7 @@ type AppConfig struct {
Host string `env:"GUARD_HOST" default:"127.0.0.1"`
Uri string `env:"GUARD_URI" default:"http://127.0.0.1:3001"`
DatabaseURL string `env:"GUARD_DB_URL" required:"true"`
RedisURL string `env:"GUARD_REDIS_URL" default:"redis://localhost:6379/0"`
Admin AdminConfig
Jwt JwtConfig
Minio MinioConfig