plakken/db.go

53 lines
954 B
Go
Raw Normal View History

2023-10-02 20:33:13 +02:00
package main
import (
"context"
"github.com/redis/go-redis/v9"
"log"
2023-10-02 20:33:13 +02:00
"time"
)
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 {
db.Do(ctx, key, ttl)
2023-10-02 20:33:13 +02:00
}
}
func getContent(key string) string {
content := db.HGet(ctx, key, "content").Val()
return content
}
func deleteContent(key string) {
err := db.Del(ctx, key)
if err != nil {
log.Println(err)
}
}