上传服务器支持分流s3上传
This commit is contained in:
29
act/act.go
Normal file
29
act/act.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package act
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
type act struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type Sql struct {
|
||||||
|
UID uint64 `gorm:"primaryKey;comment:玩家编号"`
|
||||||
|
ActID int `gorm:"primaryKey;comment:活动编号"`
|
||||||
|
Action string `gorm:"type:text;comment:活动配置"`
|
||||||
|
UpdatedAt *gtime.Time `gorm:"index;comment:更新时间"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *act {
|
||||||
|
|
||||||
|
return &act{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *act) CreateTable(name string) {
|
||||||
|
|
||||||
|
//prefix := g.DB().GetPrefix()
|
||||||
|
//
|
||||||
|
//g.DB()
|
||||||
|
//g.DB().Exec(gctx.New())
|
||||||
|
|
||||||
|
}
|
||||||
13
act/act.sql
Normal file
13
act/act.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
create table shiningu_game_act
|
||||||
|
(
|
||||||
|
uid bigint not null comment '玩家编号',
|
||||||
|
act_id int not null comment '活动编号',
|
||||||
|
action text null comment '活动配置',
|
||||||
|
updated_at datetime null comment '更新时间',
|
||||||
|
primary key (uid, act_id)
|
||||||
|
)
|
||||||
|
comment '玩家活动数据' charset = utf8mb4;
|
||||||
|
|
||||||
|
create index shiningu_game_act_uid_index
|
||||||
|
on shiningu_game_act (uid);
|
||||||
|
|
||||||
19
act/save.go
Normal file
19
act/save.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package act
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 查询表格时候存在
|
||||||
|
func (s *act) TableExists(name string) bool {
|
||||||
|
Prefix := g.DB().GetPrefix()
|
||||||
|
get, err := g.DB().TableFields(gctx.New(), Prefix+name)
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Error(gctx.New(), err)
|
||||||
|
}
|
||||||
|
if get != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"github.com/gogf/gf/v2/os/gcfg"
|
"github.com/gogf/gf/v2/os/gcfg"
|
||||||
"github.com/gogf/gf/v2/os/gcmd"
|
"github.com/gogf/gf/v2/os/gcmd"
|
||||||
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -15,6 +16,7 @@ type serverCfg struct {
|
|||||||
Name string `json:"name" dc:"服务名"`
|
Name string `json:"name" dc:"服务名"`
|
||||||
Address string `json:"address" dc:"服务地址"`
|
Address string `json:"address" dc:"服务地址"`
|
||||||
Prod bool `json:"prod" dc:"是否生产环境"`
|
Prod bool `json:"prod" dc:"是否生产环境"`
|
||||||
|
S3 string `json:"s3" dc:"使用哪个对象储存中转"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateReq struct {
|
type UpdateReq struct {
|
||||||
@@ -22,6 +24,8 @@ type UpdateReq struct {
|
|||||||
FileUrl string `json:"file_url" dc:"文件地址"`
|
FileUrl string `json:"file_url" dc:"文件地址"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var s3Mod *s3.Mod
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Update = gcmd.Command{
|
Update = gcmd.Command{
|
||||||
Name: "update",
|
Name: "update",
|
||||||
@@ -33,6 +37,7 @@ var (
|
|||||||
//加载编辑配置文件
|
//加载编辑配置文件
|
||||||
g.Cfg("hack").GetAdapter().(*gcfg.AdapterFile).SetFileName("hack/config.yaml")
|
g.Cfg("hack").GetAdapter().(*gcfg.AdapterFile).SetFileName("hack/config.yaml")
|
||||||
getFileName, err := g.Cfg("hack").Get(ctx, "gfcli.build.name")
|
getFileName, err := g.Cfg("hack").Get(ctx, "gfcli.build.name")
|
||||||
|
Filename := getFileName.String()
|
||||||
|
|
||||||
var list []*serverCfg
|
var list []*serverCfg
|
||||||
serverList := g.Cfg().MustGet(ctx, "server_list")
|
serverList := g.Cfg().MustGet(ctx, "server_list")
|
||||||
@@ -52,28 +57,9 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
g.Dump("需要更新的服务器", list)
|
g.Dump("需要更新的服务器", list)
|
||||||
|
//获取上传链接
|
||||||
filename := "linux_amd64/shining_u_server"
|
var url = make(map[string]string)
|
||||||
obj, err := os.Open(filename)
|
filename := "linux_amd64/" + Filename
|
||||||
ff, err := obj.Stat()
|
|
||||||
var s3Mod *s3.Mod
|
|
||||||
//切换s3目标
|
|
||||||
updateServerS3Name, _ := g.Config().Get(ctx, "update_server_s3_name")
|
|
||||||
if updateServerS3Name != nil {
|
|
||||||
s3Mod = s3.New(updateServerS3Name.String())
|
|
||||||
} else {
|
|
||||||
s3Mod = s3.New("default")
|
|
||||||
}
|
|
||||||
bucketName := s3Mod.GetCfg().BucketName
|
|
||||||
|
|
||||||
_, err = s3Mod.PutObject(obj, getFileName.String(), bucketName, ff.Size())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//上传当前文件
|
|
||||||
url, err := s3Mod.GetFileUrl(getFileName.String(), bucketName)
|
|
||||||
g.Log().Debugf(ctx, "下载地址:%v", url)
|
|
||||||
|
|
||||||
client := g.Client()
|
client := g.Client()
|
||||||
client.SetTimeout(time.Minute)
|
client.SetTimeout(time.Minute)
|
||||||
@@ -82,14 +68,28 @@ var (
|
|||||||
//循环服务器,推送更新
|
//循环服务器,推送更新
|
||||||
for _, v := range list {
|
for _, v := range list {
|
||||||
address := v.Address
|
address := v.Address
|
||||||
|
if v.S3 == "" {
|
||||||
|
v.S3 = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
//查询当前上传地址是否存在
|
||||||
|
if _, ok := url[v.S3]; !ok {
|
||||||
|
url[v.S3], err = UploadS3(v.S3, filename)
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Error(ctx, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
g.Log().Debugf(ctx, "准备同步服务器:%v,url=%v", v.Name, address+"/callback/update")
|
g.Log().Debugf(ctx, "准备同步服务器:%v,url=%v", v.Name, address+"/callback/update")
|
||||||
get, err := client.Post(ctx, address+"/callback/update", &UpdateReq{
|
get, err := client.Post(ctx, address+"/callback/update", &UpdateReq{
|
||||||
FileUrl: url.String(),
|
FileUrl: url[v.S3],
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
get, err = client.Proxy("http://127.0.0.1:10809").
|
g.Log().Debugf(ctx, "切换代理进行上传:err=%v", err)
|
||||||
|
get, err = client.Proxy("http://192.168.50.114:10808").
|
||||||
Post(ctx, address+"/callback/update", &UpdateReq{
|
Post(ctx, address+"/callback/update", &UpdateReq{
|
||||||
FileUrl: url.String(),
|
FileUrl: url[v.S3],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -103,3 +103,22 @@ var (
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func UploadS3(typ string, filename string) (res string, err error) {
|
||||||
|
//updateServerS3Name, _ := g.Config().Get(ctx, "update_server_s3_name")
|
||||||
|
|
||||||
|
s3Mod = s3.New(typ)
|
||||||
|
bucketName := s3Mod.GetCfg().BucketName
|
||||||
|
obj, err := os.Open(filename)
|
||||||
|
ff, err := obj.Stat()
|
||||||
|
_, err = s3Mod.PutObject(obj, filename, bucketName, ff.Size())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//上传当前文件
|
||||||
|
get, err := s3Mod.GetFileUrl(filename, bucketName)
|
||||||
|
g.Log().Debugf(gctx.New(), "下载地址:%s", get)
|
||||||
|
|
||||||
|
res = get.String()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user