common_get_file_handler.go 595 字节
package common

import (
	"github.com/zeromicro/go-zero/rest/httpx"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc/cmd/bsi/api/internal/svc"
	"net/http"
	"path/filepath"
)

func CommonGetFileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req struct {
			Module string `path:"module"`
		}
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}
		path := "public"
		handler := http.FileServer(http.Dir(path))
		r.URL.Path = filepath.Join(req.Module)
		handler.ServeHTTP(w, r)
	}
}