multiple improvements

This commit is contained in:
2021-01-30 06:53:34 -03:00
parent 517aa43668
commit fa16e16e9b
19 changed files with 993 additions and 403 deletions
+66
View File
@@ -0,0 +1,66 @@
basepath: ""
host: ""
types:
AStr:
name: AStr
desc: ""
fields:
A:
type: string
array: false
desc: ""
map: false
mapkey: ""
mapval: ""
B:
type: string
array: true
desc: ""
map: false
mapkey: ""
mapval: ""
col: ""
methods:
SomeAPI:
desc: SomeAPI
verb: PUT
path: /someapi
perm: ASD
reqtype:
typename: AStr
ispointer: true
isarray: false
restype:
typename: AStr
ispointer: true
isarray: true
raw: false
SomeAPI2:
desc: SomeAPI2
verb: POST
path: /someapi
perm: ""
reqtype:
typename: AStr
ispointer: true
isarray: false
restype:
typename: AStr
ispointer: true
isarray: true
raw: false
SomeAPI3:
desc: SomeAPI3
verb: POST
path: /someapi3
perm: ""
reqtype:
typename: AStr
ispointer: true
isarray: false
restype:
typename: AStr
ispointer: true
isarray: true
raw: false
namespace: ""
-63
View File
@@ -1,63 +0,0 @@
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("/someapi", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
h_SomeAPI2(w, r)
default:
http.Error(w, "Method not allowed", 500)
}
})
return ret
}
func h_SomeAPI2(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(r.Context(), "REQ", r)
ctx = context.WithValue(ctx, "RES", w)
req := &AStr{}
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 := SomeAPI2(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
}
}
+127
View File
@@ -0,0 +1,127 @@
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("/someapi", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
h_SomeAPI2(w, r)
case "PUT":
h_SomeAPI(w, r)
default:
http.Error(w, "Method not allowed", 500)
}
})
mux.HandleFunc("/someapi3", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
h_SomeAPI3(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 := &Noop{}
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
}
}
func h_SomeAPI2(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(r.Context(), "REQ", r)
ctx = context.WithValue(ctx, "RES", w)
req := &AStr2{}
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 := SomeAPI2(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
}
}
func h_SomeAPI3(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(r.Context(), "REQ", r)
ctx = context.WithValue(ctx, "RES", w)
req := &AStr{}
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 := SomeAPI3(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
}
}
+51 -25
View File
@@ -2,42 +2,68 @@ package goapi
import (
"context"
"time"
)
//
///*@API*/
//type ARequestStruct struct {
// A *string `json:"a"`
// B int64
// C time.Time
// D *string
//}
//
///*@API*/
//type AResponseStruct struct {
// A string
// B int64
// C time.Time
// D *string
//}
//
///*
//@API
//@PATH: /someapi
//*/
//func SomeAPI(ctx context.Context, a *ARequestStruct) (*AResponseStruct, error) {
// return nil, nil
//}
/*@API*/
type ARequestStruct struct {
A *string `json:"SUPERCALIFRAGILISPEALIDOUX"`
B int64 `json:"bcd"`
C time.Time
D *string
}
/*@API*/
type AResponseStruct struct {
A string
B int64
C time.Time
D *string
}
/*@API*/
type AStr struct {
A string
Country string
City string
HouseNumber int64
IsCondo bool
SomeWeirdTest string `json:"SUPERCALIFRAGILISPEALIDOUX"`
}
/*@API*/
type AStr2 struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Products []string
Addresses map[string]AStr
}
/*@API*/
type Noop struct{}
/*
@API
@PATH: /someapi
@PERM: ASD
@VERB: PUT
*/
func SomeAPI(ctx context.Context, a *Noop) ([]*AStr, error) {
return nil, nil
}
/*
@API
@PATH: /someapi
*/
func SomeAPI2(ctx context.Context, a *AStr) ([]*AStr, error) {
func SomeAPI2(ctx context.Context, a *AStr2) ([]*AStr, error) {
return nil, nil
}
/*
@API
@PATH: /someapi3
*/
func SomeAPI3(ctx context.Context, a *AStr) ([]*AStr, error) {
return nil, nil
}
+69 -5
View File
@@ -3,7 +3,10 @@ package gocli
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"time"
)
var Basepath string = ""
@@ -33,18 +36,79 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) {
if err != nil {
return nil, err
}
if res.StatusCode >= 400 {
bs, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
return nil, errors.New(string(bs))
}
ret := json.NewDecoder(res.Body)
return ret, nil
}
type AStr struct {
A string `json:"a"`
type ARequestStruct struct {
C time.Time `json:"c"`
D string `json:"d"`
A string `json:"a"`
B int64 `json:"b"`
}
func SomeAPI2(req *AStr) (res []*AStr, err error) {
dec, err := invoke("POST", "/someapi", req)
type AResponseStruct struct {
A string `json:"a"`
B int64 `json:"b"`
C time.Time `json:"c"`
D string `json:"d"`
}
type AStr struct {
A string `json:"a"`
B []string `json:"b"`
}
type AStr2 struct {
X string `json:"x"`
Y []string `json:"y"`
Z []AStr `json:"z"`
W map[string]AStr `json:"w"`
}
func SomeAPI3(req *AStr) (res []*AStr, err error) {
var dec *json.Decoder
dec, err = invoke("POST", "/someapi3", req)
if err != nil {
return
}
ret := []*AStr{}
err = dec.Decode(ret)
err = dec.Decode(&ret)
if err != nil {
return nil, err
}
return ret, err
}
func SomeAPI(req *AStr) (res []*AStr, err error) {
var dec *json.Decoder
dec, err = invoke("PUT", "/someapi", req)
if err != nil {
return
}
ret := []*AStr{}
err = dec.Decode(&ret)
if err != nil {
return nil, err
}
return ret, err
}
func SomeAPI2(req *AStr) (res []*AStr, err error) {
var dec *json.Decoder
dec, err = invoke("POST", "/someapi", req)
if err != nil {
return
}
ret := []*AStr{}
err = dec.Decode(&ret)
if err != nil {
return nil, err
}
+61
View File
@@ -0,0 +1,61 @@
###
#SomeAPI
PUT https://host/basepath/someapi
Content-Type: application/json
Cookie: dc=<MYCOOKIE>
{}
###
#SomeAPI2
POST https://host/basepath/someapi
Content-Type: application/json
Cookie: dc=<MYCOOKIE>
{
"addresses": {
"a": {
"SUPERCALIFRAGILISPEALIDOUX": "A STRING VALUE",
"city": "A STRING VALUE",
"country": "A STRING VALUE",
"housenumber": 123456,
"iscondo": true
},
"b": {
"SUPERCALIFRAGILISPEALIDOUX": "A STRING VALUE",
"city": "A STRING VALUE",
"country": "A STRING VALUE",
"housenumber": 123456,
"iscondo": true
},
"c": {
"SUPERCALIFRAGILISPEALIDOUX": "A STRING VALUE",
"city": "A STRING VALUE",
"country": "A STRING VALUE",
"housenumber": 123456,
"iscondo": true
}
},
"firstname": "A STRING VALUE",
"lastname": "A STRING VALUE",
"products": [
"A STRING VALUE",
"A STRING VALUE",
"A STRING VALUE"
]
}
###
#SomeAPI3
POST https://host/basepath/someapi3
Content-Type: application/json
Cookie: dc=<MYCOOKIE>
{
"SUPERCALIFRAGILISPEALIDOUX": "A STRING VALUE",
"city": "A STRING VALUE",
"country": "A STRING VALUE",
"housenumber": 123456,
"iscondo": true
}
+34
View File
@@ -83,15 +83,49 @@ async function InvokeOk(path: string, method: HTMLMethod, body?: any): Promise<b
//#region Types
export interface AStr {
a:string
b:string
}
export interface AStr2 {
z:AStr
w:{[s:string]:AStr}
x:string
y:string
}
export interface ARequestStruct {
b:number
c:Date
d:string
a:string
}
export interface AResponseStruct {
a:string
b:number
c:Date
d:string
}
//#endregion
//#region Methods
/**
SomeAPI*/
export async function SomeAPI(req:AStr):Promise<AStr[]>{
return InvokeJSON("/someapi","PUT",req)
}
/**
SomeAPI2*/
export async function SomeAPI2(req:AStr):Promise<AStr[]>{
return InvokeJSON("/someapi","POST",req)
}
/**
SomeAPI3*/
export async function SomeAPI3(req:AStr):Promise<AStr[]>{
return InvokeJSON("/someapi3","POST",req)
}
//#endregion