Skip to content

Commit ca6d672

Browse files
authored
feat: Migrate gin to echo (#60)
1 parent a10879d commit ca6d672

10 files changed

Lines changed: 183 additions & 234 deletions

File tree

go.mod

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ require (
1010
github.com/deckarep/golang-set/v2 v2.3.0
1111
github.com/deepmap/oapi-codegen v1.12.4
1212
github.com/getkin/kin-openapi v0.117.0
13-
github.com/gin-contrib/gzip v0.0.6
14-
github.com/gin-gonic/gin v1.9.1
1513
github.com/glebarez/sqlite v1.8.0
1614
github.com/go-resty/resty/v2 v2.7.0
1715
github.com/json-iterator/go v1.1.12
@@ -67,6 +65,7 @@ require (
6765
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
6866
github.com/geoffgarside/ber v1.1.0 // indirect
6967
github.com/gin-contrib/sse v0.1.0 // indirect
68+
github.com/gin-gonic/gin v1.9.1 // indirect
7069
github.com/glebarez/go-sqlite v1.21.1 // indirect
7170
github.com/go-chi/chi/v5 v5.0.8 // indirect
7271
github.com/go-ole/go-ole v1.2.6 // indirect
@@ -118,7 +117,7 @@ require (
118117
github.com/kr/fs v0.1.0 // indirect
119118
github.com/kr/pretty v0.3.1 // indirect
120119
github.com/kylelemons/godebug v1.1.0 // indirect
121-
github.com/labstack/gommon v0.4.0 // indirect
120+
github.com/labstack/gommon v0.4.2 // indirect
122121
github.com/leodido/go-urn v1.2.4 // indirect
123122
github.com/lufia/plan9stats v0.0.0-20230110061619-bbe2e5e100de // indirect
124123
github.com/mailru/easyjson v0.7.7 // indirect
@@ -157,7 +156,6 @@ require (
157156
github.com/spacemonkeygo/monkit/v3 v3.0.19 // indirect
158157
github.com/spf13/cobra v1.8.0 // indirect
159158
github.com/spf13/pflag v1.0.5 // indirect
160-
github.com/stretchr/testify v1.8.4 // indirect
161159
github.com/t3rm1n4l/go-mega v0.0.0-20230228171823-a01a2cda13ca // indirect
162160
github.com/tidwall/match v1.1.1 // indirect
163161
github.com/tidwall/pretty v1.2.1 // indirect
@@ -181,16 +179,16 @@ require (
181179
go.uber.org/atomic v1.10.0 // indirect
182180
go.uber.org/multierr v1.11.0 // indirect
183181
golang.org/x/arch v0.3.0 // indirect
184-
golang.org/x/crypto v0.14.0 // indirect
182+
golang.org/x/crypto v0.23.0 // indirect
185183
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
186184
golang.org/x/mod v0.12.0 // indirect
187-
golang.org/x/net v0.17.0 // indirect
185+
golang.org/x/net v0.25.0 // indirect
188186
golang.org/x/oauth2 v0.7.0 // indirect
189187
golang.org/x/sync v0.3.0 // indirect
190-
golang.org/x/sys v0.14.0 // indirect
191-
golang.org/x/term v0.13.0 // indirect
192-
golang.org/x/text v0.13.0 // indirect
193-
golang.org/x/time v0.3.0 // indirect
188+
golang.org/x/sys v0.20.0 // indirect
189+
golang.org/x/term v0.20.0 // indirect
190+
golang.org/x/text v0.15.0 // indirect
191+
golang.org/x/time v0.5.0 // indirect
194192
golang.org/x/tools v0.12.0 // indirect
195193
google.golang.org/api v0.114.0 // indirect
196194
google.golang.org/appengine v1.6.7 // indirect

go.sum

Lines changed: 14 additions & 38 deletions
Large diffs are not rendered by default.

route/v1.go

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,59 @@ package route
22

33
import (
44
"crypto/ecdsa"
5-
"os"
5+
"net/http"
6+
"strconv"
67

78
"github.com/IceWhaleTech/CasaOS-Common/external"
8-
"github.com/IceWhaleTech/CasaOS-Common/middleware"
99
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"
1010
"github.com/IceWhaleTech/CasaOS-LocalStorage/pkg/config"
1111
v1 "github.com/IceWhaleTech/CasaOS-LocalStorage/route/v1"
12-
13-
"github.com/gin-contrib/gzip"
14-
"github.com/gin-gonic/gin"
12+
"github.com/labstack/echo/v4"
13+
echo_middleware "github.com/labstack/echo/v4/middleware"
1514
)
1615

17-
func InitV1Router() *gin.Engine {
16+
func InitV1Router() http.Handler {
1817
// check if environment variable is set
19-
ginMode, success := os.LookupEnv(gin.EnvGinMode)
20-
if !success {
21-
ginMode = gin.ReleaseMode
22-
}
23-
gin.SetMode(ginMode)
18+
e := echo.New()
19+
e.Use((echo_middleware.CORSWithConfig(echo_middleware.CORSConfig{
20+
AllowOrigins: []string{"*"},
21+
AllowMethods: []string{echo.POST, echo.GET, echo.OPTIONS, echo.PUT, echo.DELETE},
22+
AllowHeaders: []string{echo.HeaderAuthorization, echo.HeaderContentLength, echo.HeaderXCSRFToken, echo.HeaderContentType, echo.HeaderAccessControlAllowOrigin, echo.HeaderAccessControlAllowHeaders, echo.HeaderAccessControlAllowMethods, echo.HeaderConnection, echo.HeaderOrigin, echo.HeaderXRequestedWith},
23+
ExposeHeaders: []string{echo.HeaderContentLength, echo.HeaderAccessControlAllowOrigin, echo.HeaderAccessControlAllowHeaders},
24+
MaxAge: 172800,
25+
AllowCredentials: true,
26+
})))
2427

25-
r := gin.New()
26-
r.Use(gin.Recovery())
27-
r.Use(middleware.Cors())
28-
r.Use(gzip.Gzip(gzip.DefaultCompression))
29-
if ginMode != gin.ReleaseMode {
30-
r.Use(middleware.WriteLog())
31-
}
32-
r.GET("/v1/recover/:type", v1.GetRecoverStorage)
33-
v1Group := r.Group("/v1")
28+
e.Use(echo_middleware.Gzip())
29+
e.Use(echo_middleware.Recover())
30+
e.Use(echo_middleware.Logger())
31+
32+
// r.GET("/v1/recover/:type", v1.GetRecoverStorage)
33+
v1Group := e.Group("/v1")
3434

35-
v1Group.Use(jwt.JWT(
36-
func() (*ecdsa.PublicKey, error) {
37-
return external.GetPublicKey(config.CommonInfo.RuntimePath)
35+
v1Group.Use(echo_middleware.JWTWithConfig(echo_middleware.JWTConfig{
36+
Skipper: func(c echo.Context) bool {
37+
return c.RealIP() == "::1" || c.RealIP() == "127.0.0.1"
3838
},
39-
))
39+
ParseTokenFunc: func(token string, c echo.Context) (interface{}, error) {
40+
valid, claims, err := jwt.Validate(token, func() (*ecdsa.PublicKey, error) { return external.GetPublicKey(config.CommonInfo.RuntimePath) })
41+
if err != nil || !valid {
42+
return nil, echo.ErrUnauthorized
43+
}
4044

45+
c.Request().Header.Set("user_id", strconv.Itoa(claims.ID))
46+
47+
return claims, nil
48+
},
49+
TokenLookupFuncs: []echo_middleware.ValuesExtractor{
50+
func(c echo.Context) ([]string, error) {
51+
if len(c.Request().Header.Get(echo.HeaderAuthorization)) > 0 {
52+
return []string{c.Request().Header.Get(echo.HeaderAuthorization)}, nil
53+
}
54+
return []string{c.QueryParam("token")}, nil
55+
},
56+
},
57+
}))
4158
{
4259
v1DisksGroup := v1Group.Group("/disks")
4360
v1DisksGroup.Use()
@@ -79,5 +96,5 @@ func InitV1Router() *gin.Engine {
7996
}
8097
}
8198

82-
return r
99+
return e
83100
}

route/v1/cloud.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,30 @@ import (
1010
"github.com/IceWhaleTech/CasaOS-LocalStorage/drivers/google_drive"
1111
"github.com/IceWhaleTech/CasaOS-LocalStorage/pkg/utils/httper"
1212
"github.com/IceWhaleTech/CasaOS-LocalStorage/service"
13-
"github.com/gin-gonic/gin"
13+
"github.com/labstack/echo/v4"
1414
)
1515

16-
func ListStorages(c *gin.Context) {
16+
func ListStorages(ctx echo.Context) error {
1717
// var req model.PageReq
18-
// if err := c.ShouldBind(&req); err != nil {
19-
// c.JSON(common_err.SUCCESS, model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.CLIENT_ERROR), Data: err.Error()})
18+
// if err := ctx.Bind(&req); err != nil {
19+
// return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.CLIENT_ERROR), Data: err.Error()})
2020
// return
2121
// }
2222
// req.Validate()
2323

24-
//logger.Info("ListStorages", zap.Any("req", req))
25-
//storages, total, err := service.MyService.Storage().GetStorages(req.Page, req.PerPage)
24+
// logger.Info("ListStorages", zap.Any("req", req))
25+
// storages, total, err := service.MyService.Storage().GetStorages(req.Page, req.PerPage)
2626
// if err != nil {
27-
// c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
27+
// return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
2828
// return
2929
// }
30-
// c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: model.PageResp{
30+
// return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: model.PageResp{
3131
// Content: storages,
3232
// Total: total,
3333
// }})
3434
r, err := service.MyService.Storage().GetStorages()
35-
3635
if err != nil {
37-
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
38-
return
36+
return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
3937
}
4038

4139
for i := 0; i < len(r.MountPoints); i++ {
@@ -59,25 +57,23 @@ func ListStorages(c *gin.Context) {
5957
})
6058
}
6159

62-
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: list})
60+
return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: list})
6361
}
6462

65-
func UmountStorage(c *gin.Context) {
63+
func UmountStorage(ctx echo.Context) error {
6664
json := make(map[string]string)
67-
c.ShouldBind(&json)
65+
ctx.Bind(&json)
6866
mountPoint := json["mount_point"]
6967
if mountPoint == "" {
70-
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.CLIENT_ERROR), Data: "mount_point is empty"})
71-
return
68+
return ctx.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.CLIENT_ERROR), Data: "mount_point is empty"})
7269
}
7370
err := service.MyService.Storage().UnmountStorage(mountPoint)
7471
if err != nil {
75-
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
76-
return
72+
return ctx.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
7773
}
7874
service.MyService.Storage().DeleteConfigByName(strings.ReplaceAll(mountPoint, "/mnt/", ""))
7975
if fs, err := os.ReadDir(mountPoint); err == nil && len(fs) == 0 {
8076
os.RemoveAll(mountPoint)
8177
}
82-
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: "success"})
78+
return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: "success"})
8379
}

