mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-24 05:51:33 -05:00
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"ociswrapper/ocis"
|
|
)
|
|
|
|
func parseJsonBody(reqBody io.ReadCloser) (map[string]any, error) {
|
|
body, _ := io.ReadAll(reqBody)
|
|
|
|
if len(body) == 0 || !json.Valid(body) {
|
|
return nil, errors.New("Invalid json data")
|
|
}
|
|
|
|
var bodyMap map[string]any
|
|
json.Unmarshal(body, &bodyMap)
|
|
|
|
return bodyMap, nil
|
|
}
|
|
|
|
func sendResponse(res http.ResponseWriter, ocisStatus bool) {
|
|
resBody := make(map[string]string)
|
|
|
|
if ocisStatus {
|
|
res.WriteHeader(http.StatusOK)
|
|
resBody["status"] = "OK"
|
|
resBody["message"] = "oCIS server is running"
|
|
} else {
|
|
res.WriteHeader(http.StatusInternalServerError)
|
|
resBody["status"] = "ERROR"
|
|
resBody["message"] = "Unable to start oCIS server"
|
|
}
|
|
res.Header().Set("Content-Type", "application/json")
|
|
|
|
jsonResponse, _ := json.Marshal(resBody)
|
|
res.Write(jsonResponse)
|
|
}
|
|
|
|
func SetEnvHandler(res http.ResponseWriter, req *http.Request) {
|
|
if req.Method != http.MethodPut {
|
|
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
environments, err := parseJsonBody(req.Body)
|
|
if err != nil {
|
|
http.Error(res, "Bad request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
ocisStatus := ocis.Restart(environments)
|
|
|
|
sendResponse(res, ocisStatus)
|
|
}
|
|
|
|
func RollbackHandler(res http.ResponseWriter, req *http.Request) {
|
|
if req.Method != http.MethodDelete {
|
|
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
ocisStatus := ocis.Restart(nil)
|
|
|
|
sendResponse(res, ocisStatus)
|
|
}
|