9
9
"net/http"
10
10
"ociswrapper/common"
11
11
"ociswrapper/ocis"
12
- "strings"
13
12
)
14
13
15
14
type BasicResponse struct {
@@ -206,29 +205,15 @@ func CommandHandler(res http.ResponseWriter, req *http.Request) {
206
205
}
207
206
208
207
func OcisServiceHandler (res http.ResponseWriter , req * http.Request ) {
209
- if req .Method != http .MethodPost && req .Method != http .MethodDelete {
210
- sendResponse (res , http .StatusMethodNotAllowed , "Method not allowed" )
211
- return
212
- }
213
-
214
- serviceName := strings .TrimPrefix (req .URL .Path , "/services/" )
215
-
216
- if serviceName == "" {
217
- sendResponse (res , http .StatusUnprocessableEntity , "Service name not specified" )
218
- return
219
- }
220
-
208
+ serviceName := req .PathValue ("service" )
221
209
envMap := []string {fmt .Sprintf ("OCIS_EXCLUDE_RUN_SERVICES=%s" , serviceName )}
222
210
223
211
if req .Method == http .MethodPost {
224
212
// restart oCIS without service that need to start separately
225
213
success , _ := ocis .Restart (envMap )
226
214
if success {
227
- // Clear `EnvConfigs` to prevent persistence of temporary changes
228
- log .Println (fmt .Sprintf ("Environment Config when service Post request has been hit: %s\n " , ocis .EnvConfigs ))
229
-
230
215
var envBody map [string ]interface {}
231
- var envMap []string
216
+ var serviceEnvMap []string
232
217
233
218
if req .Body != nil && req .ContentLength > 0 {
234
219
var err error
@@ -240,29 +225,27 @@ func OcisServiceHandler(res http.ResponseWriter, req *http.Request) {
240
225
}
241
226
242
227
for key , value := range envBody {
243
- envMap = append (envMap , fmt .Sprintf ("%s=%v" , key , value ))
228
+ serviceEnvMap = append (serviceEnvMap , fmt .Sprintf ("%s=%v" , key , value ))
244
229
}
245
230
246
- log .Println (fmt .Sprintf ("serviceName to start: %s\n " , serviceName ))
231
+ log .Println (fmt .Sprintf ("Starting oCIS service %s...... " , serviceName ))
247
232
248
- go ocis .RunOcisService (serviceName , envMap )
233
+ go ocis .StartService (serviceName , serviceEnvMap )
249
234
success , _ := ocis .WaitForConnection ()
250
235
if success {
251
236
sendResponse (res , http .StatusOK , fmt .Sprintf ("oCIS service %s started successfully" , serviceName ))
252
237
return
253
238
}
254
239
}
255
-
256
240
sendResponse (res , http .StatusInternalServerError , fmt .Sprintf ("Failed to restart oCIS without service %s" , serviceName ))
257
- }
258
-
259
- if req .Method == http .MethodDelete {
241
+ } else if req .Method == http .MethodDelete {
260
242
success , message := ocis .StopService (serviceName )
261
243
if success {
262
244
sendResponse (res , http .StatusOK , fmt .Sprintf ("oCIS service %s stopped successfully" , serviceName ))
263
245
} else {
264
246
sendResponse (res , http .StatusInternalServerError , message )
265
247
}
248
+ } else {
249
+ sendResponse (res , http .StatusMethodNotAllowed , "Invalid method requested" )
266
250
}
267
-
268
251
}
0 commit comments