2020-11-06 23:16:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-11-22 12:05:45 +00:00
|
|
|
dc "go.digitalcircle.com.br/golib/base"
|
2020-11-06 23:16:52 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func processGoServerOutput(api *API) {
|
|
|
|
|
|
|
|
APIParamTypeToString := func(t *APIParamType) string {
|
|
|
|
ret := ""
|
|
|
|
if t.Ispointer {
|
|
|
|
ret = ret + "&"
|
|
|
|
}
|
|
|
|
ret = ret + t.Typename
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
|
|
|
|
f := config.Gofname
|
|
|
|
|
|
|
|
os.Remove(f)
|
|
|
|
b.WriteString(fmt.Sprintf(`package %s
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
`, packageName))
|
|
|
|
|
|
|
|
b.WriteString(`
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
for _, m := range api.Methods {
|
|
|
|
if m.Perm != "" {
|
|
|
|
b.WriteString(fmt.Sprintf(`
|
|
|
|
ret.Perms["%s_%s"]="%s"
|
|
|
|
`, m.Verb, m.Path, m.Perm))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.WriteString("\n\n")
|
|
|
|
|
|
|
|
for p, mv := range httpMapper {
|
|
|
|
|
|
|
|
b.WriteString(fmt.Sprintf(" mux.HandleFunc(\"%s\",func(w http.ResponseWriter, r *http.Request) {\n", strings.Replace(p, "//", "/", -1)))
|
|
|
|
b.WriteString(" switch r.Method{\n")
|
|
|
|
|
|
|
|
for v, id := range mv {
|
|
|
|
|
|
|
|
b.WriteString(fmt.Sprintf(" case \"%s\":", v))
|
|
|
|
if api.Methods[id].Raw {
|
|
|
|
b.WriteString(fmt.Sprintf(" %s(w , r)\n", id))
|
|
|
|
} else {
|
|
|
|
b.WriteString(fmt.Sprintf(" h_%s(w , r)\n", id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.WriteString(" default:")
|
|
|
|
b.WriteString(" http.Error(w,\"Method not allowed\",500)")
|
|
|
|
|
|
|
|
b.WriteString(" }\n")
|
|
|
|
b.WriteString("})\n")
|
|
|
|
}
|
|
|
|
b.WriteString("return ret\n }\n")
|
|
|
|
|
|
|
|
for k, m := range api.Methods {
|
|
|
|
if !m.Raw {
|
|
|
|
b.WriteString(fmt.Sprintf("\n func h_%s(w http.ResponseWriter, r *http.Request) {\n", k))
|
|
|
|
|
|
|
|
b.WriteString(fmt.Sprintf(
|
|
|
|
`
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx = context.WithValue(r.Context(), "REQ", r)
|
|
|
|
ctx = context.WithValue(ctx, "RES", w)
|
|
|
|
req := %s{}
|
|
|
|
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 := %s(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
`, APIParamTypeToString(m.ReqType), k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(f, b.Bytes(), 0600)
|
|
|
|
dc.Err(err)
|
|
|
|
cmd := exec.Command("/bin/sh", "-c", "go fmt "+f)
|
|
|
|
bs, err := cmd.Output()
|
|
|
|
//dc.Err(err)
|
|
|
|
dc.Log(string(bs))
|
|
|
|
}
|