Skip to content

Commit bb0712b

Browse files
author
XZB-1248
committed
optimize: compress frontend assets
1 parent 723c953 commit bb0712b

File tree

8 files changed

+271
-27
lines changed

8 files changed

+271
-27
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ body:
2323
attributes:
2424
label: Version / 版本
2525
description: What version of our software are you running?
26-
placeholder: v0.1.1
26+
placeholder: v0.1.2
2727
validations:
2828
required: true
2929
- type: textarea

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## v0.1.2
2+
3+
* Optimize: compress frontend assets.
4+
5+
* 优化: 压缩前端资源,加快加载速度。
6+
7+
8+
19
## v0.1.1
210

311
* Add: text file editor.

server/handler/bridge.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,16 @@ func bridgePush(ctx *gin.Context) {
100100
buf := make([]byte, 2<<14)
101101
srcConn.SetReadDeadline(common.Now.Add(5 * time.Second))
102102
n, err := bridge.src.Request.Body.Read(buf)
103+
if n == 0 {
104+
break
105+
}
103106
if err != nil {
104107
eof = err == io.EOF
105108
if !eof {
106109
break
107110
}
108111
}
109-
if n == 0 {
110-
break
111-
}
112-
dstConn.SetWriteDeadline(time.Now().Add(10 * time.Second))
112+
dstConn.SetWriteDeadline(common.Now.Add(10 * time.Second))
113113
_, err = bridge.dst.Writer.Write(buf[:n])
114114
if eof || err != nil {
115115
break
@@ -154,16 +154,16 @@ func bridgePull(ctx *gin.Context) {
154154
buf := make([]byte, 2<<14)
155155
srcConn.SetReadDeadline(common.Now.Add(5 * time.Second))
156156
n, err := bridge.src.Request.Body.Read(buf)
157+
if n == 0 {
158+
break
159+
}
157160
if err != nil {
158161
eof = err == io.EOF
159162
if !eof {
160163
break
161164
}
162165
}
163-
if n == 0 {
164-
break
165-
}
166-
dstConn.SetWriteDeadline(time.Now().Add(10 * time.Second))
166+
dstConn.SetWriteDeadline(common.Now.Add(10 * time.Second))
167167
_, err = bridge.dst.Writer.Write(buf[:n])
168168
if eof || err != nil {
169169
break

server/handler/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func checkClient(ctx *gin.Context) {
4343
ctx.AbortWithStatusJSON(http.StatusBadRequest, modules.Packet{Code: -1, Msg: `${i18n|invalidParameter}`})
4444
return
4545
}
46-
_, err := os.Open(fmt.Sprintf(config.BuiltPath, form.OS, form.Arch))
46+
_, err := os.Stat(fmt.Sprintf(config.BuiltPath, form.OS, form.Arch))
4747
if err != nil {
4848
ctx.AbortWithStatusJSON(http.StatusNotFound, modules.Packet{Code: 1, Msg: `${i18n|osOrArchNotPrebuilt}`})
4949
return

server/main.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import (
1111
"encoding/hex"
1212
"fmt"
1313
"github.com/rakyll/statik/fs"
14+
"io"
1415
"net"
1516
"os"
1617
"os/signal"
18+
"path"
19+
"strings"
1720
"syscall"
1821
"time"
1922

@@ -70,7 +73,9 @@ func main() {
7073
handler.InitRouter(app.Group(`/api`))
7174
app.Any(`/ws`, wsHandshake)
7275
app.NoRoute(handler.AuthHandler, func(ctx *gin.Context) {
73-
http.FileServer(webFS).ServeHTTP(ctx.Writer, ctx.Request)
76+
if !serveGzip(ctx, webFS) && !checkCache(ctx, webFS) {
77+
http.FileServer(webFS).ServeHTTP(ctx.Writer, ctx.Request)
78+
}
7479
})
7580
}
7681

@@ -306,3 +311,81 @@ func authCheck() gin.HandlerFunc {
306311
lastRequest = now
307312
}
308313
}
314+
315+
func serveGzip(ctx *gin.Context, statikFS http.FileSystem) bool {
316+
headers := ctx.Request.Header
317+
filename := path.Clean(ctx.Request.RequestURI)
318+
if !strings.Contains(headers.Get(`Accept-Encoding`), `gzip`) {
319+
return false
320+
}
321+
if strings.Contains(headers.Get(`Connection`), `Upgrade`) {
322+
return false
323+
}
324+
if strings.Contains(headers.Get(`Accept`), `text/event-stream`) {
325+
return false
326+
}
327+
328+
file, err := statikFS.Open(filename + `.gz`)
329+
if err != nil {
330+
return false
331+
}
332+
333+
file.Seek(0, io.SeekStart)
334+
conn, ok := ctx.Request.Context().Value(`Conn`).(net.Conn)
335+
if !ok {
336+
file.Close()
337+
return false
338+
}
339+
340+
etag := fmt.Sprintf(`"%x-%s"`, []byte(filename), config.COMMIT)
341+
if headers.Get(`If-None-Match`) == etag {
342+
ctx.Status(http.StatusNotModified)
343+
return true
344+
}
345+
ctx.Header(`Cache-Control`, `max-age=604800`)
346+
ctx.Header(`ETag`, etag)
347+
ctx.Header(`Expires`, common.Now.Add(7*24*time.Hour).Format(`Mon, 02 Jan 2006 15:04:05 GMT`))
348+
349+
ctx.Writer.Header().Del(`Content-Length`)
350+
ctx.Header(`Content-Encoding`, `gzip`)
351+
ctx.Header(`Vary`, `Accept-Encoding`)
352+
ctx.Status(http.StatusOK)
353+
354+
for {
355+
eof := false
356+
buf := make([]byte, 2<<14)
357+
n, err := file.Read(buf)
358+
if n == 0 {
359+
break
360+
}
361+
if err != nil {
362+
eof = err == io.EOF
363+
if !eof {
364+
break
365+
}
366+
}
367+
conn.SetWriteDeadline(common.Now.Add(10 * time.Second))
368+
_, err = ctx.Writer.Write(buf[:n])
369+
if eof || err != nil {
370+
break
371+
}
372+
}
373+
conn.SetWriteDeadline(time.Time{})
374+
file.Close()
375+
ctx.Done()
376+
return true
377+
}
378+
379+
func checkCache(ctx *gin.Context, _ http.FileSystem) bool {
380+
filename := path.Clean(ctx.Request.RequestURI)
381+
382+
etag := fmt.Sprintf(`"%x-%s"`, []byte(filename), config.COMMIT)
383+
if ctx.Request.Header.Get(`If-None-Match`) == etag {
384+
ctx.Status(http.StatusNotModified)
385+
return true
386+
}
387+
ctx.Header(`ETag`, etag)
388+
ctx.Header(`Cache-Control`, `max-age=604800`)
389+
ctx.Header(`Expires`, common.Now.Add(7*24*time.Hour).Format(`Mon, 02 Jan 2006 15:04:05 GMT`))
390+
return false
391+
}

web/package-lock.json

Lines changed: 125 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"antd-dayjs-webpack-plugin": "^1.0.6",
4141
"babel-loader": "^8.2.4",
4242
"clean-webpack-plugin": "^4.0.0",
43+
"compression-webpack-plugin": "^10.0.0",
4344
"copy-webpack-plugin": "^10.2.4",
4445
"css-loader": "^6.7.1",
4546
"html-webpack-plugin": "^5.5.0",

0 commit comments

Comments
 (0)