/
static.go
232 lines (209 loc) · 6.36 KB
/
static.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Package http implements http server.
package http
import (
"bytes"
"crypto/sha1"
"fmt"
"html/template"
"net/http"
"os"
"strings"
"time"
"github.com/Laisky/errors/v2"
gconfig "github.com/Laisky/go-config/v2"
gutils "github.com/Laisky/go-utils/v4"
"github.com/Laisky/go-utils/v4/json"
"github.com/gin-gonic/gin"
iconfig "github.com/Laisky/go-ramjet/internal/tasks/gptchat/config"
itemplates "github.com/Laisky/go-ramjet/internal/tasks/gptchat/templates"
ijs "github.com/Laisky/go-ramjet/internal/tasks/gptchat/templates/js"
ipages "github.com/Laisky/go-ramjet/internal/tasks/gptchat/templates/pages"
ipartials "github.com/Laisky/go-ramjet/internal/tasks/gptchat/templates/partials"
icss "github.com/Laisky/go-ramjet/internal/tasks/gptchat/templates/scss"
"github.com/Laisky/go-ramjet/library/log"
)
var (
httpcli *http.Client
staticFiles struct {
LibJs, SiteJs, DataJs, CSS *staticFile
}
)
func init() {
prepareStaticFiles()
}
// SetupHTTPCli setup http client
func SetupHTTPCli() (err error) {
httpargs := []gutils.HTTPClientOptFunc{
gutils.WithHTTPClientTimeout(300 * time.Second),
}
if gconfig.Shared.GetString("openai.proxy") != "" {
log.Logger.Info("use proxy for openai")
httpargs = append(httpargs, gutils.WithHTTPClientProxy(iconfig.Config.Proxy))
} else if os.Getenv("HTTP_PROXY") != "" {
log.Logger.Info("set proxy for openai from env")
httpargs = append(httpargs, gutils.WithHTTPClientProxy(os.Getenv("HTTP_PROXY")))
}
httpcli, err = gutils.NewHTTPClient(httpargs...)
if err != nil {
return errors.Wrap(err, "new http client")
}
return nil
}
type staticFile struct {
Name, Ext string
Content []byte
Hash string
ContentType string
}
func prepareStaticFiles() {
staticFiles.LibJs = &staticFile{
Name: "libs",
Ext: ".js",
ContentType: "application/javascript",
Content: bytes.Join([][]byte{
ijs.Libs,
}, []byte("\n")),
}
staticFiles.SiteJs = &staticFile{
Name: "sites",
Ext: ".js",
ContentType: "application/javascript",
Content: ijs.Chat,
}
staticFiles.DataJs = &staticFile{
Name: "data",
Ext: ".js",
ContentType: "application/javascript",
Content: ijs.ChatPrompts,
}
staticFiles.CSS = &staticFile{
Name: "sites",
Ext: ".css",
ContentType: "text/css",
Content: icss.SitesCSS,
}
for _, v := range []*staticFile{
staticFiles.LibJs,
staticFiles.SiteJs,
staticFiles.DataJs,
staticFiles.CSS,
} {
hashed := sha1.Sum(v.Content)
v.Hash = fmt.Sprintf("%x", hashed)[:7]
v.Name = fmt.Sprintf("%s-%s%s", v.Name, v.Hash, v.Ext)
}
}
// RegisterStatic register static files
func RegisterStatic(g gin.IRouter) {
for _, sf := range []*staticFile{
staticFiles.LibJs,
staticFiles.SiteJs,
staticFiles.DataJs,
staticFiles.CSS,
} {
sf := sf
g.GET(fmt.Sprintf("/%s", sf.Name), func(ctx *gin.Context) {
ctx.Header("Cache-Control", "max-age=86400")
ctx.Data(http.StatusOK, sf.ContentType, sf.Content)
})
}
}
var ts = time.Now().Format(time.RFC3339Nano)
func injectVer() any {
v := map[string]any{}
_ = json.UnmarshalFromString(gutils.PrettyBuildInfo(), &v)
return v
}
// Chat render chat page
func Chat(ctx *gin.Context) {
tpl := template.New("mytemplate")
for name, cnt := range map[string]string{
"base": itemplates.Base,
"chat": ipages.Chat,
} {
if _, err := tpl.New(name).Parse(cnt); AbortErr(ctx, err) {
return
}
}
if iconfig.Config.GoogleAnalytics != "" {
if _, err := tpl.Parse(ipartials.GoogleAnalytics); AbortErr(ctx, err) {
return
}
}
ctx.Status(http.StatusOK)
ctx.Header(gutils.HTTPHeaderContentType, "text/html; charset=utf-8")
injectData := map[string]any{
"openai": map[string]any{
"direct": iconfig.Config.API,
"proxy": "/api",
},
"static_libs": map[string]any{
"chat_prompts": staticFiles.DataJs.Name,
},
"qa_chat_models": iconfig.Config.QAChatModels,
"version": injectVer(),
}
injectDataPayload, err := json.MarshalToString(injectData)
if AbortErr(ctx, err) {
return
}
tplArg := struct {
DataJSON string
BootstrapJs, BootstrapCss,
SeeJs, ShowdownJs, BootstrapIcons,
PrismJs, PrismCss,
FuseJs string
LibJs, SiteJs, DataJs, SiteCss string
Version string
GaCode string
}{
DataJSON: injectDataPayload,
BootstrapJs: iconfig.Config.StaticLibs["bootstrap_js"],
BootstrapCss: iconfig.Config.StaticLibs["bootstrap_css"],
BootstrapIcons: iconfig.Config.StaticLibs["bootstrap_icons"],
SeeJs: iconfig.Config.StaticLibs["sse_js"],
ShowdownJs: iconfig.Config.StaticLibs["showdown_js"],
PrismJs: iconfig.Config.StaticLibs["prism_js"],
PrismCss: iconfig.Config.StaticLibs["prism_css"],
FuseJs: iconfig.Config.StaticLibs["fuse_js"],
DataJs: staticFiles.DataJs.Name,
LibJs: staticFiles.LibJs.Name,
SiteJs: staticFiles.SiteJs.Name,
SiteCss: staticFiles.CSS.Name,
Version: ts,
GaCode: iconfig.Config.GoogleAnalytics,
}
tplArg.BootstrapIcons = gutils.OptionalVal(&tplArg.BootstrapIcons,
"https://s3.laisky.com/static/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css")
tplArg.BootstrapJs = gutils.OptionalVal(&tplArg.BootstrapJs,
"https://s3.laisky.com/static/twitter-bootstrap/5.2.3/js/bootstrap.bundle.min.js")
tplArg.BootstrapCss = gutils.OptionalVal(&tplArg.BootstrapCss,
"https://s3.laisky.com/static/twitter-bootstrap/5.2.3/css/bootstrap.min.css")
tplArg.ShowdownJs = gutils.OptionalVal(&tplArg.ShowdownJs,
"https://s3.laisky.com/static/showdown/2.1.0/showdown.min.js")
tplArg.SeeJs = gutils.OptionalVal(&tplArg.SeeJs,
"https://s3.laisky.com/static/sse/1.0.0-patch/sse.js")
tplArg.PrismJs = gutils.OptionalVal(&tplArg.PrismJs,
"https://s3.laisky.com/static/prism/1.29.0/prism.js")
tplArg.PrismCss = gutils.OptionalVal(&tplArg.PrismCss,
"https://s3.laisky.com/static/prism/1.29.0/prism.css")
tplArg.FuseJs = gutils.OptionalVal(&tplArg.FuseJs,
"https://s3.laisky.com/static/fuse.js/6.6.2/fuse.min.js")
err = tpl.ExecuteTemplate(ctx.Writer, "base", tplArg)
if AbortErr(ctx, err) {
return
}
}
// CopyHeader copy header from `from` to `to`
func CopyHeader(to, from http.Header) {
for key, values := range from {
lowerKey := strings.ToLower(key)
switch {
case lowerKey == "content-type",
strings.HasPrefix(lowerKey, "x-oneapi-"):
for _, v := range values {
to.Add(key, v)
}
}
}
}