Skip to content

Commit

Permalink
feat: use go:embed from go1.16
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Feb 20, 2021
1 parent 98c09a9 commit d3c4e05
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 41 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ jobs:
- uses: actions/setup-go@v2
with:
go-version: 1.16.0
- uses: actions/setup-node@v2
with:
node-version: '12'
- name: frontend
run: |
cd frontend
npm install
npm run build
cd ..
- name: build
env:
target: ${{ matrix.target }}
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ jobs:
- uses: actions/setup-go@v2
with:
go-version: 1.16.0
- uses: actions/setup-node@v2
with:
node-version: '12'
- id: release-asset
env:
target: ${{ matrix.target }}
run: |
cd frontend
npm install
npm run build
cd ..
version=${{ steps.get-version.outputs.version-without-v }}
os=$(echo $target | awk -F. '{print $1}')
arch=$(echo $target | awk -F. '{print $2}')
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ This is the server program of [rtty](https://github.com/zhaojh329/rtty)
# Usage
## download the pre-built release binary from [Release](https://github.com/zhaojh329/rttys/releases) page according to your os and arch or compile it by yourself.

go get -u github.com/zhaojh329/rttys
git clone https://github.com/zhaojh329/rttys

## Update statik
cd frontend
npm install
npm run build
cd ..

go get github.com/rakyll/statik
statik -src=frontend/dist
./build.sh linux amd64

## Authorization
### Token
Expand Down
10 changes: 6 additions & 4 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
# 如何使用
## [Release](https://github.com/zhaojh329/rttys/releases)页面下载编译好的二进制文件或者自己编译

go get -u github.com/zhaojh329/rttys
git clone https://github.com/zhaojh329/rttys

## 更新 statik
cd frontend
npm install
npm run build
cd ..

go get github.com/rakyll/statik
statik -src=frontend/dist
./build.sh linux amd64

## 认证
### Token
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/zhaojh329/rttys

go 1.14
go 1.16

require (
github.com/dwdcth/consoleEx v0.0.0-20180521133551-f56f6eb78b76
Expand All @@ -10,7 +10,6 @@ require (
github.com/json-iterator/go v1.1.9
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28
github.com/mattn/go-colorable v0.1.4
github.com/rakyll/statik v0.1.7
github.com/rs/zerolog v1.18.0
github.com/urfave/cli/v2 v2.3.0
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
Expand Down
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dwdcth/consoleEx v0.0.0-20180521133551-f56f6eb78b76 h1:eObfFy0e/9OQCd5tHy+855jrW7zTihdgIPD7hf2SOQ0=
github.com/dwdcth/consoleEx v0.0.0-20180521133551-f56f6eb78b76/go.mod h1:WPzFRpaqRmrZAD1vMpqUGZR24FE1EBoSG9lHKQyZOMM=
Expand Down Expand Up @@ -38,9 +39,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.18.0 h1:CbAm3kP2Tptby1i9sYy2MGRg0uxIN9cyDb59Ys7W8z8=
github.com/rs/zerolog v1.18.0/go.mod h1:9nvC1axdVrAHcu/s9taAVfBuIdTZLVQmKQyvrUjF5+I=
Expand All @@ -50,13 +50,12 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
Expand All @@ -74,11 +73,12 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc=
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
35 changes: 23 additions & 12 deletions http.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main

import (
"embed"
"net"
"net/http"
"path"
"strings"
"time"

"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
"github.com/rakyll/statik/fs"
"github.com/rs/zerolog/log"
"github.com/zhaojh329/rttys/cache"
"github.com/zhaojh329/rttys/config"
_ "github.com/zhaojh329/rttys/statik"
"github.com/zhaojh329/rttys/utils"
)

Expand All @@ -23,6 +23,9 @@ type credentials struct {

var httpSessions *cache.Cache

//go:embed frontend/dist
var static embed.FS

func allowOrigin(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
Expand Down Expand Up @@ -171,23 +174,31 @@ func httpStart(br *broker) {
c.Status(http.StatusForbidden)
})

statikFS, err := fs.New()
if err != nil {
log.Fatal().Msg(err.Error())
}

r.NoRoute(func(c *gin.Context) {
f, err := statikFS.Open(path.Clean(c.Request.URL.Path))
if err != nil {
c.Request.URL.Path = "/"
if !strings.HasPrefix(c.Request.URL.Path, "/frontend/dist/") {
c.Request.URL.Path = "/frontend/dist" + c.Request.URL.Path
r.HandleContext(c)
return
}
f.Close()
http.FileServer(statikFS).ServeHTTP(c.Writer, c.Request)

p := path.Clean(c.Request.URL.Path)

if p != "/frontend/dist/" {
f, err := static.Open(p[1:])
if err != nil {
c.Request.URL.Path = "/frontend/dist/"
r.HandleContext(c)
return
}
f.Close()
}

http.FileServer(http.FS(static)).ServeHTTP(c.Writer, c.Request)
})

go func() {
var err error

if cfg.SslCert != "" && cfg.SslKey != "" {
log.Info().Msgf("Listen user on: %s SSL on", cfg.AddrUser)
err = r.RunTLS(cfg.AddrUser, cfg.SslCert, cfg.SslKey)
Expand Down
14 changes: 0 additions & 14 deletions statik/statik.go

This file was deleted.

0 comments on commit d3c4e05

Please sign in to comment.