-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
154 lines (134 loc) · 3.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* @Author: Bin
* @Date: 2023-03-05
* @FilePath: /gpt-zmide-server/main.go
*/
package main
import (
"bytes"
"embed"
"html/template"
"io"
"io/fs"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/gin-gonic/gin"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
"gpt-zmide-server/helper"
"gpt-zmide-server/helper/logger"
_ "gpt-zmide-server/models"
"gpt-zmide-server/routers"
)
//go:embed dist/assets
var FSStatic embed.FS
//go:embed dist/views
var FSViews embed.FS
func renderNode(n *html.Node) string {
var buf bytes.Buffer
w := io.Writer(&buf)
html.Render(w, n)
return buf.String()
}
func newScriptDevNode() html.Node {
// scriptDev := `
// <script type="module">
// import RefreshRuntime from 'http://localhost:5173/@react-refresh'
// RefreshRuntime.injectIntoGlobalHook(window)
// window.$RefreshReg$ = () => {}
// window.$RefreshSig$ = () => (type) => type
// window.__vite_plugin_react_preamble_installed__ = true
// </script>
// `
scriptDev := html.Node{
Type: html.ElementNode,
Data: "script",
DataAtom: atom.Body,
Attr: []html.Attribute{
{
Key: "type",
Val: "module",
},
},
}
scriptDev.AppendChild(&html.Node{
Type: html.TextNode,
Data: `
import RefreshRuntime from '/@react-refresh'
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
`,
})
return scriptDev
}
func main() {
// 判断是部署环境
if helper.IsRelease() {
gin.SetMode(gin.ReleaseMode)
}
logger.InitLogger()
r := gin.Default()
// 配置静态文件路由
if gin.Mode() == "debug" {
// 前端调试模式
// 注入前端调试代码
var templ = template.New("")
templDir, err := template.ParseGlob("./views/*")
if err == nil {
for _, item := range templDir.Templates() {
document, err := goquery.NewDocumentFromReader(strings.NewReader(item.Tree.Root.String()))
if err == nil {
headStr := document.Find("head")
nodeItem := newScriptDevNode()
headStr.AddBack().Get(0).AppendChild(&nodeItem)
}
templItem := template.Must(templ.Parse(renderNode(document.AddBack().Get(0))))
// templ := template.Must(template.New(templ.Tree.Root.String()).Parse())
logger.Debug("解析 html " + item.Name())
templ.AddParseTree(item.Name(), templItem.Tree)
}
}
// goquery.NewDocument();
r.SetHTMLTemplate(templ)
r.StaticFS("/assets", http.Dir("./dist/assets"))
// 反向代理代码目录
proxyHandle := func(c *gin.Context) {
remote, err := url.Parse("http://localhost:5173")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.ServeHTTP(c.Writer, c.Request)
}
// r.Any("/src/:path/*name", proxyHandle)
r.Any("/src/*name", proxyHandle)
r.Any("/@id/*name", proxyHandle)
r.Any("/node_modules/*name", proxyHandle)
r.Any("/@vite/*name", proxyHandle)
r.Any("/@react-refresh", proxyHandle)
} else {
templ := template.Must(template.New("").ParseFS(FSViews, "dist/views/*"))
r.SetHTMLTemplate(templ)
staticDir, _ := fs.Sub(FSStatic, "dist/assets")
r.StaticFS("/assets", http.FS(staticDir))
}
// 注册路由
r = routers.BuildRouter(r)
// Listen and Server in 0.0.0.0:8091
host := "http://0.0.0.0:8091"
if helper.Config.Host != "" {
host = "http://" + helper.Config.Host + ":" + strconv.Itoa(helper.Config.Port)
}
serverHost, err := url.Parse(host)
if err != nil {
serverHost = &url.URL{Host: "0.0.0.0:8091"}
}
logger.Info("gpt-zmide-server start up, listening and serving HTTP on " + serverHost.String())
r.Run(serverHost.Host)
}