2023-10-02 20:33:13 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/redis/go-redis/v9"
|
2023-10-20 03:30:19 +02:00
|
|
|
"log"
|
2023-10-02 20:33:13 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ctx = context.Background()
|
|
|
|
|
2023-11-26 00:00:53 +01:00
|
|
|
func ConnectDB() *redis.Client {
|
2023-10-20 03:30:19 +02:00
|
|
|
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,
|
|
|
|
})
|
2023-10-20 03:30:19 +02:00
|
|
|
return localDb
|
2023-10-02 20:33:13 +02:00
|
|
|
}
|
|
|
|
|
2023-10-03 20:41:03 +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,
|
|
|
|
}
|
2023-10-20 03:30:19 +02:00
|
|
|
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-10-20 03:30:19 +02:00
|
|
|
db.Do(ctx, key, ttl)
|
2023-10-02 20:33:13 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-04 00:10:29 +02:00
|
|
|
|
|
|
|
func getContent(key string) string {
|
2023-11-26 00:00:53 +01:00
|
|
|
return db.HGet(ctx, key, "content").Val()
|
2023-10-20 03:30:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteContent(key string) {
|
|
|
|
err := db.Del(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2023-10-04 00:10:29 +02:00
|
|
|
}
|