Skip to content

Commit f7dfea6

Browse files
authored
add ChangePort() to common (#11)
1 parent b972ec8 commit f7dfea6

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
LEGACY_WITHOUT_VERSION v0.3.6-alpha3
2-
v0.3.5 v0.3.6-alpha3
1+
LEGACY_WITHOUT_VERSION v0.3.6-alpha4
2+
v0.3.5 v0.3.6-alpha4

common/management.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@ import (
1515
const (
1616
ManagementURLFilename = "management.url"
1717
StaticURLFilename = "static.url"
18-
APIPath = "/v1/gateway/routes"
18+
APIGatewayRoutes = "/v1/gateway/routes"
19+
APIGatewayPort = "/v1/gateway/port"
1920
)
2021

2122
type ManagementService interface {
2223
CreateRoute(route *Route) error
24+
ChangePort(request *ChangePortRequest) error
2325
}
2426

2527
type managementService struct {
2628
address string
2729
}
2830

2931
func (m *managementService) CreateRoute(route *Route) error {
30-
url := strings.TrimSuffix(m.address, "/") + "/" + strings.TrimPrefix(APIPath, "/")
32+
url := strings.TrimSuffix(m.address, "/") + "/" + strings.TrimPrefix(APIGatewayRoutes, "/")
3133
body, err := json.Marshal(route)
3234
if err != nil {
3335
return err
@@ -45,10 +47,35 @@ func (m *managementService) CreateRoute(route *Route) error {
4547
return nil
4648
}
4749

50+
func (m *managementService) ChangePort(request *ChangePortRequest) error {
51+
url := strings.TrimSuffix(m.address, "/") + "/" + strings.TrimPrefix(APIGatewayPort, "/")
52+
body, err := json.Marshal(request)
53+
if err != nil {
54+
return err
55+
}
56+
57+
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(body))
58+
if err != nil {
59+
return err
60+
}
61+
62+
req.Header.Set("Content-Type", "application/json")
63+
response, err := http.DefaultClient.Do(req)
64+
if err != nil {
65+
return err
66+
}
67+
68+
if response.StatusCode != http.StatusOK {
69+
return errors.New("failed to change port (status code: " + fmt.Sprint(response.StatusCode) + ")")
70+
}
71+
72+
return nil
73+
}
74+
4875
func NewManagementService(RuntimePath string) (ManagementService, error) {
49-
managementAddresFile := filepath.Join(RuntimePath, ManagementURLFilename)
76+
managementAddressFile := filepath.Join(RuntimePath, ManagementURLFilename)
5077

51-
buf, err := ioutil.ReadFile(managementAddresFile)
78+
buf, err := ioutil.ReadFile(managementAddressFile)
5279
if err != nil {
5380
return nil, err
5481
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/IceWhaleTech/CasaOS-Gateway
33
go 1.19
44

55
require (
6-
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220829003259-cf7e89c30d78
6+
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220830042034-a4df79c84ce6
77
github.com/gin-contrib/gzip v0.0.6
88
github.com/gin-gonic/gin v1.8.1
99
github.com/go-playground/assert/v2 v2.0.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
3939
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4040
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
4141
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
42-
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220829003259-cf7e89c30d78 h1:Cxy+bTjqpl0txx6gn2/D7O/E6G2SW5c6Ve7RXUin5K4=
43-
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220829003259-cf7e89c30d78/go.mod h1:5sqNKg5cEH7IUnCklLSTrVoGx1dMBhm9DFDsCYVPvPQ=
42+
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220830042034-a4df79c84ce6 h1:ICitt/XvD1oh3BTdQbzV95G48TFY/aiZr55iA8dMD8M=
43+
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220830042034-a4df79c84ce6/go.mod h1:5sqNKg5cEH7IUnCklLSTrVoGx1dMBhm9DFDsCYVPvPQ=
4444
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
4545
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
4646
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=

route/management_route.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ func (m *ManagementRoute) buildV1RouteGroup(v1Group *gin.RouterGroup) {
8888
ctx.Status(http.StatusCreated)
8989
})
9090

91-
v1GatewayGroup.GET("/port", jwt.JWT(), func(ctx *gin.Context) {
91+
v1GatewayGroup.GET("/port", jwt.ExceptLocalhost(), func(ctx *gin.Context) {
9292
ctx.JSON(http.StatusOK, model.Result{
9393
Success: common_err.SUCCESS,
9494
Message: common_err.GetMsg(common_err.SUCCESS),
9595
Data: m.management.GetGatewayPort(),
9696
})
9797
})
9898

99-
v1GatewayGroup.PUT("/port", jwt.JWT(), func(ctx *gin.Context) {
99+
v1GatewayGroup.PUT("/port", jwt.ExceptLocalhost(), func(ctx *gin.Context) {
100100
var request *common.ChangePortRequest
101101

102102
if err := ctx.ShouldBindJSON(&request); err != nil {

0 commit comments

Comments
 (0)