route/v1/disk.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/IceWhaleTech/CasaOS-LocalStorage/common"
1313
model1 "github.com/IceWhaleTech/CasaOS-LocalStorage/model"
1414
"github.com/IceWhaleTech/CasaOS-LocalStorage/service"
15-
"github.com/gin-gonic/gin"
15+
"github.com/labstack/echo/v4"
1616
"github.com/shirou/gopsutil/v3/disk"
1717
"go.uber.org/zap"
1818
)
@@ -36,14 +36,13 @@ type StorageMessage struct {
3636
// @Security ApiKeyAuth
3737
// @Success 200 {string} string "ok"
3838
// @Router /disk/list [get]
39-
func GetDiskList(c *gin.Context) {
39+
func GetDiskList(ctx echo.Context) error {
4040
blkList := service.MyService.Disk().LSBLK(false)
4141

4242
dbList, err := service.MyService.Disk().GetSerialAllFromDB()
4343
if err != nil {
4444
logger.Error("error when getting all volumes from database", zap.Error(err))
45-
c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
46-
return
45+
return ctx.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
4746
}
4847

4948
part := make(map[string]int64, len(dbList))
@@ -153,7 +152,7 @@ func GetDiskList(c *gin.Context) {
153152
"avail": avail,
154153
}
155154

156-
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
155+
return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
157156
}
158157

159158
// @Summary disk list
@@ -164,29 +163,26 @@ func GetDiskList(c *gin.Context) {
164163
// @Success 200 {string} string "ok"
165164
// @Router /disk/list [get]
166165

167-
func DeleteDisksUmount(c *gin.Context) {
166+
func DeleteDisksUmount(ctx echo.Context) error {
168167
js := make(map[string]string)
169-
if err := c.ShouldBind(&js); err != nil {
170-
c.JSON(http.StatusBadRequest, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: err.Error()})
171-
return
168+
if err := ctx.Bind(&js); err != nil {
169+
return ctx.JSON(http.StatusBadRequest, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: err.Error()})
172170
}
173171

174172
// requires password from user to confirm the action
175173
// if claims, err := jwt.ParseToken(c.GetHeader("Authorization"), false); err != nil || encryption.GetMD5ByStr(js["password"]) != claims.Password {
176-
// c.JSON(http.StatusUnauthorized, model.Result{Success: common_err.PWD_INVALID, Message: common_err.GetMsg(common_err.PWD_INVALID)})
174+
// return ctx.JSON(http.StatusUnauthorized, model.Result{Success: common_err.PWD_INVALID, Message: common_err.GetMsg(common_err.PWD_INVALID)})
177175
// return
178176
// }
179177

180178
path := js["path"]
181179

182180
if len(path) == 0 {
183-
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
184-
return
181+
return ctx.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
185182
}
186183

187184
if _, ok := diskMap[path]; ok {
188-
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.DISK_BUSYING, Message: common_err.GetMsg(common_err.DISK_BUSYING)})
189-
return
185+
return ctx.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.DISK_BUSYING, Message: common_err.GetMsg(common_err.DISK_BUSYING)})
190186
}
191187

