add recorder
This commit is contained in:
59
recorder/file.go
Normal file
59
recorder/file.go
Normal file
@ -0,0 +1,59 @@
|
||||
package recorder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/go-gost/core/recorder"
|
||||
)
|
||||
|
||||
type fileRecorderOptions struct {
|
||||
sep string
|
||||
}
|
||||
|
||||
type FileRecorderOption func(opts *fileRecorderOptions)
|
||||
|
||||
func SepRecorderOption(sep string) FileRecorderOption {
|
||||
return func(opts *fileRecorderOptions) {
|
||||
opts.sep = sep
|
||||
}
|
||||
}
|
||||
|
||||
type fileRecorder struct {
|
||||
filename string
|
||||
sep string
|
||||
}
|
||||
|
||||
// FileRecorder records data to file.
|
||||
func FileRecorder(filename string, opts ...FileRecorderOption) recorder.Recorder {
|
||||
var options fileRecorderOptions
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &fileRecorder{
|
||||
filename: filename,
|
||||
sep: options.sep,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *fileRecorder) Record(ctx context.Context, b []byte) error {
|
||||
f, err := os.OpenFile(r.filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err = f.Write(b); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.sep != "" {
|
||||
_, err := f.WriteString(r.sep)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *fileRecorder) Close() error {
|
||||
return nil
|
||||
}
|
101
recorder/redis.go
Normal file
101
recorder/redis.go
Normal file
@ -0,0 +1,101 @@
|
||||
package recorder
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-gost/core/recorder"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type redisRecorderOptions struct {
|
||||
db int
|
||||
password string
|
||||
key string
|
||||
}
|
||||
type RedisRecorderOption func(opts *redisRecorderOptions)
|
||||
|
||||
func DBRedisRecorderOption(db int) RedisRecorderOption {
|
||||
return func(opts *redisRecorderOptions) {
|
||||
opts.db = db
|
||||
}
|
||||
}
|
||||
|
||||
func PasswordRedisRecorderOption(password string) RedisRecorderOption {
|
||||
return func(opts *redisRecorderOptions) {
|
||||
opts.password = password
|
||||
}
|
||||
}
|
||||
|
||||
func KeyRedisRecorderOption(key string) RedisRecorderOption {
|
||||
return func(opts *redisRecorderOptions) {
|
||||
opts.key = key
|
||||
}
|
||||
}
|
||||
|
||||
type redisSetRecorder struct {
|
||||
client *redis.Client
|
||||
key string
|
||||
}
|
||||
|
||||
// RedisSetRecorder records data to a redis set.
|
||||
func RedisSetRecorder(addr string, opts ...RedisRecorderOption) recorder.Recorder {
|
||||
var options redisRecorderOptions
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &redisSetRecorder{
|
||||
client: redis.NewClient(&redis.Options{
|
||||
Addr: addr,
|
||||
Password: options.password,
|
||||
DB: options.db,
|
||||
}),
|
||||
key: options.key,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *redisSetRecorder) Record(ctx context.Context, b []byte) error {
|
||||
if r.key == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return r.client.SAdd(ctx, r.key, b).Err()
|
||||
}
|
||||
|
||||
func (r *redisSetRecorder) Close() error {
|
||||
return r.client.Close()
|
||||
}
|
||||
|
||||
type redisListRecorder struct {
|
||||
client *redis.Client
|
||||
key string
|
||||
}
|
||||
|
||||
// RedisListRecorder records data to a redis list.
|
||||
func RedisListRecorder(addr string, opts ...RedisRecorderOption) recorder.Recorder {
|
||||
var options redisRecorderOptions
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &redisListRecorder{
|
||||
client: redis.NewClient(&redis.Options{
|
||||
Addr: addr,
|
||||
Password: options.password,
|
||||
DB: options.db,
|
||||
}),
|
||||
key: options.key,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *redisListRecorder) Record(ctx context.Context, b []byte) error {
|
||||
if r.key == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return r.client.LPush(ctx, r.key, b).Err()
|
||||
}
|
||||
|
||||
func (r *redisListRecorder) Close() error {
|
||||
return r.client.Close()
|
||||
}
|
Reference in New Issue
Block a user