Ai
1 Star 0 Fork 1

iThings/core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
proxy.go 2.37 KB
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2024-09-25 17:26 +08:00 . feat: bug fix
package proxy
import (
"fmt"
"gitee.com/i-Things/core/service/apisvr/internal/svc"
"gitee.com/i-Things/share/conf"
"gitee.com/i-Things/share/ctxs"
"gitee.com/i-Things/share/utils"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
func Handler(svcCtx *svc.ServiceContext) http.HandlerFunc {
dir := http.Dir(utils.GerRealPwd(svcCtx.Config.Proxy.FileProxy.FrontDir))
fileServer := http.FileServer(dir)
return func(w http.ResponseWriter, r *http.Request) {
header := w.Header()
upath := r.URL.Path
for _, v := range svcCtx.Config.Proxy.StaticProxy {
if strings.HasPrefix(upath, v.Router) {
staticProxy(svcCtx, v, w, r)
return
}
}
header.Set("Access-Control-Allow-Headers", ctxs.HttpAllowHeader)
header.Set("Access-Control-Allow-Origin", "*")
f, err := dir.Open(upath)
if err != nil {
defaultHandle(svcCtx, upath, w, r, false)
return
} else {
info, err := f.Stat()
if err != nil || info.Mode().IsDir() {
defaultHandle(svcCtx, upath, w, r, info.Mode().IsDir())
return
}
}
fileServer.ServeHTTP(w, r)
}
}
func staticProxy(svcCtx *svc.ServiceContext, conf *conf.StaticProxyConf, w http.ResponseWriter, r *http.Request) {
remote, err := url.Parse(conf.Dest)
if err != nil {
//defaultHandle(svcCtx, w, r)
return
}
proxy := httputil.NewSingleHostReverseProxy(remote)
r.Host = remote.Host
if conf.DeletePrefix {
r.URL.Path = strings.TrimPrefix(r.URL.Path, conf.Router)
}
for k := range w.Header() {
w.Header().Del(k)
}
proxy.ServeHTTP(w, r)
}
func defaultHandle(svcCtx *svc.ServiceContext, upath string, w http.ResponseWriter, r *http.Request, isDir bool) {
if !svcCtx.Config.Proxy.FileProxy.IsEnable {
http.NotFound(w, r)
return
}
dir := http.Dir(utils.GerRealPwd(svcCtx.Config.Proxy.FileProxy.FrontDir))
var (
f http.File
err error
)
switch upath {
case "/favicon.ico":
f, err = dir.Open(fmt.Sprintf("%sfavicon.ico", svcCtx.Config.Proxy.FileProxy.CoreDir))
case "/":
http.Redirect(w, r, svcCtx.Config.Proxy.FileProxy.CoreDir, http.StatusMovedPermanently)
return
default:
if strings.HasSuffix(upath, "/") {
f, err = dir.Open(upath + "index.html")
} else if isDir {
f, err = dir.Open(upath + "/index.html")
}
}
if err != nil || f == nil {
http.NotFound(w, r)
return
}
indexFile, err := io.ReadAll(f)
if err != nil {
return
}
w.WriteHeader(http.StatusOK)
w.Write(indexFile)
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/i-Things/core.git
git@gitee.com:i-Things/core.git
i-Things
core
core
v0.1.6

搜索帮助