1 Star 1 Fork 0

linngc / center.gf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
golang_compile.go 4.23 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/golangproxy/internal/consts"
"gitee.com/linngc/center.gf/contrib/plugins/xproxy/golangproxy/internal/utility"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"golang.org/x/mod/module"
"io"
"net/http"
"path"
"strings"
)
func (c *cGolangProxy) Compile(ctx context.Context) (u *marshal.ObjectsInfo, err error) {
r := g.RequestFromCtx(ctx)
cfg := utility.Config()
cache := cfg.Cache
dial := clientv1.Dial(ctx, cache, "golang")
moduleName := r.Get("moduleName").String() //模块名称
g.Log().Infof(ctx, "path:", r.Request.URL.Path)
// sumdb handler
if strings.HasPrefix(moduleName, "sumdb/") {
c.getSumdb(r)
return u, nil
}
i := strings.Index(moduleName, "/@")
if i < 0 {
return u, gerror.NewCode(gcode.CodeMissingConfiguration, "no such path")
}
localPath := path.Join(cache, 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", cfg.Mirror))
}
// 尝试从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, "cache mirror file failed. message: %v", err)
return
}
}(ctx, localPath, data)
moduleName = strings.ToLower(moduleName)
what := moduleName[i+len("/@"):]
switch what {
case "latest":
c.goLatest(r, data)
case "v/list":
c.goList(r, data)
default:
what = strings.TrimPrefix(what, "v/")
ext := path.Ext(what)
vers, err := module.UnescapeVersion(strings.TrimSuffix(what, ext))
if err != nil {
r.Response.WriteHeader(http.StatusNotFound)
}
if vers == "latest" {
r.Response.WriteHeader(http.StatusNotFound)
}
if ext != ".info" && vers != module.CanonicalVersion(vers) {
r.Response.WriteHeader(http.StatusNotFound)
}
switch ext {
case ".info":
c.goInfo(r, data)
case ".mod":
c.goMod(r, data)
case ".zip":
c.goZip(r, data)
default:
return u, gerror.NewCode(gcode.CodeMissingConfiguration, "request not recognized")
}
}
}
}
//https://goproxy.cn/golang.org/x/net/@v/list
return info, nil
}
func (c *cGolangProxy) goList(r *ghttp.Request, data []byte) {
if len(data) > 0 {
c.cacheHeader(r, consts.ContentTypeText, len(data))
}
}
func (c *cGolangProxy) goLatest(r *ghttp.Request, data []byte) {
if len(data) > 0 {
c.cacheHeader(r, consts.ContentTypeJSON, len(data))
}
}
func (c *cGolangProxy) goInfo(r *ghttp.Request, data []byte) {
if len(data) > 0 {
c.cacheHeader(r, consts.ContentTypeJSON, len(data))
}
}
func (c *cGolangProxy) goMod(r *ghttp.Request, data []byte) {
if len(data) > 0 {
c.cacheHeader(r, consts.ContentTypeText, len(data))
}
}
func (c *cGolangProxy) goZip(r *ghttp.Request, data []byte) {
if len(data) > 0 {
c.cacheHeader(r, consts.ContentTypeBinary, len(data))
}
}
func (c *cGolangProxy) cacheHeader(r *ghttp.Request, contentType string, len int) {
r.Response.Header().Set("Content-Type", contentType)
r.Response.Header().Set("Content-Length", fmt.Sprint(len))
r.Response.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", len))
}
Go
1
https://gitee.com/linngc/center.gf.git
git@gitee.com:linngc/center.gf.git
linngc
center.gf
center.gf
96490c8eba32

搜索帮助