diff options
| author | Shulhan <ms@kilabit.info> | 2023-12-22 02:04:11 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-12-22 03:07:52 +0700 |
| commit | c0d336ca456093b2b7c0b585dbe08f62cbc8ca83 (patch) | |
| tree | cb313f0352a91562cc99ad115d62bad1447dc2b9 /http_server.go | |
| parent | 66a18a789edaa35dfa4446846eeeffabfee0b269 (diff) | |
| download | awwan-c0d336ca456093b2b7c0b585dbe08f62cbc8ca83.tar.xz | |
all: add [context.Context] to Local and Play
Passing context allow the command Local or Play to be cancelled when
running in asynchronous mode, in this case when awwan run with WUI.
Diffstat (limited to 'http_server.go')
| -rw-r--r-- | http_server.go | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/http_server.go b/http_server.go index 70dc7a8..c45cde2 100644 --- a/http_server.go +++ b/http_server.go @@ -5,6 +5,7 @@ package awwan import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -49,6 +50,10 @@ type httpServer struct { // idExecRes contains the execution ID and its response. idExecRes map[string]*ExecResponse + // idContextCancel contains the execution ID and its context + // cancellation function. + idContextCancel map[string]context.CancelFunc + aww *Awwan memfsBase *memfs.MemFS // The files caches. @@ -63,7 +68,8 @@ func newHTTPServer(aww *Awwan, address string) (httpd *httpServer, err error) { ) httpd = &httpServer{ - idExecRes: make(map[string]*ExecResponse), + idExecRes: make(map[string]*ExecResponse), + idContextCancel: make(map[string]context.CancelFunc), aww: aww, baseDir: aww.BaseDir, @@ -701,11 +707,20 @@ func (httpd *httpServer) Execute(epr *libhttp.EndpointRequest) (resb []byte, err httpd.idExecRes[execRes.ID] = execRes + var ( + ctx = context.Background() + ctxDoCancel context.CancelFunc + ) + + ctx, ctxDoCancel = context.WithCancel(ctx) + + httpd.idContextCancel[execRes.ID] = ctxDoCancel + go func() { if req.Mode == CommandModeLocal { - err = httpd.aww.Local(req) + err = httpd.aww.Local(ctx, req) } else { - err = httpd.aww.Play(req) + err = httpd.aww.Play(ctx, req) } execRes.end(err) }() |
