fixes
This commit is contained in:
@@ -3,6 +3,7 @@ package lib
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -50,18 +51,34 @@ func processGoClientOutput(f string) error {
|
||||
}
|
||||
|
||||
WNL("package %s", api.Namespace)
|
||||
|
||||
for k, v := range api.UsedImportsFunctions {
|
||||
log.Printf("Adding import from funcions: %s => %s", k, v)
|
||||
WNL(`import "%s"`, k)
|
||||
}
|
||||
|
||||
for k, v := range api.UsedImportsTypes {
|
||||
log.Printf("Adding import from types: %s => %s", k, v)
|
||||
WNL(`import "%s"`, k)
|
||||
}
|
||||
|
||||
WNL(`import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
var Basepath string = ""
|
||||
var Host string = ""
|
||||
var ExtraHeaders map[string]string = make(map[string]string)
|
||||
|
||||
var cli *http.Client
|
||||
|
||||
func SetCli(nc *http.Client) {
|
||||
cli = nc
|
||||
}
|
||||
|
||||
func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) {
|
||||
b := &bytes.Buffer{}
|
||||
err := json.NewEncoder(b).Encode(bodyo)
|
||||
@@ -81,14 +98,12 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
cli := http.Client{}
|
||||
res, err := cli.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode >= 400 {
|
||||
bs, err := ioutil.ReadAll(res.Body)
|
||||
@@ -111,7 +126,7 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) {
|
||||
W("[]")
|
||||
}
|
||||
if f.Map {
|
||||
W("map[%s]%s", f.Mapkey, f.Mapval)
|
||||
WNL("map[%s]%s", f.Mapkey, f.Mapval)
|
||||
} else {
|
||||
WNL(f.Type)
|
||||
}
|
||||
|
||||
@@ -9,13 +9,22 @@ import (
|
||||
|
||||
func processGoServerOutput(f string) error {
|
||||
buf := &bytes.Buffer{}
|
||||
W := func(s string, p ...interface{}) {
|
||||
buf.WriteString(fmt.Sprintf(s, p...))
|
||||
}
|
||||
//W := func(s string, p ...interface{}) {
|
||||
// buf.WriteString(fmt.Sprintf(s, p...))
|
||||
//}
|
||||
WNL := func(s string, p ...interface{}) {
|
||||
buf.WriteString(fmt.Sprintf(s+"\n", p...))
|
||||
}
|
||||
|
||||
ResImplType := func(v *APIParamType) string {
|
||||
ret := ""
|
||||
if !v.IsArray || v.Ispointer {
|
||||
ret = ret + "&"
|
||||
}
|
||||
ret += v.Typename + "{}"
|
||||
return ret
|
||||
}
|
||||
|
||||
WNL("package %s", api.Namespace)
|
||||
WNL(`import (
|
||||
"context"
|
||||
@@ -25,17 +34,17 @@ func processGoServerOutput(f string) error {
|
||||
)`)
|
||||
|
||||
for k := range api.UsedImportsFunctions {
|
||||
W(`import "%s"`, k)
|
||||
WNL(`import "%s"`, k)
|
||||
}
|
||||
|
||||
WNL(`type API struct {
|
||||
Mux *http.ServeMux
|
||||
Perms map[string]string
|
||||
}
|
||||
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 (a *API) GetPerm(r *http.Request) string {
|
||||
return a.Perms[r.Method+"_"+strings.Split(r.RequestURI, "?")[0]]
|
||||
}
|
||||
`)
|
||||
|
||||
WNL(`func Init() *API{
|
||||
@@ -61,18 +70,17 @@ func processGoServerOutput(f string) error {
|
||||
} else {
|
||||
WNL(` h_%s(w,r)`, v1.Method.Name)
|
||||
}
|
||||
|
||||
WNL(` default:
|
||||
http.Error(w,"Method not allowed",500)`)
|
||||
|
||||
WNL(` }`)
|
||||
}
|
||||
WNL(` })`)
|
||||
|
||||
WNL(` default:
|
||||
http.Error(w,"Method not allowed",500)
|
||||
}`)
|
||||
|
||||
}
|
||||
WNL(` })`)
|
||||
|
||||
WNL(` return ret
|
||||
}
|
||||
`)
|
||||
}`)
|
||||
|
||||
for _, v := range api.Methods {
|
||||
WNL(`func h_%s(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -80,15 +88,7 @@ func processGoServerOutput(f string) error {
|
||||
ctx = context.WithValue(r.Context(), "REQ", r)
|
||||
ctx = context.WithValue(ctx, "RES", w)`, v.Name)
|
||||
|
||||
W(" var req ")
|
||||
|
||||
if v.ReqType.IsArray {
|
||||
W("[]")
|
||||
}
|
||||
if v.ReqType.Ispointer {
|
||||
W("*")
|
||||
}
|
||||
WNL(v.ReqType.Typename)
|
||||
WNL(" req := %s", ResImplType(v.ReqType))
|
||||
|
||||
WNL(` if r.Method!=http.MethodGet && r.Method!=http.MethodHead {`)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user