172 lines
6.2 KiB
Go
172 lines
6.2 KiB
Go
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
|
// Use of this source code is governed by the GPL 3.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
func (pb *Files) makeHTTPFile(pf *File) error {
|
|
newf, _ := os.OpenFile(pf.Filebase+".http.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
defer newf.Close()
|
|
|
|
headerHTTP(newf, pf)
|
|
|
|
fmt.Fprintf(newf, "// START HTTP\n")
|
|
fmt.Fprintf(newf, "\n")
|
|
|
|
/*
|
|
FRUITS := pf.Bases.Name
|
|
FRUIT := pf.Base.Name
|
|
fruitVars := pf.Base.Vars
|
|
pf.generateAutoTablePB(newf, FRUITS, FRUIT, fruitVars)
|
|
*/
|
|
|
|
for _, msg := range pf.allMsg() {
|
|
if msg.DoHTTP {
|
|
color := pf.findMsg(msg.GuiVarName)
|
|
if color == nil {
|
|
return fmt.Errorf("failed to find struct %s", msg.GuiVarName)
|
|
}
|
|
FRUITS := msg.Name
|
|
FRUIT := msg.GuiVarName
|
|
// fruitVars := color.Vars
|
|
httpSendReply(newf, FRUITS)
|
|
httpCustom(newf, FRUITS, FRUIT, FRUIT)
|
|
log.Printf("NEED TO ADD GUI FOR %s with var %s and found msg struct %s\n", msg.Name, msg.GuiVarName, color.Lockname)
|
|
}
|
|
}
|
|
|
|
fmt.Fprintf(newf, "\n")
|
|
fmt.Fprintf(newf, "// END HTTP\n")
|
|
return nil
|
|
}
|
|
|
|
func headerHTTP(w io.Writer, pf *File) {
|
|
// header must come first
|
|
headerComment(w)
|
|
|
|
fmt.Fprintf(w, "package %s\n", pf.Package)
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, "import (")
|
|
fmt.Fprintln(w, " \"time\"")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " \"go.wit.com/lib/protobuf/httppb\"")
|
|
fmt.Fprintln(w, ")")
|
|
fmt.Fprintln(w, "")
|
|
}
|
|
|
|
func httpTest(w io.Writer, FRUITS string, fRUITS string, FRUIT string, fRUIT string) {
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) "+fRUITS+"Custom(w *guipb.Widget) {")
|
|
fmt.Fprintln(w, " row := mt.x."+FRUITS+"[w.Location.Y-1]")
|
|
fmt.Fprintln(w, " // log.Info(\"got to "+fRUITS+"Custom() with\", w.Location.X, w.Location.Y-1)")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " for _, sf := range mt.buttonFuncs {")
|
|
fmt.Fprintln(w, " if sf.order == int(w.Location.X) {")
|
|
fmt.Fprintln(w, " // log.Info(\"found order\", sf.order)")
|
|
fmt.Fprintln(w, " if sf.Custom != nil {")
|
|
fmt.Fprintln(w, " log.Info(\"doing Custom() func for button\")")
|
|
fmt.Fprintln(w, " sf.Custom(row)")
|
|
fmt.Fprintln(w, " return")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, " mt.CustomFunc(row)")
|
|
fmt.Fprintln(w, "}")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) Custom(f func(*"+FRUIT+")) {")
|
|
fmt.Fprintln(w, " mt.pb.RegisterCustom(mt."+fRUITS+"Custom)")
|
|
fmt.Fprintln(w, " mt.CustomFunc = f")
|
|
fmt.Fprintln(w, "}")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) GetUuid() string {")
|
|
fmt.Fprintln(w, " return mt.pb.Uuid")
|
|
fmt.Fprintln(w, "}")
|
|
fmt.Fprintln(w, "// END TABLE UPDATE")
|
|
}
|
|
|
|
func httpSendReply(w io.Writer, FRUITS string) {
|
|
fmt.Fprintln(w, "func (p *"+FRUITS+") SendReply(w http.ResponseWriter, reqPB *httppb.HttpRequest) error {")
|
|
fmt.Fprintln(w, " data, err := p.Marshal()")
|
|
fmt.Fprintln(w, " if err != nil {")
|
|
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, log.Sprintf(, err))")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, " if len(data) == 0 {")
|
|
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, \"Patches PB data was nil/emtpy without Marsha() error\")")
|
|
fmt.Fprintln(w, " return nil")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, " _, err = w.Write(data)")
|
|
fmt.Fprintln(w, " if err != nil {")
|
|
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, log.Sprintf(, i, err))")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, " return err")
|
|
fmt.Fprintln(w, "}")
|
|
}
|
|
|
|
func httpCustom(w io.Writer, FRUITS string, fRUITS string, FRUIT string) {
|
|
fmt.Fprintln(w, "// Marshal protobuf, then http POST, then Unmarshal() to protobuf again")
|
|
fmt.Fprintln(w, "func (p *"+FRUITS+") HttpPost(baseURL string, route string) (*"+FRUITS+", *httppb.HttpRequest, error) {")
|
|
fmt.Fprintln(w, " data, err := p.Marshal()")
|
|
fmt.Fprintln(w, " if err != nil {")
|
|
fmt.Fprintln(w, " return nil, nil, err")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " reqPB, err := httppb.DoPost(baseURL, route, data)")
|
|
fmt.Fprintln(w, " if reqPB == nil {")
|
|
fmt.Fprintln(w, " return nil, nil, err")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " newpb := New"+FRUITS+"()")
|
|
fmt.Fprintln(w, " err = newpb.Unmarshal(reqPB.Body)")
|
|
fmt.Fprintln(w, " return newpb, reqPB, err")
|
|
fmt.Fprintln(w, "}")
|
|
}
|
|
|
|
func httpCustomOld(w io.Writer, FRUITS string, fRUITS string, FRUIT string) {
|
|
fmt.Fprintln(w, "// err handling here isn't great")
|
|
fmt.Fprintln(w, "func (p *"+FRUITS+") HttpPost(baseURL string, route string) (*"+FRUITS+", *httppb.HttpRequest, error) {")
|
|
fmt.Fprintln(w, " // if you ever have 'http://www.wit.com//' GO will regect the server recieving it.")
|
|
fmt.Fprintln(w, " // Even though the linux kernel gets the network payload")
|
|
fmt.Fprintln(w, " // also it never gives you an error about that, it just goes away invisably inside GO")
|
|
fmt.Fprintln(w, " tmpURL, _ := url.Parse(baseURL) // 'http://forge.grid.wit.com:2520')")
|
|
fmt.Fprintln(w, " finalURL := tmpURL.JoinPath(\"/"+fRUITS+"/\", route)")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " data, _ := p.Marshal()")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " var err error")
|
|
fmt.Fprintln(w, " var req *http.Request")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data))")
|
|
fmt.Fprintln(w, " if req == nil {")
|
|
fmt.Fprintln(w, " return nil, nil, err")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " usr, _ := user.Current()")
|
|
fmt.Fprintln(w, " req.Header.Set(\"author\", usr.Username)")
|
|
fmt.Fprintln(w, " hostname, _ := os.Hostname()")
|
|
fmt.Fprintln(w, " req.Header.Set(\"hostname\", hostname)")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " newdata, err := httppb.PostReq(req)")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " reqPB, err2 := httppb.ReqToPB(req)")
|
|
fmt.Fprintln(w, " reqPB.URL = finalURL.String()")
|
|
fmt.Fprintln(w, " if err != nil {")
|
|
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, fmt.Sprintf(err))")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, " if err2 != nil {")
|
|
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, fmt.Sprintf(err2))")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " newpb := New"+FRUITS+"()")
|
|
fmt.Fprintln(w, " err = newpb.Unmarshal(newdata)")
|
|
fmt.Fprintln(w, "")
|
|
fmt.Fprintln(w, " return newpb, reqPB, err")
|
|
fmt.Fprintln(w, "}")
|
|
}
|