common_get_log_handler.go 787 字节
package common

import (
	"net/http"
	"path/filepath"
	"strings"

	"github.com/zeromicro/go-zero/rest/httpx"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc/cmd/bsi/api/internal/svc"
)

func CommonGetLogHandler(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 := svcCtx.Config.Log.Path
		if svcCtx.Config.Log.Mode != "file" {
			return
		}
		if path == "" {
			path = "logs"
		}
		if !strings.HasSuffix(req.Module, ".log") {
			req.Module += ".log"
		}
		handler := http.FileServer(http.Dir(path))
		r.URL.Path = filepath.Join(req.Module)
		handler.ServeHTTP(w, r)
	}
}