plakken/db.go

50 lines
893 B
Go
Raw Normal View History

2023-10-02 20:33:13 +02:00
package main
import (
"context"
"log"
2023-10-02 20:33:13 +02:00
"time"
"github.com/redis/go-redis/v9"
2023-10-02 20:33:13 +02:00
)
var ctx = context.Background()
func ConnectDB() *redis.Client {
localDb := redis.NewClient(&redis.Options{
2023-10-02 20:33:13 +02:00
Addr: currentConfig.redisAddr,
Username: currentConfig.redisUser,
Password: currentConfig.redisPassword,
DB: currentConfig.redisDB,
})
return localDb
2023-10-02 20:33:13 +02:00
}
func insertPaste(key string, content string, secret string, ttl time.Duration) {
2023-10-02 20:33:13 +02:00
type dbSchema struct {
content string
secret string
}
hash := dbSchema{
content: content,
secret: secret,
}
err := db.HSet(ctx, key, "content", hash.content)
if err != nil {
log.Println(err)
}
err = db.HSet(ctx, key, "secret", hash.secret)
2023-10-02 20:33:13 +02:00
if ttl > -1 {
2023-12-28 00:02:50 +01:00
db.Expire(ctx, key, ttl)
2023-10-02 20:33:13 +02:00
}
}
func getContent(key string) string {
return db.HGet(ctx, key, "content").Val()
}
func DeleteContent(key string) {
db.Del(ctx, key)
}