diff options
| author | Jeff Carr <[email protected]> | 2025-09-08 03:37:27 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-09-08 03:37:27 -0500 |
| commit | 1fe6c532674ad10cbd5021a619fa0a269fe33194 (patch) | |
| tree | f2c98f2f3a1df32fc71d1b1594498d2a14571e0f /generateHTTP.go | |
| parent | 7d67ae7528fdda142e51dda401e9f801563c947a (diff) | |
autogen some http code
Diffstat (limited to 'generateHTTP.go')
| -rw-r--r-- | generateHTTP.go | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/generateHTTP.go b/generateHTTP.go new file mode 100644 index 0000000..ef6ea0e --- /dev/null +++ b/generateHTTP.go @@ -0,0 +1,135 @@ +// 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 + 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 httpCustom(w io.Writer, FRUITS string, fRUITS string, FRUIT string) { + fmt.Fprintln(w, "// err handling here isn't great") + fmt.Fprintln(w, "func (p *Repos) HttpPost(baseURL string, route string) (*Repos, *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(\"/repos/\", 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, " log.Info(\"reposs PB HttpPost\", finalURL.String())") + 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 := NewRepos()") + fmt.Fprintln(w, " err = newpb.Unmarshal(newdata)") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " // log.Info(repos PB HttpPost finalURL.String(), p.Len(), newpb.Len())") + fmt.Fprintln(w, " return newpb, reqPB, err") + fmt.Fprintln(w, "}") +} |
