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)
|
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}"
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
239
http.go
239
http.go
|
@ -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,153 +75,143 @@ 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())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(route, "/patches/applied") {
|
|
||||||
log.Info("todo: handle applied patches")
|
|
||||||
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 {
|
if err != nil {
|
||||||
return
|
reqPB.Errors = append(reqPB.Errors, log.Sprintf("%v", err))
|
||||||
}
|
}
|
||||||
|
result := forgepb.NewPatches()
|
||||||
/*
|
switch route {
|
||||||
for repo := range repos.IterAll() {
|
case "/patches/new":
|
||||||
repo.Namespace += "good"
|
result = addNewPatches(pb, reqPB)
|
||||||
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
|
log.Infof("addNewPatches() pb.Len()=%d result.Len()=%d\n", pb.Len(), result.Len())
|
||||||
}
|
case "/patches/get":
|
||||||
*/
|
result = sendPendingPatches(pb, reqPB)
|
||||||
found.SendPB(w)
|
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 strings.HasPrefix(route, "/patches/") {
|
/*
|
||||||
/*
|
if route == "/patchset" {
|
||||||
pb, err := forgepb.GetPatchesFromHttp(reqPB)
|
if err := savePatchset(w, reqPB.Body); err != nil {
|
||||||
if err != nil {
|
log.Warn("forged /patchset error", err)
|
||||||
log.Info("error converting to patches PB")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
handlePatches(w, pb)
|
if err := me.forge.SavePatchsets(); err != nil {
|
||||||
*/
|
log.Warn("savePatchsets() failed", err)
|
||||||
return
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if route == "/lookup" {
|
||||||
for repo := range repos.IterAll() {
|
log.Info("doing lookup len(reqPB.Body) =", len(reqPB.Body))
|
||||||
repo.Namespace += "good"
|
found, err := lookupRepos(reqPB.Body)
|
||||||
log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath)
|
if err != nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
found.SendPB(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if route == "/update" {
|
found.SendPB(w)
|
||||||
log.Info("doing update len(reqPB.Body) =", len(reqPB.Body))
|
return
|
||||||
found, err := updateRepos(reqPB.Body)
|
}
|
||||||
if err != nil {
|
*/
|
||||||
|
/*
|
||||||
|
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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := me.forge.SavePatchsets(); err != nil {
|
||||||
|
log.Warn("savePatchsets() failed", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
found.SendPB(w)
|
if route == "/lookup" {
|
||||||
return
|
log.Info("doing lookup len(reqPB.Body) =", len(reqPB.Body))
|
||||||
}
|
found, err := lookupRepos(reqPB.Body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if route == "/GetPatchsets" || route == "/patchsets/get" {
|
found.SendPB(w)
|
||||||
data, err := me.forge.Patchsets.Marshal()
|
|
||||||
if err != nil {
|
|
||||||
log.Info("patchsets.Marshal() to wire failed", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
start := time.Now()
|
if route == "/update" {
|
||||||
log.Info("going to w.Write(data) with len", len(data))
|
log.Info("doing update len(reqPB.Body) =", len(reqPB.Body))
|
||||||
w.Write(data)
|
found, err := updateRepos(reqPB.Body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
found.SendPB(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
if route == "/GetPatchsets" || route == "/patchsets/get" {
|
||||||
|
data, err := me.forge.Patchsets.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
log.Info("patchsets.Marshal() to wire failed", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
log.Info("going to w.Write(reqPB.Body) with len", len(reqPB.Body))
|
log.Info("going to w.Write(data) with len", len(data))
|
||||||
w.Write(reqPB.Body)
|
w.Write(data)
|
||||||
*/
|
|
||||||
age := shell.FormatDuration(time.Since(start))
|
|
||||||
log.Printf("Done with xfer in (%s). happy hacking!\n", age)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if route == "/patchsetget" {
|
age := shell.FormatDuration(time.Since(start))
|
||||||
filename := r.URL.Query().Get("filename")
|
log.Printf("Done with xfer in (%s). happy hacking!\n", age)
|
||||||
getPatchset(w, filename)
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
if route == "/patchsetget" {
|
||||||
|
filename := r.URL.Query().Get("filename")
|
||||||
|
getPatchset(w, filename)
|
||||||
|
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")
|
||||||
|
|
4
main.go
4
main.go
|
@ -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)
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue