feat: add start endpoint

This commit is contained in:
Saw-jan
2024-07-05 15:08:45 +05:45
parent 3306a1f277
commit 733f1852ea
3 changed files with 28 additions and 1 deletions

View File

@@ -138,6 +138,13 @@ func Restart(envMap map[string]any) (bool, string) {
return WaitForConnection()
}
func IsOcisRunning() bool {
if cmd != nil {
return cmd.Process.Pid > 0
}
return false
}
func waitAllServices(startTime time.Time, timeout time.Duration) {
timeoutS := timeout * time.Second

View File

@@ -5,6 +5,7 @@ import (
"errors"
"io"
"net/http"
"ociswrapper/common"
"ociswrapper/ocis"
)
@@ -115,7 +116,7 @@ func RollbackHandler(res http.ResponseWriter, req *http.Request) {
}
func StopOcisHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
if req.Method != http.MethodPost {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
return
}
@@ -124,6 +125,24 @@ func StopOcisHandler(res http.ResponseWriter, req *http.Request) {
sendResponse(res, success, message)
}
func StartOcisHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if ocis.IsOcisRunning() {
sendResponse(res, false, "oCIS server is already running")
return
}
common.Wg.Add(1)
go ocis.Start(nil)
success, message := ocis.WaitForConnection()
sendResponse(res, success, message)
}
func CommandHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)

View File

@@ -26,6 +26,7 @@ func Start(port string) {
mux.HandleFunc("/rollback", handlers.RollbackHandler)
mux.HandleFunc("/command", handlers.CommandHandler)
mux.HandleFunc("/stop", handlers.StopOcisHandler)
mux.HandleFunc("/start", handlers.StartOcisHandler)
httpServer.Handler = mux