1 Star 1 Fork 0

linngc / center.gf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
maven_compile.go 3.51 KB
一键复制 编辑 原始数据 按行查看 历史
linngc 提交于 2023-08-03 17:14 . style:调整目录结构
// 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"
cachebufferutiltiy "gitee.com/linngc/center.gf/contrib/module/cachebuffer/toolkit"
"gitee.com/linngc/center.gf/contrib/plugins/xproxy/mavenproxy/internal/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"
"strings"
)
// Compile maven pom.xml get/head请求时候入口
// 最终请求示例: http://127.0.0.1:10010/proxy/maven/public/org/springframework/boot/spring-boot/2.5.6/spring-boot-2.5.6.jar
// libName 示例 public
// jarPath 示例 org/springframework/boot/spring-boot/2.5.6/spring-boot-2.5.6.jar
func (c *cMavenProxy) Compile(ctx context.Context, authFunc ...func(authorization string) bool) (u *marshal.ObjectsInfo, err error) {
r := g.RequestFromCtx(ctx)
cfg := utility.Config()
cache := cfg.Cache
dial := clientv1.Dial(ctx, cache, "maven")
libName := r.Get("libName").String() //仓库ID
moduleName := r.Get("moduleName").String() //jar包路径名称
repository, err := utility.GetRepository(ctx, cfg, libName)
if err != nil {
return u, gerror.NewCode(gcode.CodeMissingConfiguration, err.Error())
}
if repository.Mode&4 != 4 {
return u, gerror.NewCode(gcode.CodeMissingConfiguration, "repository not support read")
}
ext := path.Ext(moduleName)
if ext == "" && !strings.HasSuffix(moduleName, "/") {
r.Response.RedirectTo(r.RequestURI)
return u, nil
}
localPath := path.Join(repository.Id, moduleName)
//请求授权判断
if repository.Auth {
if nil != authFunc && len(authFunc) > 0 {
auth := utility.CheckAuth(ctx)
if len(auth) == 0 {
return u, gerror.NewCode(gcode.CodeInvalidParameter, "Authorization IsNull")
}
if !authFunc[0](auth) {
return u, gerror.NewCode(gcode.CodeNotAuthorized, "Unauthorised")
}
if err = utility.GenerateHash(localPath); err != nil {
return u, gerror.NewCode(gcode.CodeInternalError, fmt.Sprintf("generate hash failed, message: %v", err))
}
}
}
//先从缓存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 := cachebufferutiltiy.GetRemote(ctx, cfg.Mirror, moduleName)
defer utility.CloseResponse(response)
if response == nil {
return u, gerror.NewCode(gcode.CodeMissingConfiguration, "read Remote not support")
}
code := response.StatusCode
if code == http.StatusOK {
data, err := io.ReadAll(response.Body)
// tip 使用marshal.ObjectsInfo 数据组装
info = dial.Client.GetData(localPath, data)
go func(ctx context.Context, localPath string, data []byte) {
if err = dial.Client.PutByObject(ctx, localPath, data); err != nil {
g.Log().Errorf(ctx, fmt.Sprintf("cache mirror file failed. message: %v", err))
}
}(ctx, localPath, data)
}
}
rawPath := fmt.Sprintf("/%s%s", repository.Id, moduleName)
g.Log().Debugf(ctx, "repository jarFile Response uri:'%s'", u)
r.Request.URL.RawPath = rawPath
r.Request.URL.Path = rawPath
g.Log().Debugf(ctx, "Response jarFile 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
96490c8eba32

搜索帮助