diff --git a/client/helpers/ajax.js b/client/helpers/ajax.js index 599e99d4..f965b4ee 100644 --- a/client/helpers/ajax.js +++ b/client/helpers/ajax.js @@ -1,4 +1,4 @@ -export function http_get(url, type = "json") { +export function http_get(url, type = "json", params) { return new Promise((done, err) => { const xhr = new XMLHttpRequest(); xhr.open("GET", url, true); @@ -30,6 +30,12 @@ export function http_get(url, type = "json") { xhr.onerror = function() { handle_error_response(xhr, err); }; + if (params && params.abort) { + params.abort.signal.onabort = () => { + xhr.abort(); + handle_error_response(xhr, err); + }; + } }); } @@ -152,10 +158,17 @@ function handle_error_response(xhr, err) { if (navigator.onLine === false) { err({ message: "Connection Lost", code: "NO_INTERNET" }); } else if (xhr.status === 0 && xhr.responseText === "") { - err({ - message: "Service unavailable, if the problem persist, contact your administrator", - code: "INTERNAL_SERVER_ERROR", - }); + switch(xhr.readyState) { + case XMLHttpRequest.DONE: + case XMLHttpRequest.UNSENT: + err({ message: "aborted", code: "ABORTED" }); + break + default: + err({ + message: "Service unavailable, if the problem persist, contact your administrator", + code: "INTERNAL_SERVER_ERROR", + }); + } } else if (xhr.status === 500) { err({ message: message || "Oups something went wrong with our servers", diff --git a/server/common/app.go b/server/common/app.go index 1615e1cd..0be4c58b 100644 --- a/server/common/app.go +++ b/server/common/app.go @@ -1,8 +1,13 @@ package common +import ( + "context" +) + type App struct { Backend IBackend Body map[string]interface{} Session map[string]string Share Share + Context context.Context } diff --git a/server/common/types.go b/server/common/types.go index a147450a..758945a8 100644 --- a/server/common/types.go +++ b/server/common/types.go @@ -46,7 +46,7 @@ type ISearch interface { } type IAuditPlugin interface { - Query(searchParams map[string]string) (AuditQueryResult, error) + Query(ctx *App, searchParams map[string]string) (AuditQueryResult, error) } type AuditQueryResult struct { Form *Form `json:"form"` diff --git a/server/ctrl/admin.go b/server/ctrl/admin.go index 6cf1eae1..c6d2c7a6 100644 --- a/server/ctrl/admin.go +++ b/server/ctrl/admin.go @@ -147,7 +147,7 @@ func FetchAuditHandler(ctx *App, res http.ResponseWriter, req *http.Request) { } searchParams[key] = element[0] } - result, err := plg.Query(searchParams) + result, err := plg.Query(ctx, searchParams) if err != nil { SendErrorResult(res, err) return diff --git a/server/middleware/index.go b/server/middleware/index.go index 132a0f6b..00234854 100644 --- a/server/middleware/index.go +++ b/server/middleware/index.go @@ -12,13 +12,14 @@ import ( type Middleware func(func(*App, http.ResponseWriter, *http.Request)) func(*App, http.ResponseWriter, *http.Request) func NewMiddlewareChain(fn func(*App, http.ResponseWriter, *http.Request), m []Middleware, app App) http.HandlerFunc { + return func(res http.ResponseWriter, req *http.Request) { var resw ResponseWriter = NewResponseWriter(res) var f func(*App, http.ResponseWriter, *http.Request) = fn - for i := len(m) - 1; i >= 0; i-- { f = m[i](f) } + app.Context = req.Context() f(&app, &resw, req) if req.Body != nil { req.Body.Close() diff --git a/server/model/audit.go b/server/model/audit.go index 76733254..6bfef863 100644 --- a/server/model/audit.go +++ b/server/model/audit.go @@ -57,7 +57,7 @@ var AuditForm Form = Form{ type SimpleAudit struct{} -func (this SimpleAudit) Query(searchParams map[string]string) (AuditQueryResult, error) { +func (this SimpleAudit) Query(ctx *App, searchParams map[string]string) (AuditQueryResult, error) { return AuditQueryResult{ Form: &AuditForm, RenderHTML: `