Execute runs a script from the configured queries location.
(w http.ResponseWriter, r *http.Request)
| 31 | |
| 32 | // Execute runs a script from the configured queries location. |
| 33 | func (h *ScriptHandler) Execute(w http.ResponseWriter, r *http.Request) { |
| 34 | vars := mux.Vars(r) |
| 35 | queriesPath := vars["queriesLocation"] |
| 36 | script := vars["script"] |
| 37 | database := vars["database"] |
| 38 | |
| 39 | if database == "" { |
| 40 | database = h.db.GetDatabase() |
| 41 | } |
| 42 | |
| 43 | ctx, cancel := requestContext(r, database) |
| 44 | defer cancel() |
| 45 | |
| 46 | result, err := h.ExecuteScriptQuery(r.WithContext(ctx), queriesPath, script) |
| 47 | if err != nil { |
| 48 | jsonError(w, err.Error(), http.StatusBadRequest) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | if r.Method == "GET" && h.cache != nil { |
| 53 | h.cache.BuntSet(r.URL.String(), string(result)) |
| 54 | } |
| 55 | //nolint |
| 56 | w.Write(result) |
| 57 | } |
| 58 | |
| 59 | // ExecuteScriptQuery runs a script and returns the result bytes. |
| 60 | func (h *ScriptHandler) ExecuteScriptQuery(rq *http.Request, queriesPath string, script string) ([]byte, error) { |