192188
diskInfo := service.MyService.Disk().GetDiskInfo(path)
@@ -197,8 +193,7 @@ func DeleteDisksUmount(c *gin.Context) {
197193
}
198194
for _, v := range diskInfo.Children {
199195
if err := service.MyService.Disk().UmountPointAndRemoveDir(v); err != nil {
200-
c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.REMOVE_MOUNT_POINT_ERROR, Message: err.Error()})
201-
return
196+
return ctx.JSON(http.StatusInternalServerError, model.Result{Success: common_err.REMOVE_MOUNT_POINT_ERROR, Message: err.Error()})
202197
}
203198

204199
// delete data
@@ -230,23 +225,22 @@ func DeleteDisksUmount(c *gin.Context) {
230225
}
231226
}()
232227

233-
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: path})
228+
return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: path})
234229
}
235-
func GetDiskSize(c *gin.Context) {
236-
path := c.Query("path")
230+
231+
func GetDiskSize(ctx echo.Context) error {
232+
path := ctx.QueryParam("path")
237233
if len(path) == 0 {
238-
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
239-
return
234+
return ctx.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
240235
}
241236
p, err := disk.Usage(path)
242237
if err != nil {
243-
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
244-
return
238+
return ctx.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
245239
}
246240
data := map[string]interface{}{
247241
"path": path,
248242
"free": p.Free,
249243
"used": p.Used,
250244
}
251-
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
245+
return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
252246
}

route/v1/driver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"github.com/IceWhaleTech/CasaOS-Common/model"
55
"github.com/IceWhaleTech/CasaOS-Common/utils/common_err"
66
"github.com/IceWhaleTech/CasaOS-LocalStorage/internal/op"
7-
"github.com/gin-gonic/gin"
7+
"github.com/labstack/echo/v4"
88
)
99

10-
func ListDriverInfo(c *gin.Context) {
11-
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: op.GetDriverInfoMap()})
10+
func ListDriverInfo(ctx echo.Context) error {
11+
return ctx.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: op.GetDriverInfoMap()})
1212
}

0 commit comments

Comments
 (0)