1 Star 1 Fork 0

linngc / center.gf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
alpine_get.go 2.36 KB
一键复制 编辑 原始数据 按行查看 历史
linngc 提交于 2023-07-28 17:56 . perf:完善alpine 镜像代理模块
// Package business
// @Link https://gitee.com/linngc/center.gf
// @Copyright Copyright (c) 2022 center CLI
// @Author linngc
// @License
//
package business
import (
"context"
"fmt"
"gitee.com/linngc/center.gf/contrib/module/cachebuffer/clientv1"
"gitee.com/linngc/center.gf/contrib/module/cachebuffer/model/marshal"
"gitee.com/linngc/center.gf/contrib/plugins/mirror/alpine/utility"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"io"
"net/http"
"path"
)
// GetMapping get/head请求时候入口
// 最终请求示例: http://mirrors.ustc.edu.cn/alpine/v3.18/main/x86_64/7zip-22.01-r5.apk
// moduleName
func (c *cAlpineProxy) GetMapping(ctx context.Context) (u *marshal.ObjectsInfo, err error) {
r := g.RequestFromCtx(ctx)
cfg := utility.Config()
cache := cfg.Cache
dial := clientv1.Dial(ctx, cache, "alpine")
version := r.Get("version").String() //版本
sort := r.Get("sort").String() //数据分类
command := r.Get("command").String() //系统指令类型
moduleName := r.Get("moduleName").String() //包路径名称
localPath := path.Join(version, sort, command, moduleName)
//先从缓存Cos获取文件,不存在则重新调用uri http接口
info, _ := dial.Client.GetObject(ctx, localPath)
if nil == info || len(info.Bytes) == 0 {
if len(cfg.Mirror) == 0 {
return u, gerror.NewCode(gcode.CodeMissingConfiguration, fmt.Sprintf("mirror not find config %v", err))
}
// 尝试从url镜像获取返回
response := clientv1.Stat.GetRemote(ctx, cfg.Mirror, moduleName)
defer c.closeResponse(response)
if response == nil {
return u, gerror.NewCode(gcode.CodeMissingConfiguration, "read Remote not support")
}
code := response.StatusCode
data, err := io.ReadAll(response.Body)
if code == http.StatusOK {
go func(ctx context.Context, moduleName string, data []byte) {
if info, err = dial.Client.PutByObject(ctx, localPath, data); err != nil {
g.Log().Errorf(ctx, fmt.Sprintf("cache mirror file failed. message: %v", err))
}
}(ctx, moduleName, data)
}
// todo 待实现 marshal.ObjectsInfo 数据组装
//info = &marshal.ObjectsInfo{Bucket: c.Bucket, Cache: c.CosType, Name: moduleName, ETag: md5, Location: location, Size: int64(len(data)), Bytes: data}
}
g.Log().Debugf(ctx, "Response File Writer uri:'%s'", localPath)
return info, nil
}
Go
1
https://gitee.com/linngc/center.gf.git
git@gitee.com:linngc/center.gf.git
linngc
center.gf
center.gf
6203b02904a0

搜索帮助