start using a standard http PB

This commit is contained in:
Jeff Carr 2025-09-07 19:14:20 -05:00
parent eae2c3be2b
commit c2e0e8e80b
7 changed files with 208 additions and 138 deletions

View File

@ -4,13 +4,13 @@ VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d_%H%M) BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
all: build-verbose all: build-verbose
./forged merge # ./forged merge
build: goimports build: goimports
GO111MODULE=off go build \ GO111MODULE=off go build \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
build-verbose: build-verbose: goimports
GO111MODULE=off go build -v -x \ GO111MODULE=off go build -v -x \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"

View File

@ -54,7 +54,7 @@ func savePatchset(w http.ResponseWriter, msg []byte) error {
log.Info("error =", err) log.Info("error =", err)
return err return err
} }
log.Info("GOT patchset:", len(msg)) log.Info("GOT patchset: m.Len() =", len(msg))
// fmt.Fprintln(w, "GOT patchset:", len(msg)) // fmt.Fprintln(w, "GOT patchset:", len(msg))
now := time.Now() now := time.Now()
// timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R // timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R

View File

@ -5,10 +5,30 @@ import (
"strings" "strings"
"go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/lib/protobuf/httppb" "go.wit.com/lib/protobuf/httppb"
"go.wit.com/log" "go.wit.com/log"
) )
func addNewPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
newPatchesPB := new(forgepb.Patches)
for newpatch := range pb.IterAll() {
me.forge.AddNewPatch(newpatch)
newPatchesPB.Append(newpatch)
}
return newPatchesPB
}
func sendPendingPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
allPatchesPB := new(forgepb.Patches)
for pset := range me.forge.Patchsets.IterAll() {
for newpatch := range pset.Patches.IterAll() {
allPatchesPB.Append(newpatch)
}
}
return allPatchesPB
}
func handlePatches(w http.ResponseWriter, pb *forgepb.Patches) error { func handlePatches(w http.ResponseWriter, pb *forgepb.Patches) error {
route := pb.HttpRequest.Route route := pb.HttpRequest.Route
@ -23,6 +43,12 @@ func handlePatches(w http.ResponseWriter, pb *forgepb.Patches) error {
return nil return nil
} }
func makeReposPB(reqPB *httppb.HttpRequest) (*gitpb.Repos, error) {
pb := gitpb.NewRepos()
err := pb.Unmarshal(reqPB.Body)
return pb, err
}
func makePatchesPB(reqPB *httppb.HttpRequest) (*forgepb.Patches, error) { func makePatchesPB(reqPB *httppb.HttpRequest) (*forgepb.Patches, error) {
pb := forgepb.NewPatches() pb := forgepb.NewPatches()
err := pb.Unmarshal(reqPB.Body) err := pb.Unmarshal(reqPB.Body)

61
handleRepos.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"strings"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/lib/protobuf/httppb"
"go.wit.com/log"
)
func handleRepos(pb *gitpb.Repos, route string) error {
log.Info("GOT PATCHES ROUTE", route, "with # patches =", pb.Len())
if strings.HasPrefix(route, "/patches/old") {
} else if strings.HasPrefix(route, "/patches/new") {
log.Info("add new patches")
} else {
log.Info("unknown route", route)
}
return nil
}
// returns a repo PB with just the master and devel branch versions
// is just this information so it keeps things small when it's sent over the wire
func getCurrentRepoVersions(namespace string) *gitpb.Repo {
newrepo := new(gitpb.Repo)
newrepo.Namespace = namespace
found := me.forge.Repos.FindByNamespace(namespace)
if found == nil {
// todo: clone repo here
return newrepo
}
newrepo.MasterHash = found.MasterHash
newrepo.DevelHash = found.DevelHash
newrepo.URL = found.URL
return newrepo
}
func pullRequest(pb *gitpb.Repos, reqPB *httppb.HttpRequest) *gitpb.Repos {
versionsPB := gitpb.NewRepos()
for repo := range pb.IterAll() {
found := getCurrentRepoVersions(repo.Namespace)
versionsPB.Append(found)
}
return versionsPB
}
func addRequest(pb *gitpb.Repos, reqPB *httppb.HttpRequest) *gitpb.Repos {
newReposPB := gitpb.NewRepos()
for repo := range pb.IterAll() {
if found := me.forge.Repos.FindByNamespace(repo.Namespace); found != nil {
// already know about this namespace
continue
}
newrepo := new(gitpb.Repo)
newrepo.Namespace = repo.Namespace
newrepo.URL = repo.URL
newReposPB.Append(newrepo)
}
return newReposPB
}

105
http.go
View File

@ -5,9 +5,9 @@ import (
"net" "net"
"net/http" "net/http"
"strings" "strings"
"time"
"go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/lib/protobuf/httppb" "go.wit.com/lib/protobuf/httppb"
"go.wit.com/log" "go.wit.com/log"
) )
@ -58,7 +58,6 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
route := reqPB.Route route := reqPB.Route
parts := strings.Split(route, "?") parts := strings.Split(route, "?")
// log.Info("client sent url =", route, parts)
requrl := parts[0] requrl := parts[0]
if route == "/" { if route == "/" {
@ -76,17 +75,49 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
log.Warn("forged REQUEST URL =", requrl, "from =", who) log.Warn("forged REQUEST URL =", requrl, "from =", who)
if strings.HasPrefix(route, "/repos/") {
pb, err := makeReposPB(reqPB)
if err != nil {
reqPB.Errors = append(reqPB.Errors, log.Sprintf("%v", err))
}
result := gitpb.NewRepos()
switch route {
case "/repos/pull":
result = pullRequest(pb, reqPB)
case "/repos/add":
result = addRequest(pb, reqPB)
default:
result = pullRequest(pb, reqPB)
}
if err := result.SendReply(w, reqPB); err != nil {
log.Info("Oh well, Send to client failed. err =", err)
}
// todo: logReq(reqPB)
return
}
if strings.HasPrefix(route, "/patches/") { if strings.HasPrefix(route, "/patches/") {
pb, err := makePatchesPB(reqPB) pb, err := makePatchesPB(reqPB)
log.Info("err", err, "len", pb.Len()) if err != nil {
return reqPB.Errors = append(reqPB.Errors, log.Sprintf("%v", err))
} }
result := forgepb.NewPatches()
if strings.HasPrefix(route, "/patches/applied") { switch route {
log.Info("todo: handle applied patches") case "/patches/new":
result = addNewPatches(pb, reqPB)
log.Infof("addNewPatches() pb.Len()=%d result.Len()=%d\n", pb.Len(), result.Len())
case "/patches/get":
result = sendPendingPatches(pb, reqPB)
default:
result = addNewPatches(pb, reqPB)
}
if err := result.SendReply(w, reqPB); err != nil {
log.Info("Oh well, Send to client failed. err =", err)
}
// todo: logReq(reqPB)
return return
} }
/*
if route == "/patchset" { if route == "/patchset" {
if err := savePatchset(w, reqPB.Body); err != nil { if err := savePatchset(w, reqPB.Body); err != nil {
log.Warn("forged /patchset error", err) log.Warn("forged /patchset error", err)
@ -106,28 +137,23 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
/*
for repo := range repos.IterAll() {
repo.Namespace += "good"
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
}
*/
found.SendPB(w) found.SendPB(w)
return return
} }
*/
if strings.HasPrefix(route, "/patches/") {
/* /*
if strings.HasPrefix(route, "/patches/") {
pb, err := forgepb.GetPatchesFromHttp(reqPB) pb, err := forgepb.GetPatchesFromHttp(reqPB)
if err != nil { if err != nil {
log.Info("error converting to patches PB") log.Info("error converting to patches PB")
return return
} }
handlePatches(w, pb) handlePatches(w, pb)
*/
return return
} }
*/
/*
if route == "/patchset" { if route == "/patchset" {
if err := savePatchset(w, reqPB.Body); err != nil { if err := savePatchset(w, reqPB.Body); err != nil {
log.Warn("forged /patchset error", err) log.Warn("forged /patchset error", err)
@ -147,41 +173,6 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
/*
for repo := range repos.IterAll() {
repo.Namespace += "good"
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
}
*/
found.SendPB(w)
return
}
if route == "/patchset" {
if err := savePatchset(w, reqPB.Body); err != nil {
log.Warn("forged /patchset error", err)
return
}
if err := me.forge.SavePatchsets(); err != nil {
log.Warn("savePatchsets() failed", err)
return
}
return
}
if route == "/lookup" {
log.Info("doing lookup len(reqPB.Body) =", len(reqPB.Body))
found, err := lookupRepos(reqPB.Body)
if err != nil {
return
}
/*
for repo := range repos.IterAll() {
repo.Namespace += "good"
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
}
*/
found.SendPB(w) found.SendPB(w)
return return
} }
@ -196,7 +187,9 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
found.SendPB(w) found.SendPB(w)
return return
} }
*/
/*
if route == "/GetPatchsets" || route == "/patchsets/get" { if route == "/GetPatchsets" || route == "/patchsets/get" {
data, err := me.forge.Patchsets.Marshal() data, err := me.forge.Patchsets.Marshal()
if err != nil { if err != nil {
@ -208,11 +201,6 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
log.Info("going to w.Write(data) with len", len(data)) log.Info("going to w.Write(data) with len", len(data))
w.Write(data) w.Write(data)
/*
start := time.Now()
log.Info("going to w.Write(reqPB.Body) with len", len(reqPB.Body))
w.Write(reqPB.Body)
*/
age := shell.FormatDuration(time.Since(start)) age := shell.FormatDuration(time.Since(start))
log.Printf("Done with xfer in (%s). happy hacking!\n", age) log.Printf("Done with xfer in (%s). happy hacking!\n", age)
return return
@ -223,6 +211,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
getPatchset(w, filename) getPatchset(w, filename)
return return
} }
*/
if route == "/goReference.svg" { if route == "/goReference.svg" {
w.Header().Set("Content-Type", "image/svg+xml") w.Header().Set("Content-Type", "image/svg+xml")

View File

@ -22,8 +22,8 @@ var ARGNAME string = "forged"
var resources embed.FS var resources embed.FS
var HOSTNAME string = "forge.wit.com" var HOSTNAME string = "forge.wit.com"
var LIBDIR string = "/var/lib/forged/" var LIBDIR string = "/var/lib/forged/" // need to deprecate this
var FORGEDIR string = "/home/forge" // var FORGEDIR string = "/home/forge" // deprecated?
func main() { func main() {
me = new(mainType) me = new(mainType)

View File

@ -1,13 +1,8 @@
package main package main
import ( // a middleware example. probably not interesting since we only pass protobufs
"bytes"
"context"
"io/ioutil"
"log"
"net/http"
)
/*
// Define a key type to avoid context key collisions. // Define a key type to avoid context key collisions.
type contextKey string type contextKey string
@ -42,7 +37,6 @@ func bufferBodyMiddleware(next http.Handler) http.Handler {
}) })
} }
/*
// okHandler is the final handler. It can now safely access the body from the context, // okHandler is the final handler. It can now safely access the body from the context,
// knowing that other middleware might have also read it. // knowing that other middleware might have also read it.
func okHandler(w http.ResponseWriter, r *http.Request) { func okHandler(w http.ResponseWriter, r *http.Request) {