From a18faee98e50fa45db1263cd2b284351b40bf24c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sim=C3=A3o?= Date: Wed, 4 Nov 2020 13:14:24 -0300 Subject: [PATCH] Refactor to improve later; fix comments in single line --- test/goapi/api.go | 63 +++++++++++++++++++++++++++ test/tscli/api.ts | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 test/goapi/api.go create mode 100644 test/tscli/api.ts diff --git a/test/goapi/api.go b/test/goapi/api.go new file mode 100644 index 0000000..021d68c --- /dev/null +++ b/test/goapi/api.go @@ -0,0 +1,63 @@ +package goapi + +import ( + "context" + "encoding/json" + "net/http" + "strings" +) + +type API struct { + Mux *http.ServeMux + Perms map[string]string +} + +func (a *API) GetPerm(r *http.Request) string { + return a.Perms[r.Method+"_"+strings.Split(r.RequestURI, "?")[0]] +} + +func Init() API { + mux := &http.ServeMux{} + + ret := API{ + Mux: mux, + Perms: make(map[string]string), + } + + mux.HandleFunc("", func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "POST": + h_SomeAPI(w, r) + default: + http.Error(w, "Method not allowed", 500) + } + }) + return ret +} + +func h_SomeAPI(w http.ResponseWriter, r *http.Request) { + + ctx := r.Context() + ctx = context.WithValue(r.Context(), "REQ", r) + ctx = context.WithValue(ctx, "RES", w) + req := &ARequestStruct{} + if r.Method != http.MethodGet && r.Method != http.MethodHead { + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + } + + res, err := SomeAPI(ctx, req) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + w.Header().Add("Content-Type", "Application/json") + err = json.NewEncoder(w).Encode(res) + if err != nil { + http.Error(w, err.Error(), 500) + return + } +} diff --git a/test/tscli/api.ts b/test/tscli/api.ts new file mode 100644 index 0000000..e6ed067 --- /dev/null +++ b/test/tscli/api.ts @@ -0,0 +1,107 @@ +//#region Base + + +var apibase=""; + +export function SetAPIBase(s:string){ + apibase=s; +} + +export function GetAPIBase(): string{ + return apibase; +} + +let REGEX_DATE = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)(Z|([+\-])(\d{2}):(\d{2}))$/ + +type HTMLMethod = "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "TRACE" + +async function Invoke(path: string, method: HTMLMethod, body?: any): Promise { + let jbody = undefined + let init = {method: method, mode: "cors", credentials: "include", withCredentials: true} + if (!!body) { + let jbody = JSON.stringify(body) + //@ts-ignore + init.body = jbody + } + if (apibase.endsWith("/") && path.startsWith("/")) { + path = path.substr(1, path.length) + } + let fpath = (apibase + path) + //@ts-ignore + let res = await fetch(fpath, init) + + return res +} + +async function InvokeJSON(path: string, method: HTMLMethod, body?: any): Promise { + + let txt = await InvokeTxt(path, method, body) + if (txt == "") { + txt = "{}" + } + let ret = JSON.parse(txt, (k: string, v: string) => { + if (REGEX_DATE.exec(v)) { + return new Date(v) + } + return v + }) + + return ret +} + +async function InvokeTxt(path: string, method: HTMLMethod, body?: any): Promise { + //@ts-ignore + let res = await Invoke(path, method, body) + + let txt = await res.text() + + if (res.status < 200 || res.status >= 400) { + // webix.alert("API Error:" + res.status + "\n" + txt) + console.error("API Error:" + res.status + "\n" + txt) + let e = new Error(txt) + throw e + } + + return txt +} + +async function InvokeOk(path: string, method: HTMLMethod, body?: any): Promise { + + //@ts-ignore + let res = await Invoke(path, method, body) + + let txt = await res.text() + if (res.status >= 400) { + console.error("API Error:" + res.status + "\n" + txt) + return false + } + return true +} + +//#endregion + +//#region Types +export interface ARequestStruct { + c:Date + d:string + a:string + b:number +} + +export interface AResponseStruct { + a:string + b:number + c:Date + d:string +} + +//#endregion + +//#region Methods +/** +SomeAPI*/ +export async function SomeAPI(req:ARequestStruct):Promise{ + return InvokeJSON("","POST",req) +} + +//#endregion