@@ -46,6 +46,10 @@ func New(vz *vz.VirtualMachine, vmC *config.VirtualMachine, log *logger.Context,
46
46
}
47
47
}
48
48
49
+ type powerSaveModeBody struct {
50
+ Enable bool `json:"enable"`
51
+ }
52
+
49
53
func (s * Restful ) mux () * http.ServeMux {
50
54
mux := http .NewServeMux ()
51
55
mux .HandleFunc ("/info" , func (w http.ResponseWriter , r * http.Request ) {
@@ -104,21 +108,20 @@ func (s *Restful) mux() *http.ServeMux {
104
108
http .Error (w , err .Error (), http .StatusInternalServerError )
105
109
}
106
110
})
107
- mux .HandleFunc ("/start- power-save-mode" , func (w http.ResponseWriter , r * http.Request ) {
111
+ mux .HandleFunc ("/power-save-mode" , func (w http.ResponseWriter , r * http.Request ) {
108
112
if r .Method != http .MethodPut {
109
113
http .Error (w , "put only" , http .StatusBadRequest )
110
114
return
111
115
}
112
116
113
- s .startPowerSaveMode ()
114
- })
115
- mux .HandleFunc ("/stop-power-save-mode" , func (w http.ResponseWriter , r * http.Request ) {
116
- if r .Method != http .MethodPut {
117
- http .Error (w , "put only" , http .StatusBadRequest )
117
+ var body powerSaveModeBody
118
+ if err := json .NewDecoder (r .Body ).Decode (& body ); err != nil {
119
+ s .log .Warnf ("Failed to decode request body: %v" , err )
120
+ http .Error (w , "failed to decode request body" , http .StatusBadRequest )
118
121
return
119
122
}
120
123
121
- s .stopPowerSaveMode ( )
124
+ s .powerSaveMode ( body . Enable )
122
125
})
123
126
124
127
return mux
@@ -200,12 +203,7 @@ func (s *Restful) stop() error {
200
203
return err
201
204
}
202
205
203
- func (s * Restful ) startPowerSaveMode () {
204
- s .log .Info ("request /startPowerSaveMode" )
205
- s .opt .PowerSaveMode = true
206
- }
207
-
208
- func (s * Restful ) stopPowerSaveMode () {
209
- s .log .Info ("request /stopPowerSaveMode" )
210
- s .opt .PowerSaveMode = false
206
+ func (s * Restful ) powerSaveMode (enable bool ) {
207
+ s .log .Info ("request /powerSaveMode" )
208
+ s .opt .PowerSaveMode = enable
211
209
}
0 commit comments