Skip to content

Commit f304630

Browse files
committed
change frontend
1 parent 273c129 commit f304630

File tree

6 files changed

+61
-58
lines changed

6 files changed

+61
-58
lines changed

.github/RELEASE.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
## Change log
22

3-
- Fix the permissions problem when installing sponge for linux normal users. [#13](https://github.com/zhufuyi/sponge/issues/13)
4-
- Embedded front-end static files in gin library.
3+
- Modify sponge ui.

internal/model/init.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
package model
33

44
import (
5+
"context"
6+
"database/sql"
57
"sync"
68
"time"
79

810
"github.com/zhufuyi/sponge/internal/config"
9-
1011
"github.com/zhufuyi/sponge/pkg/goredis"
1112
"github.com/zhufuyi/sponge/pkg/logger"
1213
"github.com/zhufuyi/sponge/pkg/mysql"
@@ -90,10 +91,24 @@ func CloseMysql() error {
9091
if err != nil {
9192
return err
9293
}
93-
if sqlDB != nil {
94-
return sqlDB.Close()
94+
95+
checkInUse(sqlDB, time.Second*5)
96+
97+
return sqlDB.Close()
98+
}
99+
100+
func checkInUse(sqlDB *sql.DB, duration time.Duration) {
101+
ctx, _ := context.WithTimeout(context.Background(), duration) //nolint
102+
for {
103+
select {
104+
case <-time.After(time.Millisecond * 500):
105+
if v := sqlDB.Stats().InUse; v == 0 {
106+
return
107+
}
108+
case <-ctx.Done():
109+
return
110+
}
95111
}
96-
return nil
97112
}
98113

99114
// ------------------------------------------------------------------------------------------

pkg/gin/frontend/README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@ Embed front-end web static files in gin and add routing.
66

77
### Example of use
88

9-
```go1
9+
```go
1010
import "github.com/zhufuyi/sponge/pkg/gin/frontend"
1111

1212
//go:embed user
1313
var staticFS embed.FS
1414

1515
func setFrontendRouter(r *gin.Engine) error {
1616
var (
17-
// index.html file path, also the routing of access
18-
htmlPath = "user/home"
19-
// file setting
20-
addrConfigFile = "user/home/config.js"
21-
22-
// addr setting, the set address is in the addrConfigFile file
23-
defaultAddr = "http://localhost:8080"
24-
// if cross-service is required, fill in the address of the server where the service is deployed here, e.g. http://192.168.3.37:8080
25-
customAddr = ""
17+
isUseEmbedFS = true
18+
htmlDir = "user/home"
19+
configFile = "user/home/config.js"
20+
modifyConfigFn = func(content []byte) []byte {
21+
// modify config code
22+
return content
23+
}
2624
)
2725

28-
return frontend.New(htmlPath, defaultAddr, customAddr, addrConfigFile, staticFS).SetRouter(r)
26+
err := frontend.New(staticFS, isUseEmbedFs, htmlDir, configFile, modifyConfigFn).SetRouter(r)
27+
if err != nil {
28+
panic(err)
29+
}
2930
}
3031
```
3132

32-
Note: in the above example, `user` is the directory where the front-end is located, the static file index.html is in the `user/home` directory, if customAddr is empty and the default address is `http://localhost:8080`, then the access to the index.html routing address is `http:// localhost:8080/user/home`. If you set customAddr to `http://192.168.3.37:8080`, the index.html routing address will be `http://192.168.3.37:8080/user/home`.
33+
Note: in the above example, `user` is the directory where the front-end is located, the static file index.html is in the `user/home` directory. If isUseEmbedFS is false and apiBaseUrl is set in the configuration file, cross-host access is supported.

pkg/gin/frontend/frontend.go

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package frontend
33

44
import (
5-
"bytes"
65
"embed"
76
"fmt"
87
"io/fs"
@@ -24,48 +23,40 @@ var targetDir = "frontend"
2423

2524
// FrontEnd is the frontend router configuration
2625
type FrontEnd struct {
27-
// directory where index.html is located, e.g. web/html.
28-
htmlDir string
29-
// defaultAddr is the default address of the back-end api request, e.g. http://localhost:8080.
30-
31-
defaultAddr string
32-
// custom address,the default is defaultAddr, which is used to configure whether cross-host access is possible.
33-
customAddr string
34-
// configuration file in which js requests the address of the back-end api request, e.g. config.js.
35-
addrConfigFile string
36-
3726
// embed.FS static resources.
3827
staticFS embed.FS
28+
// must be set to false if cross-host access is required, otherwise true
29+
isUseEmbedFS bool
30+
// directory where index.html is located, e.g. web/html
31+
htmlDir string
32+
// configuration file in which js requests the address of the back-end api request, e.g. config.js
33+
configFile string
34+
// modify config
35+
modifyConfigFn func(content []byte) []byte
3936
}
4037

4138
// New create a new frontend
42-
func New(htmlDir string, defaultAddr string, customAddr string, addrConfigFile string, staticFS embed.FS) *FrontEnd {
43-
if defaultAddr == "" {
44-
panic("default address cannot be empty")
45-
}
46-
if customAddr == "" {
47-
customAddr = defaultAddr
48-
}
39+
func New(staticFS embed.FS, isUseEmbedFS bool, htmlDir string, configFile string, modifyConfigFn func(content []byte) []byte) *FrontEnd {
4940
htmlDir = strings.Trim(htmlDir, "/")
5041
return &FrontEnd{
51-
htmlDir: htmlDir,
52-
defaultAddr: defaultAddr,
53-
customAddr: customAddr,
54-
addrConfigFile: addrConfigFile,
5542
staticFS: staticFS,
43+
isUseEmbedFS: isUseEmbedFS,
44+
htmlDir: htmlDir,
45+
configFile: configFile,
46+
modifyConfigFn: modifyConfigFn,
5647
}
5748
}
5849

5950
// SetRouter set frontend router
6051
func (f *FrontEnd) SetRouter(r *gin.Engine) error {
61-
routerPath := fmt.Sprintf("/%s/index.html", f.htmlDir)
52+
routerPath := fmt.Sprintf("%s/index.html", f.htmlDir)
6253
// solve vue using history route 404 problem
6354
r.NoRoute(browserRefreshFS(f.staticFS, routerPath))
6455

6556
relativePath := fmt.Sprintf("/%s/*filepath", f.htmlDir)
6657

6758
// use embed file
68-
if f.isUseEmbedFS() {
59+
if f.isUseEmbedFS {
6960
r.GET(relativePath, func(c *gin.Context) {
7061
staticServer := http.FileServer(http.FS(f.staticFS))
7162
staticServer.ServeHTTP(c.Writer, c.Request)
@@ -87,10 +78,6 @@ func (f *FrontEnd) SetRouter(r *gin.Engine) error {
8778
return nil
8879
}
8980

90-
func (f *FrontEnd) isUseEmbedFS() bool {
91-
return f.defaultAddr == f.customAddr
92-
}
93-
9481
func (f *FrontEnd) saveFSToLocal() error {
9582
_ = os.RemoveAll(filepath.Join(targetDir, f.htmlDir))
9683
time.Sleep(time.Millisecond * 10)
@@ -116,8 +103,8 @@ func (f *FrontEnd) saveFSToLocal() error {
116103
}
117104

118105
// replace config address
119-
if path == f.addrConfigFile {
120-
content = bytes.ReplaceAll(content, []byte(f.defaultAddr), []byte(f.customAddr))
106+
if path == f.configFile {
107+
content = f.modifyConfigFn(content)
121108
}
122109

123110
// Save the content to the local file

pkg/gin/frontend/frontend_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,26 @@ var staticFS embed.FS
1414

1515
func TestFrontEnd_SetRouter(t *testing.T) {
1616
var (
17-
htmlPath = "user/home"
18-
addrConfigFile = "user/home/config.js"
19-
20-
defaultAddr = "http://localhost:8080"
21-
customAddr = ""
17+
isUseEmbedFS = true
18+
htmlDir = "user/home"
19+
configFile = "user/home/config.js"
20+
modifyConfigFn = func(content []byte) []byte {
21+
return content
22+
}
2223
)
2324

2425
r := gin.New()
2526
gin.SetMode(gin.ReleaseMode)
26-
err := New(htmlPath, defaultAddr, customAddr, addrConfigFile, staticFS).SetRouter(r)
27+
err := New(staticFS, isUseEmbedFS, htmlDir, configFile, modifyConfigFn).SetRouter(r)
2728
if err != nil {
2829
t.Error(err)
2930
}
3031
time.Sleep(time.Millisecond * 100)
3132

32-
customAddr = "http://127.0.0.1:8080"
3333
r = gin.New()
3434
gin.SetMode(gin.ReleaseMode)
35-
err = New(htmlPath, defaultAddr, customAddr, addrConfigFile, staticFS).SetRouter(r)
35+
isUseEmbedFS = false
36+
err = New(staticFS, isUseEmbedFS, htmlDir, configFile, modifyConfigFn).SetRouter(r)
3637
if err != nil {
3738
t.Error(err)
3839
}

pkg/gin/middleware/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func main() {
8080

8181
r.POST("/user/login", Login)
8282
r.GET("/user/:id", middleware.Auth(), h.GetByID) // no verify field
83-
// r.GET("/user/:id", middleware.Auth(middleware.WithVerify(adminVerify)), userFun) // with verify field
83+
// r.GET("/user/:id", middleware.Auth(middleware.WithVerify(adminVerify)), h.GetByID) // with verify field
8484

8585
r.Run(serverAddr)
8686
}
@@ -115,7 +115,7 @@ func main() {
115115
r := gin.Default()
116116

117117
r.POST("/user/login", Login)
118-
r.GET("/user/:id", middleware.AuthCustom(verify), userFun)
118+
r.GET("/user/:id", middleware.AuthCustom(verify), h.GetByID)
119119

120120
r.Run(serverAddr)
121121
}

0 commit comments

Comments
 (0)