// 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 { FRUITS := msg.Name fRUITS := untitle(FRUITS) httpSendReply(newf, FRUITS) httpPost(newf, FRUITS, fRUITS) log.Printf("func (p *%s) HttpPost(baseURL string, route string) (*%s, *httppb.HttpRequest, error)\n", FRUITS, FRUITS) } } 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 httpPost(w io.Writer, FRUITS string, fRUITS 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, " tmp := filepath.Join(\""+fRUITS+"\",route)") fmt.Fprintln(w, " reqPB, err := httppb.DoPost(baseURL, tmp, data)") fmt.Fprintln(w, " if reqPB == nil {") fmt.Fprintln(w, " return nil, nil, err") fmt.Fprintln(w, " }") fmt.Fprintln(w, "") fmt.Fprintln(w, " if len(reqPB.ServerData) == 0 {") fmt.Fprintln(w, " return nil, reqPB, fmt.Errorf(\"server returned len(data)=0\")") fmt.Fprintln(w, " }") fmt.Fprintln(w, "") fmt.Fprintln(w, " newpb := new("+FRUITS+")") fmt.Fprintln(w, " err = newpb.Unmarshal(reqPB.ServerData)") fmt.Fprintln(w, " return newpb, reqPB, err") fmt.Fprintln(w, "}") }