Skip to content

Commit

Permalink
optimize: compress frontend assets
Browse files Browse the repository at this point in the history
  • Loading branch information
XZB-1248 committed Jun 19, 2022
1 parent 723c953 commit bb0712b
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ body:
attributes:
label: Version / 版本
description: What version of our software are you running?
placeholder: v0.1.1
placeholder: v0.1.2
validations:
required: true
- type: textarea
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v0.1.2

* Optimize: compress frontend assets.

* 优化: 压缩前端资源,加快加载速度。



## v0.1.1

* Add: text file editor.
Expand Down
16 changes: 8 additions & 8 deletions server/handler/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ func bridgePush(ctx *gin.Context) {
buf := make([]byte, 2<<14)
srcConn.SetReadDeadline(common.Now.Add(5 * time.Second))
n, err := bridge.src.Request.Body.Read(buf)
if n == 0 {
break
}
if err != nil {
eof = err == io.EOF
if !eof {
break
}
}
if n == 0 {
break
}
dstConn.SetWriteDeadline(time.Now().Add(10 * time.Second))
dstConn.SetWriteDeadline(common.Now.Add(10 * time.Second))
_, err = bridge.dst.Writer.Write(buf[:n])
if eof || err != nil {
break
Expand Down Expand Up @@ -154,16 +154,16 @@ func bridgePull(ctx *gin.Context) {
buf := make([]byte, 2<<14)
srcConn.SetReadDeadline(common.Now.Add(5 * time.Second))
n, err := bridge.src.Request.Body.Read(buf)
if n == 0 {
break
}
if err != nil {
eof = err == io.EOF
if !eof {
break
}
}
if n == 0 {
break
}
dstConn.SetWriteDeadline(time.Now().Add(10 * time.Second))
dstConn.SetWriteDeadline(common.Now.Add(10 * time.Second))
_, err = bridge.dst.Writer.Write(buf[:n])
if eof || err != nil {
break
Expand Down
2 changes: 1 addition & 1 deletion server/handler/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func checkClient(ctx *gin.Context) {
ctx.AbortWithStatusJSON(http.StatusBadRequest, modules.Packet{Code: -1, Msg: `${i18n|invalidParameter}`})
return
}
_, err := os.Open(fmt.Sprintf(config.BuiltPath, form.OS, form.Arch))
_, err := os.Stat(fmt.Sprintf(config.BuiltPath, form.OS, form.Arch))
if err != nil {
ctx.AbortWithStatusJSON(http.StatusNotFound, modules.Packet{Code: 1, Msg: `${i18n|osOrArchNotPrebuilt}`})
return
Expand Down
85 changes: 84 additions & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import (
"encoding/hex"
"fmt"
"github.com/rakyll/statik/fs"
"io"
"net"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -70,7 +73,9 @@ func main() {
handler.InitRouter(app.Group(`/api`))
app.Any(`/ws`, wsHandshake)
app.NoRoute(handler.AuthHandler, func(ctx *gin.Context) {
http.FileServer(webFS).ServeHTTP(ctx.Writer, ctx.Request)
if !serveGzip(ctx, webFS) && !checkCache(ctx, webFS) {
http.FileServer(webFS).ServeHTTP(ctx.Writer, ctx.Request)
}
})
}

Expand Down Expand Up @@ -306,3 +311,81 @@ func authCheck() gin.HandlerFunc {
lastRequest = now
}
}

func serveGzip(ctx *gin.Context, statikFS http.FileSystem) bool {
headers := ctx.Request.Header
filename := path.Clean(ctx.Request.RequestURI)
if !strings.Contains(headers.Get(`Accept-Encoding`), `gzip`) {
return false
}
if strings.Contains(headers.Get(`Connection`), `Upgrade`) {
return false
}
if strings.Contains(headers.Get(`Accept`), `text/event-stream`) {
return false
}

file, err := statikFS.Open(filename + `.gz`)
if err != nil {
return false
}

file.Seek(0, io.SeekStart)
conn, ok := ctx.Request.Context().Value(`Conn`).(net.Conn)
if !ok {
file.Close()
return false
}

etag := fmt.Sprintf(`"%x-%s"`, []byte(filename), config.COMMIT)
if headers.Get(`If-None-Match`) == etag {
ctx.Status(http.StatusNotModified)
return true
}
ctx.Header(`Cache-Control`, `max-age=604800`)
ctx.Header(`ETag`, etag)
ctx.Header(`Expires`, common.Now.Add(7*24*time.Hour).Format(`Mon, 02 Jan 2006 15:04:05 GMT`))

ctx.Writer.Header().Del(`Content-Length`)
ctx.Header(`Content-Encoding`, `gzip`)
ctx.Header(`Vary`, `Accept-Encoding`)
ctx.Status(http.StatusOK)

for {
eof := false
buf := make([]byte, 2<<14)
n, err := file.Read(buf)
if n == 0 {
break
}
if err != nil {
eof = err == io.EOF
if !eof {
break
}
}
conn.SetWriteDeadline(common.Now.Add(10 * time.Second))
_, err = ctx.Writer.Write(buf[:n])
if eof || err != nil {
break
}
}
conn.SetWriteDeadline(time.Time{})
file.Close()
ctx.Done()
return true
}

func checkCache(ctx *gin.Context, _ http.FileSystem) bool {
filename := path.Clean(ctx.Request.RequestURI)

etag := fmt.Sprintf(`"%x-%s"`, []byte(filename), config.COMMIT)
if ctx.Request.Header.Get(`If-None-Match`) == etag {
ctx.Status(http.StatusNotModified)
return true
}
ctx.Header(`ETag`, etag)
ctx.Header(`Cache-Control`, `max-age=604800`)
ctx.Header(`Expires`, common.Now.Add(7*24*time.Hour).Format(`Mon, 02 Jan 2006 15:04:05 GMT`))
return false
}
125 changes: 125 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"antd-dayjs-webpack-plugin": "^1.0.6",
"babel-loader": "^8.2.4",
"clean-webpack-plugin": "^4.0.0",
"compression-webpack-plugin": "^10.0.0",
"copy-webpack-plugin": "^10.2.4",
"css-loader": "^6.7.1",
"html-webpack-plugin": "^5.5.0",
Expand Down
Loading

0 comments on commit bb0712b

Please sign in to comment.