aboutsummaryrefslogtreecommitdiff
path: root/http_server.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-12-22 02:57:21 +0700
committerShulhan <ms@kilabit.info>2023-12-22 03:08:04 +0700
commitaa64efe9e8957da6c122f9decaaa78dca83d14ee (patch)
treea6b60b025b369c21d9d4e4efce957c313be38c69 /http_server.go
parentc0d336ca456093b2b7c0b585dbe08f62cbc8ca83 (diff)
downloadawwan-aa64efe9e8957da6c122f9decaaa78dca83d14ee.tar.xz
all: implement HTTP API to stop local or play execution
The HTTP API for stopping execution have the following signature, DELETE /awwan/api/execute?id=<string> If the ID is exist, the execution will be cancelled and return HTTP status 200 with the following body, Content-Type: application/json { "code": 200, } Otherwise it will return HTTP status 404 with error message. References: https://todo.sr.ht/~shulhan/awwan/9
Diffstat (limited to 'http_server.go')
-rw-r--r--http_server.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/http_server.go b/http_server.go
index c45cde2..2a01332 100644
--- a/http_server.go
+++ b/http_server.go
@@ -192,6 +192,19 @@ func (httpd *httpServer) registerEndpoints() (err error) {
return fmt.Errorf("%s: %w", logp, err)
}
+ // Register endpoint to cancel execution.
+
+ err = httpd.RegisterEndpoint(&libhttp.Endpoint{
+ Method: libhttp.RequestMethodDelete,
+ Path: pathAwwanAPIExecute,
+ RequestType: libhttp.RequestTypeJSON,
+ ResponseType: libhttp.ResponseTypeJSON,
+ Call: httpd.ExecuteCancel,
+ })
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
+
// Register Server-sent events to tail the execution state and
// output.
@@ -728,6 +741,43 @@ func (httpd *httpServer) Execute(epr *libhttp.EndpointRequest) (resb []byte, err
return resb, nil
}
+// ExecuteCancel cancel execution by its ID.
+//
+// Request format,
+//
+// DELETE /awwan/api/execute?id=<string>
+//
+// If the ID is exist, the execution will be cancelled and return HTTP
+// status 200 with the following body,
+//
+// Content-Type: application/json
+// {
+// "code": 200,
+// }
+//
+// Otherwise it will return HTTP status 404 with error message.
+func (httpd *httpServer) ExecuteCancel(epr *libhttp.EndpointRequest) (resb []byte, err error) {
+ var (
+ endRes = &libhttp.EndpointResponse{}
+ execID = epr.HttpRequest.Form.Get(paramNameID)
+ ctxDoCancel = httpd.idContextCancel[execID]
+ )
+
+ if ctxDoCancel == nil {
+ endRes.Code = http.StatusNotFound
+ endRes.Message = fmt.Sprintf(`execution ID not found: %q`, execID)
+ return nil, endRes
+ }
+
+ ctxDoCancel()
+
+ endRes.Code = http.StatusOK
+ endRes.Message = fmt.Sprintf(`execution ID %q has been cancelled`, execID)
+
+ resb, err = json.Marshal(endRes)
+ return resb, err
+}
+
// ExecuteTail fetch the latest output of execution using Server-sent
// events.
//