清理代码 & 支持 ignore

This commit is contained in:
dragon
2025-05-09 14:50:10 +08:00
parent 9413188aa9
commit ef31433d91
14 changed files with 405 additions and 118 deletions

View File

@@ -118,18 +118,22 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
http.Redirect(writer, request, fmt.Sprintf("https://%s/%s", meta.Alias[0], meta.Path), http.StatusFound)
return nil
}
for prefix, backend := range meta.Proxy {
if strings.HasPrefix(meta.Path, prefix) {
targetPath := strings.TrimPrefix(meta.Path, prefix)
proxyPath := "/" + meta.Path
if strings.HasPrefix(proxyPath, prefix) {
targetPath := strings.TrimPrefix(proxyPath, prefix)
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
zap.L().Debug("命中反向代理", zap.Any("prefix", prefix), zap.Any("backend", backend),
zap.Any("path", meta.Path), zap.Any("target", targetPath))
u, _ := url.Parse(backend)
request.URL.Path = targetPath
request.RequestURI = request.URL.RequestURI()
u, _ := url.Parse(backend)
httputil.NewSingleHostReverseProxy(u).ServeHTTP(writer, request)
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.Transport = s.options.HttpClient.Transport
zap.L().Debug("命中反向代理", zap.Any("prefix", prefix), zap.Any("backend", backend),
zap.Any("path", proxyPath), zap.Any("target", fmt.Sprintf("%s%s", u, targetPath)))
proxy.ServeHTTP(writer, request)
return nil
}
}
@@ -137,6 +141,10 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
if request.Method != "GET" {
return os.ErrNotExist
}
if meta.IsIgnore(meta.Path) {
zap.L().Debug("ignore path", zap.Any("request", request.RequestURI), zap.Any("meta.path", meta.Path))
return os.ErrNotExist
}
result, err := s.reader.Open(meta.Owner, meta.Repo, meta.CommitID, meta.Path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
@@ -222,6 +230,5 @@ func (s *Server) Close() error {
if err := s.options.Cache.Close(); err != nil {
return err
}
return nil
return s.backend.Close()
}