89 lines
1.5 KiB
Go
89 lines
1.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"git.uwushka.cc/nzx056/nzx056/nzx_tmpmail/loggingshit"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var rdb *redis.Client
|
|
var ctx = context.Background()
|
|
|
|
type Mail struct {
|
|
From string
|
|
To string
|
|
RawData string
|
|
}
|
|
|
|
type MailClient struct {
|
|
Name string
|
|
Mail []Mail
|
|
}
|
|
|
|
func GetMailClient(ctx context.Context, id string) (MailClient, error) {
|
|
k := "mailclient:" + id + ":mails"
|
|
it, err := rdb.LRange(ctx, k, 0, -1).Result()
|
|
if err != nil {
|
|
return MailClient{}, err
|
|
}
|
|
|
|
m := MailClient{
|
|
Name: id,
|
|
Mail: make([]Mail, 0, len(it)),
|
|
}
|
|
|
|
for _, i := range it {
|
|
var mail Mail
|
|
if err := json.Unmarshal([]byte(i), &mail); err != nil {
|
|
return MailClient{}, err
|
|
}
|
|
|
|
m.Mail = append(m.Mail, mail)
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func NewMail(ctx context.Context, rcp string, from string, rawdata string) error {
|
|
m := Mail{
|
|
From: from,
|
|
To: rcp,
|
|
RawData: rawdata,
|
|
}
|
|
|
|
b, err := json.Marshal(m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
k := "maiclient:" + rcp + ":mails"
|
|
pipe := rdb.TxPipeline()
|
|
pipe.RPush(ctx, k, b)
|
|
pipe.Expire(ctx, k, time.Minute*10)
|
|
|
|
_, err = pipe.Exec(ctx)
|
|
|
|
return err
|
|
}
|
|
|
|
func RedisInit() {
|
|
rdb = redis.NewClient(&redis.Options{
|
|
Addr: "localhost:6379",
|
|
Password: "",
|
|
DB: 0,
|
|
})
|
|
|
|
if err := rdb.Ping(ctx).Err(); err != nil {
|
|
loggingshit.Log("redis failed on rdb.Ping() with error %v", 1, err)
|
|
}
|
|
|
|
loggingshit.Log("redis init", 1)
|
|
}
|
|
|
|
func Client() *redis.Client {
|
|
return rdb
|
|
}
|