更新库pkg
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package aycache
|
||||
|
||||
import (
|
||||
"github.com/ayflying/utility_go/package/aycache/drive"
|
||||
drive2 "github.com/ayflying/utility_go/pgk/aycache/drive"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
)
|
||||
|
||||
type Mod struct {
|
||||
client *gcache.Cache
|
||||
}
|
||||
|
||||
//func NewV1(_name ...string) *cache.Mod {
|
||||
// return pgk.Cache
|
||||
//}
|
||||
|
||||
func New(_name ...string) gcache.Adapter {
|
||||
|
||||
var cacheAdapterObj gcache.Adapter
|
||||
var name = "cache"
|
||||
if len(_name) > 0 {
|
||||
name = _name[0]
|
||||
}
|
||||
switch name {
|
||||
case "cache":
|
||||
cacheAdapterObj = drive2.NewAdapterMemory()
|
||||
case "redis":
|
||||
cacheAdapterObj = drive2.NewAdapterRedis()
|
||||
case "file":
|
||||
cacheAdapterObj = drive2.NewAdapterFile("runtime/cache")
|
||||
case "es":
|
||||
cacheAdapterObj = drive.NewAdapterElasticsearch([]string{"http://127.0.0.1:9200"})
|
||||
}
|
||||
|
||||
//var client = gcache.New()
|
||||
//client.SetAdapter(cacheAdapterObj)
|
||||
return cacheAdapterObj
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
package drive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AdapterElasticsearch struct {
|
||||
//FilePath string
|
||||
Addresses []string
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) SetIfNotExistFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Get(ctx context.Context, key interface{}) (*gvar.Var, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) GetOrSetFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) GetOrSetFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Contains(ctx context.Context, key interface{}) (bool, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Size(ctx context.Context) (size int, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Data(ctx context.Context) (data map[interface{}]interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Keys(ctx context.Context) (keys []interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Values(ctx context.Context) (values []interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Clear(ctx context.Context) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterElasticsearch) Close(ctx context.Context) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func NewAdapterElasticsearch(addresses []string) gcache.Adapter {
|
||||
return &AdapterElasticsearch{
|
||||
Addresses: addresses,
|
||||
}
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
package drive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AdapterFile struct {
|
||||
FilePath string
|
||||
}
|
||||
|
||||
func (a AdapterFile) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error {
|
||||
//defer a.handleLruKey(ctx, key)
|
||||
//expireTime := a.getInternalExpire(duration)
|
||||
//a.data.Set(key, memoryDataItem{
|
||||
// a: value,
|
||||
// a: expireTime,
|
||||
//})
|
||||
//c.eventList.PushBack(&adapterMemoryEvent{
|
||||
// k: key,
|
||||
// e: expireTime,
|
||||
//})
|
||||
|
||||
arr := strings.Split(":", gconv.String(key))
|
||||
fileName := path.Join(arr...)
|
||||
return gfile.PutBytes(fileName, gconv.Bytes(value))
|
||||
}
|
||||
|
||||
func (a AdapterFile) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) SetIfNotExistFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Get(ctx context.Context, key interface{}) (*gvar.Var, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) GetOrSetFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) GetOrSetFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Contains(ctx context.Context, key interface{}) (bool, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Size(ctx context.Context) (size int, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Data(ctx context.Context) (data map[interface{}]interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Keys(ctx context.Context) (keys []interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Values(ctx context.Context) (values []interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Clear(ctx context.Context) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Close(ctx context.Context) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func NewAdapterFile(filePath string) gcache.Adapter {
|
||||
return &AdapterFile{
|
||||
FilePath: filePath,
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package drive
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
)
|
||||
|
||||
var adapterMemoryClient = gcache.New()
|
||||
|
||||
// NewAdapterMemory 创建并返回一个新的内存缓存对象。
|
||||
func NewAdapterMemory() gcache.Adapter {
|
||||
//if adapterMemoryClient == nil {
|
||||
// adapterMemoryClient = gcache.New()
|
||||
//}
|
||||
return adapterMemoryClient
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
package drive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AdapterRedis is the gcache adapter implements using Redis server.
|
||||
type AdapterMemcached struct {
|
||||
//redis *gredis.Redis
|
||||
//client
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) SetIfNotExistFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Get(ctx context.Context, key interface{}) (*gvar.Var, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) GetOrSetFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) GetOrSetFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Contains(ctx context.Context, key interface{}) (bool, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Size(ctx context.Context) (size int, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Data(ctx context.Context) (data map[interface{}]interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Keys(ctx context.Context) (keys []interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Values(ctx context.Context) (values []interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Clear(ctx context.Context) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterMemcached) Close(ctx context.Context) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
// NewAdapterRedis creates and returns a new memory cache object.
|
||||
func NewAdapterMemcached(redis *gredis.Redis) gcache.Adapter {
|
||||
return &AdapterMemcached{}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package drive
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
var adapterRedisClient gcache.Adapter
|
||||
var adapterRedisCache = gcache.New()
|
||||
|
||||
func NewAdapterRedis() gcache.Adapter {
|
||||
|
||||
if adapterRedisClient == nil {
|
||||
_cfg, _ := g.Cfg().Get(gctx.New(), "redis.default")
|
||||
var cfg *gredis.Config
|
||||
_cfg.Scan(&cfg)
|
||||
redisObj, _ := gredis.New(cfg)
|
||||
//adapterRedisClient = gcache.NewAdapterRedis(g.Redis("default"))
|
||||
adapterRedisClient = gcache.NewAdapterRedis(redisObj)
|
||||
|
||||
adapterRedisCache.SetAdapter(adapterRedisClient)
|
||||
}
|
||||
return adapterRedisCache
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
package elasticsearch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/elastic/go-elasticsearch/v8"
|
||||
)
|
||||
|
||||
var (
|
||||
es *elasticsearch.TypedClient
|
||||
)
|
||||
|
||||
type elastic struct {
|
||||
client *elasticsearch.TypedClient
|
||||
}
|
||||
|
||||
func New(name ...string) *elastic {
|
||||
// ES 配置
|
||||
cfg := elasticsearch.Config{
|
||||
Addresses: []string{
|
||||
"http://ay.cname.com:9200",
|
||||
},
|
||||
}
|
||||
if es == nil {
|
||||
var err error
|
||||
es, err = elasticsearch.NewTypedClient(cfg)
|
||||
if err != nil {
|
||||
fmt.Printf("elasticsearch.NewTypedClient failed, err:%v\n", err)
|
||||
return &elastic{}
|
||||
}
|
||||
}
|
||||
return &elastic{
|
||||
client: es,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// createIndex 创建索引
|
||||
func (s *elastic) CreateIndex(name string) {
|
||||
resp, err := s.client.Indices.
|
||||
Create(name).
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
fmt.Printf("create index failed, err:%v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("index:%#v\n", resp.Index)
|
||||
}
|
||||
|
||||
// indexDocument 索引文档
|
||||
func (s *elastic) IndexDocument(name string, key string, data interface{}) {
|
||||
|
||||
// 添加文档
|
||||
resp, err := s.client.Index(name).
|
||||
Id(key).
|
||||
Document(data).
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
fmt.Printf("indexing document failed, err:%v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("result:%#v\n", resp.Result)
|
||||
}
|
||||
|
||||
// getDocument 获取文档
|
||||
func (s *elastic) GetDocument(name string, id string) (res json.RawMessage) {
|
||||
resp, err := s.client.Get(name, id).
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
fmt.Printf("get document by id failed, err:%v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("fileds:%s\n", resp.Source_)
|
||||
res = resp.Source_
|
||||
return
|
||||
}
|
||||
|
||||
// updateDocument 更新文档
|
||||
func (s *elastic) UpdateDocument(name string, key string, data interface{}) {
|
||||
|
||||
resp, err := s.client.Update(name, key).
|
||||
Doc(data). // 使用结构体变量更新
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
fmt.Printf("update document failed, err:%v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("result:%v\n", resp.Result)
|
||||
}
|
||||
|
||||
// deleteDocument 删除 document
|
||||
func (s *elastic) DeleteDocument(name string, key string) {
|
||||
resp, err := s.client.Delete(name, key).
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
fmt.Printf("delete document failed, err:%v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("result:%v\n", resp.Result)
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package drive
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
type DingTalkMod struct {
|
||||
DingTalkWebHook string
|
||||
}
|
||||
|
||||
func DingTalkLoad(webHook string) *DingTalkMod {
|
||||
return &DingTalkMod{
|
||||
DingTalkWebHook: webHook,
|
||||
}
|
||||
}
|
||||
|
||||
func (m DingTalkMod) Send(value string) {
|
||||
ctx := gctx.New()
|
||||
// 从配置中获取发送者名称
|
||||
name, _ := g.Cfg().Get(ctx, "name")
|
||||
|
||||
// 定义钉钉机器人发送消息的URL,其中access_token为固定值
|
||||
url := m.DingTalkWebHook
|
||||
url += "×tamp=" + gtime.Now().TimestampMilliStr()
|
||||
// 使用goroutine异步发送消息
|
||||
|
||||
var post = g.Map{
|
||||
"msgtype": "text",
|
||||
"text": g.Map{
|
||||
"content": "通知姬 " + name.String() + ":\n" + value + "\n" + gtime.Now().String(),
|
||||
},
|
||||
}
|
||||
|
||||
// 构建发送的消息体,包含消息类型和内容
|
||||
_, err := g.Client().Discovery(nil).ContentJson().Post(ctx, url, post)
|
||||
if err != nil {
|
||||
g.Log().Info(ctx, err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package drive
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"gopkg.in/gomail.v2"
|
||||
)
|
||||
|
||||
type MailMod struct {
|
||||
Host string `json:"host" dc:"邮件服务器地址"`
|
||||
Port int `json:"port" dc:"邮件服务器端口"`
|
||||
User string `json:"user" dc:"邮件服务器用户名"`
|
||||
Pass string `json:"pass" dc:"邮件服务器密码"`
|
||||
From string `json:"from" dc:"邮件发送者"`
|
||||
To string `json:"to" dc:"邮件接收者"`
|
||||
Subject string `json:"subject" dc:"邮件主题"`
|
||||
}
|
||||
|
||||
func MailLoad(Host string, port int, to string, subject string) *MailMod {
|
||||
return &MailMod{
|
||||
Host: Host,
|
||||
Port: port,
|
||||
User: "root",
|
||||
Pass: "root",
|
||||
From: "root",
|
||||
To: to,
|
||||
Subject: subject,
|
||||
}
|
||||
}
|
||||
|
||||
func (m MailMod) Send(value string) {
|
||||
// 创建一个新的消息
|
||||
obj := gomail.NewMessage()
|
||||
// 设置发件人
|
||||
obj.SetHeader("From", m.From)
|
||||
// 设置收件人
|
||||
obj.SetHeader("To", m.To)
|
||||
// 设置邮件主题
|
||||
obj.SetHeader("Subject", m.Subject)
|
||||
// 设置邮件正文
|
||||
obj.SetBody("text/plain", value)
|
||||
|
||||
// 创建 SMTP 拨号器,这里需要提供 SMTP 服务器地址、端口、发件人邮箱和密码
|
||||
d := gomail.NewDialer(m.Host, m.Port, m.User, m.Pass)
|
||||
|
||||
// 发送邮件
|
||||
if err := d.DialAndSend(obj); err != nil {
|
||||
g.Log().Error(gctx.New(), err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package notice
|
||||
|
||||
import (
|
||||
v1 "github.com/ayflying/utility_go/api/pgk/v1"
|
||||
"github.com/ayflying/utility_go/pgk/notice/drive"
|
||||
)
|
||||
|
||||
type MessageV1 interface {
|
||||
Send(value string)
|
||||
}
|
||||
|
||||
func New(typ v1.NoticeType, host string, value ...interface{}) MessageV1 {
|
||||
switch typ {
|
||||
case v1.NoticeType_DINGTALK:
|
||||
return drive.DingTalkLoad(host)
|
||||
case v1.NoticeType_EMAIL:
|
||||
return drive.MailLoad(host, value[0].(int), value[1].(string), value[2].(string))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
12
pgk/pgk.go
12
pgk/pgk.go
@@ -2,29 +2,33 @@ package pgk
|
||||
|
||||
import (
|
||||
v1 "github.com/ayflying/utility_go/api/pgk/v1"
|
||||
"github.com/ayflying/utility_go/pgk/aycache"
|
||||
"github.com/ayflying/utility_go/pgk/notice"
|
||||
"github.com/ayflying/utility_go/pgk/rank"
|
||||
"github.com/ayflying/utility_go/pgk/s3"
|
||||
"github.com/ayflying/utility_go/pkg/aycache"
|
||||
"github.com/ayflying/utility_go/pkg/notice"
|
||||
"github.com/ayflying/utility_go/pkg/rank"
|
||||
"github.com/ayflying/utility_go/pkg/s3"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
)
|
||||
|
||||
var ()
|
||||
|
||||
// 统一调用
|
||||
// Deprecated: 请使用 pkg.Notice() 方法替代。
|
||||
func Notice(typ v1.NoticeType, host string) notice.MessageV1 {
|
||||
return notice.New(typ, host)
|
||||
}
|
||||
|
||||
// 统一调用cache
|
||||
// Deprecated: 请使用 pkg.Cache() 方法替代。
|
||||
func Cache(_name ...string) gcache.Adapter {
|
||||
return aycache.New(_name...)
|
||||
}
|
||||
|
||||
// Deprecated: 请使用 pkg.S3() 方法替代。
|
||||
func S3(_name ...string) *s3.Mod {
|
||||
return s3.New(_name...)
|
||||
}
|
||||
|
||||
// Deprecated: 请使用 pkg.Rank() 方法替代。
|
||||
func Rank() *rank.Mod {
|
||||
return rank.New()
|
||||
}
|
||||
|
||||
@@ -46,6 +46,8 @@ func (s *Mod) Load() {
|
||||
// 返回值:
|
||||
//
|
||||
// *F64CountRank: 返回一个指向新创建的F64CountRank实例的指针
|
||||
|
||||
// Deprecated: pgk.Rnak().CreateF64CountRank("赛季")
|
||||
func (s *Mod) CreateF64CountRank(name string) *F64CountRank {
|
||||
// 初始化F64CountRank实例的name和updateTs字段
|
||||
// name字段用于标识排行榜的名称,格式为"rank:<name>:score"
|
||||
|
||||
289
pgk/s3/s3.go
289
pgk/s3/s3.go
@@ -1,289 +0,0 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/url"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
var (
|
||||
//client *minio.Client
|
||||
ctx = gctx.New()
|
||||
)
|
||||
|
||||
type DataType struct {
|
||||
AccessKey string `json:"access_key"`
|
||||
SecretKey string `json:"secret_key"`
|
||||
Address string `json:"address"`
|
||||
Ssl bool `json:"ssl"`
|
||||
Url string `json:"url"`
|
||||
BucketName string `json:"bucket_name"`
|
||||
BucketNameCdn string `json:"bucket_name_cdn"`
|
||||
}
|
||||
|
||||
type Mod struct {
|
||||
client *minio.Client
|
||||
cfg DataType
|
||||
}
|
||||
|
||||
func New(_name ...string) *Mod {
|
||||
var name string
|
||||
if len(_name) > 0 {
|
||||
name = _name[0]
|
||||
} else {
|
||||
getName, _ := g.Cfg().Get(ctx, "s3.type")
|
||||
name = getName.String()
|
||||
}
|
||||
|
||||
get, err := g.Cfg().Get(ctx, "s3."+name)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
var cfg DataType
|
||||
get.Scan(&cfg)
|
||||
|
||||
// 使用minio-go创建S3客户端
|
||||
obj, err := minio.New(
|
||||
cfg.Address,
|
||||
&minio.Options{
|
||||
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
||||
Secure: cfg.Ssl,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
mod := &Mod{
|
||||
client: obj,
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
return mod
|
||||
}
|
||||
|
||||
//func (s *Mod) Load() {
|
||||
// //导入配置
|
||||
// get, err := g.Cfg().Get(ctx, "s3.type")
|
||||
// cfgType := get.String()
|
||||
// if cfgType == "" {
|
||||
// cfgType = "default"
|
||||
// }
|
||||
//
|
||||
// cfgData, err := g.Cfg().Get(ctx, "s3."+cfgType)
|
||||
// if cfgData.IsEmpty() {
|
||||
// panic("当前配置中未配置s3:" + cfgType)
|
||||
// }
|
||||
//
|
||||
// get, err = g.Cfg().Get(ctx, "s3."+cfgType)
|
||||
// err = get.Scan(&Cfg)
|
||||
//
|
||||
// // 使用minio-go创建S3客户端
|
||||
// obj, err := minio.New(
|
||||
// Cfg.Address,
|
||||
// &minio.Options{
|
||||
// Creds: credentials.NewStaticV4(Cfg.AccessKey, Cfg.SecretKey, ""),
|
||||
// Secure: Cfg.Ssl,
|
||||
// },
|
||||
// )
|
||||
// if err != nil {
|
||||
// log.Fatalln(err)
|
||||
// }
|
||||
//
|
||||
// client = obj
|
||||
//}
|
||||
//
|
||||
//func (s *Mod) S3(name string) {
|
||||
// get, err := g.Cfg().Get(ctx, "s3."+name)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// get.Scan(&Cfg)
|
||||
//
|
||||
// // 使用minio-go创建S3客户端
|
||||
// obj, err := minio.New(
|
||||
// Cfg.Address,
|
||||
// &minio.Options{
|
||||
// Creds: credentials.NewStaticV4(Cfg.AccessKey, Cfg.SecretKey, ""),
|
||||
// Secure: Cfg.Ssl,
|
||||
// },
|
||||
// )
|
||||
// if err != nil {
|
||||
// log.Fatalln(err)
|
||||
// }
|
||||
//
|
||||
// client = obj
|
||||
//
|
||||
//}
|
||||
|
||||
// GetCfg 获取配置
|
||||
func (s *Mod) GetCfg() *DataType {
|
||||
return &s.cfg
|
||||
}
|
||||
|
||||
// GetFileUrl 生成指向S3存储桶中指定文件的预签名URL
|
||||
//
|
||||
// @Description: 生成一个具有有限有效期的预签名URL,可用于访问S3存储桶中的文件。
|
||||
// @receiver s: S3的实例,用于执行S3操作。
|
||||
// @param name: 要获取预签名URL的文件名。
|
||||
// @param bucketName: 文件所在的存储桶名称。
|
||||
// @return presignedURL: 生成的预签名URL,可用于访问文件。
|
||||
// @return err: 在获取预签名URL过程中遇到的任何错误。
|
||||
func (s *Mod) GetFileUrl(name string, bucketName string, _expires ...time.Duration) (presignedURL *url.URL, err error) {
|
||||
// 设置预签名URL的有效期为1小时
|
||||
expires := time.Hour * 1
|
||||
if len(_expires) > 0 {
|
||||
expires = _expires[0]
|
||||
}
|
||||
cacheKey := fmt.Sprintf("s3:%v:%v", name, bucketName)
|
||||
get, _ := gcache.Get(ctx, cacheKey)
|
||||
//g.Dump(get.Vars())
|
||||
if !get.IsEmpty() {
|
||||
err = gconv.Struct(get.Val(), &presignedURL)
|
||||
//presignedURL =
|
||||
return
|
||||
}
|
||||
//expires := time.Duration(604800)
|
||||
// 调用s3().PresignedGetObject方法生成预签名URL
|
||||
presignedURL, err = s.client.PresignedGetObject(ctx, bucketName, name, expires, nil)
|
||||
err = gcache.Set(ctx, cacheKey, presignedURL, expires)
|
||||
return
|
||||
}
|
||||
|
||||
// PutFileUrl 生成一个用于上传文件到指定bucket的预签名URL
|
||||
//
|
||||
// @Description:
|
||||
// @receiver s
|
||||
// @param name 文件名
|
||||
// @param bucketName 存储桶名称
|
||||
// @return presignedURL 预签名的URL,用于上传文件
|
||||
// @return err 错误信息,如果在生成预签名URL时发生错误
|
||||
func (s *Mod) PutFileUrl(name string, bucketName string) (presignedURL *url.URL, err error) {
|
||||
// 设置预签名URL的有效期
|
||||
//expires := time.Now().Add(time.Minute * 30).Unix() // 例如:有效期30分钟
|
||||
//expires2 := time.Duration(expires)
|
||||
expires := time.Minute * 10
|
||||
// 生成预签名URL
|
||||
presignedURL, err = s.client.PresignedPutObject(ctx, bucketName, name, expires)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 获取储存桶列表
|
||||
func (s *Mod) ListBuckets() []minio.BucketInfo {
|
||||
buckets, err := s.client.ListBuckets(ctx)
|
||||
//g.Dump(buckets)
|
||||
if err != nil {
|
||||
//fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
return buckets
|
||||
}
|
||||
|
||||
// PutObject 上传文件到指定的存储桶中。
|
||||
//
|
||||
// @Description: 上传一个文件到指定的存储桶。
|
||||
// @receiver s *Mod: 表示调用此方法的Mod实例。
|
||||
// @param f io.Reader: 文件的读取器,用于读取待上传的文件内容。
|
||||
// @param name string: 待上传文件的名称。
|
||||
// @param bucketName string: 存储桶的名称。
|
||||
// @param _size ...int64: 可选参数,指定上传文件的大小。如果未提供,则默认为-1,表示读取文件直到EOF。
|
||||
// @return res minio.UploadInfo: 上传成功后返回的上传信息。
|
||||
// @return err error: 如果上传过程中出现错误,则返回错误信息。
|
||||
func (s *Mod) PutObject(f io.Reader, name string, bucketName string, _size ...int64) (res minio.UploadInfo, err error) {
|
||||
// 初始化文件大小为-1,表示将读取文件至结束。
|
||||
var size = int64(-1)
|
||||
// 如果提供了文件大小,则使用提供的大小值。
|
||||
if len(_size) > 0 {
|
||||
size = _size[0]
|
||||
}
|
||||
|
||||
// 调用client的PutObject方法上传文件,并设置内容类型为"application/octet-stream"。
|
||||
res, err = s.client.PutObject(ctx, bucketName, name, f, size, minio.PutObjectOptions{ContentType: "application/octet-stream"})
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// RemoveObject 删除文件
|
||||
func (s *Mod) RemoveObject(name string, bucketName string) (err error) {
|
||||
opts := minio.RemoveObjectOptions{
|
||||
//GovernanceBypass: true,
|
||||
//VersionID: "myversionid",
|
||||
}
|
||||
err = s.client.RemoveObject(ctx, bucketName, name, opts)
|
||||
return
|
||||
}
|
||||
|
||||
// ListObjects 文件列表
|
||||
func (s *Mod) ListObjects(bucketName string, prefix string) (res <-chan minio.ObjectInfo, err error) {
|
||||
res = s.client.ListObjects(ctx, bucketName, minio.ListObjectsOptions{
|
||||
Prefix: prefix,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// SetBucketPolicy 设置bucket或对象前缀的访问权限
|
||||
func (s *Mod) SetBucketPolicy(bucketName string, prefix string) (err error) {
|
||||
|
||||
policy := `{"Version": "2012-10-17","Statement": [{"Action": ["s3:GetObject"],"Effect": "Allow","Principal": {"AWS": ["*"]},"Resource": ["arn:aws:s3:::my-bucketname/*"],"Sid": ""}]}`
|
||||
|
||||
err = s.client.SetBucketPolicy(ctx, bucketName, policy)
|
||||
return
|
||||
}
|
||||
|
||||
// GetUrl 获取文件访问地址
|
||||
func (s *Mod) GetUrl(filePath string, defaultFile ...string) (url string) {
|
||||
bucketName := s.cfg.BucketNameCdn
|
||||
get := s.cfg.Url
|
||||
|
||||
//如果没有图片,返回默认的图片地址
|
||||
if filePath == "" && len(defaultFile) > 0 {
|
||||
filePath = defaultFile[0]
|
||||
}
|
||||
|
||||
if s.cfg.Ssl {
|
||||
url = get + filePath
|
||||
} else {
|
||||
url = get + path.Join(bucketName, filePath)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Mod) GetPath(url string) (filePath string) {
|
||||
bucketName := s.cfg.BucketNameCdn
|
||||
get := s.cfg.Url
|
||||
|
||||
return url[len(get+bucketName)+1:]
|
||||
}
|
||||
|
||||
// 复制文件
|
||||
func (s *Mod) CopyObject(bucketName string, dstStr string, srcStr string) (err error) {
|
||||
|
||||
// 原始文件
|
||||
var dst = minio.CopyDestOptions{
|
||||
Bucket: bucketName,
|
||||
Object: dstStr,
|
||||
}
|
||||
|
||||
// 新文件
|
||||
var src = minio.CopySrcOptions{
|
||||
Bucket: bucketName,
|
||||
Object: srcStr,
|
||||
}
|
||||
|
||||
_, err = s.client.CopyObject(ctx, dst, src)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user