- MES(B): 工单/BOM/备料/工序字典/PLC下发/拧紧/扫码报工/半成品/AGV/追溯逻辑,WMS与海康RCS客户端,看板Redis缓存API(概览/设备/进度/报警/趋势)+SSE - WMS(C): JWT滑动续签、Excel导入、盘点、内部API、种子数据、独立Postgres配置 - WMS客户端(E): Go网关8891反向代理+内嵌Vue3十页 - 工位终端(D): SQLite本地缓存+模拟拧紧源+内嵌Vue3页面 - Dashboard(A): 看板数据改接MES内部缓存API,SSE实时刷新+vite代理 - 清理各项目球形磨遗留代码,新增部署手册.md
78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
package main
|
||
|
||
import (
|
||
"embed"
|
||
"flag"
|
||
"fmt"
|
||
"io/fs"
|
||
"net/http"
|
||
"path"
|
||
"strings"
|
||
|
||
"bj_power_wms_client/internal/config"
|
||
"bj_power_wms_client/internal/proxy"
|
||
|
||
"github.com/zeromicro/go-zero/core/conf"
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
"github.com/zeromicro/go-zero/rest"
|
||
)
|
||
|
||
//go:embed all:web/static
|
||
var webFS embed.FS
|
||
|
||
var configFile = flag.String("f", "etc/bj_power_wms_client.yaml", "the config file")
|
||
|
||
// newStaticHandler 嵌入式静态文件:命中则直接返回;未命中的非 /api 路径
|
||
// 回退到 index.html(SPA history 路由),由浏览器端 Vue Router 接管。
|
||
func newStaticHandler(fsys embed.FS) http.Handler {
|
||
sub, err := fs.Sub(fsys, "web/static")
|
||
if err != nil {
|
||
panic(fmt.Sprintf("嵌入静态目录不可用: %v", err))
|
||
}
|
||
fileServer := http.FileServer(http.FS(sub))
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
name := strings.TrimPrefix(path.Clean(r.URL.Path), "/")
|
||
if name == "" {
|
||
name = "index.html"
|
||
}
|
||
if f, openErr := sub.Open(name); openErr == nil {
|
||
f.Close()
|
||
fileServer.ServeHTTP(w, r)
|
||
return
|
||
}
|
||
r2 := r.Clone(r.Context())
|
||
r2.URL.Path = "/"
|
||
fileServer.ServeHTTP(w, r2)
|
||
})
|
||
}
|
||
|
||
func main() {
|
||
flag.Parse()
|
||
|
||
var c config.Config
|
||
conf.MustLoad(*configFile, &c)
|
||
|
||
apiProxy := proxy.New(c.Upstream)
|
||
staticHandler := newStaticHandler(webFS)
|
||
|
||
// 兜底路由:/api/* 反向代理到 WMS 后端,其余走嵌入式静态资源(未命中回退 index.html)
|
||
notFound := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
if strings.HasPrefix(r.URL.Path, "/api") {
|
||
if r.URL.Path == "/api" || strings.HasPrefix(r.URL.Path, "/api/") {
|
||
apiProxy.ServeHTTP(w, r)
|
||
return
|
||
}
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
staticHandler.ServeHTTP(w, r)
|
||
})
|
||
|
||
server := rest.MustNewServer(c.RestConf, rest.WithNotFoundHandler(notFound))
|
||
defer server.Stop()
|
||
|
||
fmt.Printf("Starting bj_power_wms_client at :%d...\n", c.Port)
|
||
logx.Infof("Starting bj_power_wms_client at :%d...", c.Port)
|
||
server.Start()
|
||
}
|