start using a standard http PB
This commit is contained in:
parent
eae2c3be2b
commit
c2e0e8e80b
4
Makefile
4
Makefile
|
@ -4,13 +4,13 @@ VERSION = $(shell git describe --tags)
|
|||
BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
|
||||
|
||||
all: build-verbose
|
||||
./forged merge
|
||||
# ./forged merge
|
||||
|
||||
build: goimports
|
||||
GO111MODULE=off go build \
|
||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||
|
||||
build-verbose:
|
||||
build-verbose: goimports
|
||||
GO111MODULE=off go build -v -x \
|
||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ func savePatchset(w http.ResponseWriter, msg []byte) error {
|
|||
log.Info("error =", err)
|
||||
return err
|
||||
}
|
||||
log.Info("GOT patchset:", len(msg))
|
||||
log.Info("GOT patchset: m.Len() =", len(msg))
|
||||
// fmt.Fprintln(w, "GOT patchset:", len(msg))
|
||||
now := time.Now()
|
||||
// timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R
|
||||
|
|
|
@ -5,10 +5,30 @@ import (
|
|||
"strings"
|
||||
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
"go.wit.com/lib/protobuf/gitpb"
|
||||
"go.wit.com/lib/protobuf/httppb"
|
||||
"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 {
|
||||
route := pb.HttpRequest.Route
|
||||
|
||||
|
@ -23,6 +43,12 @@ func handlePatches(w http.ResponseWriter, pb *forgepb.Patches) error {
|
|||
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) {
|
||||
pb := forgepb.NewPatches()
|
||||
err := pb.Unmarshal(reqPB.Body)
|
||||
|
|
|
@ -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
105
http.go
|
@ -5,9 +5,9 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"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/log"
|
||||
)
|
||||
|
@ -58,7 +58,6 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
route := reqPB.Route
|
||||
parts := strings.Split(route, "?")
|
||||
// log.Info("client sent url =", route, parts)
|
||||
requrl := parts[0]
|
||||
|
||||
if route == "/" {
|
||||
|
@ -76,17 +75,49 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
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/") {
|
||||
pb, err := makePatchesPB(reqPB)
|
||||
log.Info("err", err, "len", pb.Len())
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(route, "/patches/applied") {
|
||||
log.Info("todo: handle applied patches")
|
||||
if err != nil {
|
||||
reqPB.Errors = append(reqPB.Errors, log.Sprintf("%v", err))
|
||||
}
|
||||
result := forgepb.NewPatches()
|
||||
switch route {
|
||||
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
|
||||
}
|
||||
|
||||
/*
|
||||
if route == "/patchset" {
|
||||
if err := savePatchset(w, reqPB.Body); err != nil {
|
||||
log.Warn("forged /patchset error", err)
|
||||
|
@ -106,28 +137,23 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
/*
|
||||
for repo := range repos.IterAll() {
|
||||
repo.Namespace += "good"
|
||||
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
|
||||
}
|
||||
*/
|
||||
found.SendPB(w)
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(route, "/patches/") {
|
||||
*/
|
||||
/*
|
||||
if strings.HasPrefix(route, "/patches/") {
|
||||
pb, err := forgepb.GetPatchesFromHttp(reqPB)
|
||||
if err != nil {
|
||||
log.Info("error converting to patches PB")
|
||||
return
|
||||
}
|
||||
handlePatches(w, pb)
|
||||
*/
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
if route == "/patchset" {
|
||||
if err := savePatchset(w, reqPB.Body); err != nil {
|
||||
log.Warn("forged /patchset error", err)
|
||||
|
@ -147,41 +173,6 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
|||
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)
|
||||
return
|
||||
}
|
||||
|
@ -196,7 +187,9 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
|||
found.SendPB(w)
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
if route == "/GetPatchsets" || route == "/patchsets/get" {
|
||||
data, err := me.forge.Patchsets.Marshal()
|
||||
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))
|
||||
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))
|
||||
log.Printf("Done with xfer in (%s). happy hacking!\n", age)
|
||||
return
|
||||
|
@ -223,6 +211,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
|||
getPatchset(w, filename)
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
if route == "/goReference.svg" {
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
|
|
4
main.go
4
main.go
|
@ -22,8 +22,8 @@ var ARGNAME string = "forged"
|
|||
var resources embed.FS
|
||||
|
||||
var HOSTNAME string = "forge.wit.com"
|
||||
var LIBDIR string = "/var/lib/forged/"
|
||||
var FORGEDIR string = "/home/forge"
|
||||
var LIBDIR string = "/var/lib/forged/" // need to deprecate this
|
||||
// var FORGEDIR string = "/home/forge" // deprecated?
|
||||
|
||||
func main() {
|
||||
me = new(mainType)
|
||||
|
|
|
@ -1,13 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
// a middleware example. probably not interesting since we only pass protobufs
|
||||
|
||||
/*
|
||||
// Define a key type to avoid context key collisions.
|
||||
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,
|
||||
// knowing that other middleware might have also read it.
|
||||
func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
Loading…
Reference in New Issue