173 lines
4.6 KiB
Go
173 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"go.wit.com/lib/protobuf/forgepb"
|
|
"go.wit.com/lib/protobuf/gitpb"
|
|
"go.wit.com/lib/protobuf/httppb"
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
func logReqPB(pb *httppb.HttpRequest) {
|
|
log.Info("LOG: httppb.HttpRequest START:")
|
|
for i, line := range pb.Logs {
|
|
log.Infof("%d, URL:%s LINE: %s\n", int(i), pb.URL, line)
|
|
}
|
|
log.Info("LOG: httppb.HttpRequest END")
|
|
}
|
|
|
|
func okHandler(w http.ResponseWriter, r *http.Request) {
|
|
reqPB, err := httppb.ReqToPB(r)
|
|
reqPB.Log("START: Got %d bytes from the client", len(reqPB.ClientData))
|
|
if err != nil {
|
|
reqPB.Logf("httppb err %v", err)
|
|
}
|
|
|
|
route := reqPB.Route
|
|
|
|
if route == "/" {
|
|
w.Header().Set("Content-Type", "text")
|
|
fmt.Fprintf(w, "go.wit.com/apps/utils/forged Version: %s\n", argv.Version())
|
|
fmt.Fprintf(w, "\n")
|
|
return
|
|
}
|
|
|
|
if strings.HasPrefix(route, "/repos/") {
|
|
pb := gitpb.NewRepos()
|
|
if err := pb.Unmarshal(reqPB.ClientData); err == nil {
|
|
reqPB.Log("Repos Unmarshal() len=%d", pb.Len())
|
|
} else {
|
|
reqPB.Logf("Repos Unmarshal() err=%v", err)
|
|
}
|
|
result := gitpb.NewRepos()
|
|
switch route {
|
|
case "/repos/check":
|
|
result = addRequest(pb, reqPB)
|
|
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
|
|
case "/repos/pull":
|
|
result = pullRequest(pb, reqPB)
|
|
case "/repos/add":
|
|
result = addRequest(pb, reqPB)
|
|
default:
|
|
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
|
|
log.Info("repos", route, "unknown")
|
|
}
|
|
if err := result.SendReply(w, reqPB); err != nil {
|
|
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
|
|
}
|
|
// todo: logReq(reqPB)
|
|
logReqPB(reqPB)
|
|
return
|
|
}
|
|
|
|
if strings.HasPrefix(route, "/patches/") {
|
|
pb := forgepb.NewPatches()
|
|
if err := pb.Unmarshal(reqPB.ClientData); err == nil {
|
|
reqPB.Logf("Patches Unmarshal() len=%d", pb.Len())
|
|
} else {
|
|
reqPB.Logf("Patches Unmarshal() err=%v", err)
|
|
}
|
|
result := forgepb.NewPatches()
|
|
switch route {
|
|
case "/patches/new":
|
|
result = addNewPatches(pb, reqPB)
|
|
reqPB.Logf("addNewPatches() pb.Len()=%d result.Len()=%d\n", pb.Len(), result.Len())
|
|
case "/patches/applied":
|
|
reqPB.Logf("not really anything needs to be done on applied patches?")
|
|
case "/patches/merged":
|
|
reqPB.Logf("a maintainer has merged these patches")
|
|
result = handleMergedPatches(pb, reqPB)
|
|
// result = handleAppliedPatches(pb, reqPB)
|
|
case "/patches/get":
|
|
result = sendPendingPatches(pb, reqPB)
|
|
default:
|
|
result = addNewPatches(pb, reqPB)
|
|
}
|
|
if err := result.SendReply(w, reqPB); err != nil {
|
|
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
|
|
}
|
|
me.forge.SavePatchsets()
|
|
// todo: logReq(reqPB)
|
|
logReqPB(reqPB)
|
|
return
|
|
}
|
|
|
|
if strings.HasPrefix(route, "/patchsets/") {
|
|
pb, err := makePatchsetsPB(reqPB)
|
|
if err != nil {
|
|
reqPB.Logf("%v", err)
|
|
}
|
|
result := forgepb.NewPatchsets()
|
|
switch route {
|
|
case "/patches/get":
|
|
result = sendPendingPatchsets(pb, reqPB)
|
|
default:
|
|
result = sendPendingPatchsets(pb, reqPB)
|
|
}
|
|
if err := result.SendReply(w, reqPB); err != nil {
|
|
log.Info("Oh well, Send to client failed. err =", err)
|
|
}
|
|
log.Info("Send to client seems to have worked. log:", reqPB.Log)
|
|
me.forge.SavePatchsets()
|
|
// todo: logReq(reqPB)
|
|
logReqPB(reqPB)
|
|
return
|
|
}
|
|
logReqPB(reqPB)
|
|
|
|
// used for uptime monitor checking
|
|
if route == "/uptime" {
|
|
writeFile(w, "uptime.html")
|
|
return
|
|
}
|
|
|
|
if route == "/goReference.svg" {
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
writeFile(w, "goReference.svg")
|
|
return
|
|
}
|
|
if route == "/skeleton.v2.css" {
|
|
writeFile(w, "skeleton.v2.css")
|
|
return
|
|
}
|
|
if route == "/favicon.ico" {
|
|
writeFile(w, "ipv6.png")
|
|
return
|
|
}
|
|
log.Warn("BAD URL =", reqPB.URL, "from", reqPB.IP, "author", reqPB.Hostname)
|
|
badurl(w, r.URL.String())
|
|
}
|
|
|
|
func writeFile(w http.ResponseWriter, filename string) {
|
|
// fmt.Fprintln(w, "GOT TEST?")
|
|
fullname := "resources/" + filename
|
|
pfile, err := resources.ReadFile(fullname)
|
|
if err != nil {
|
|
log.Println("ERROR:", err)
|
|
// w.Write(pfile)
|
|
return
|
|
}
|
|
|
|
var repohtml string
|
|
repohtml = string(pfile)
|
|
fmt.Fprintln(w, repohtml)
|
|
}
|
|
|
|
func badurl(w http.ResponseWriter, badurl string) {
|
|
fmt.Fprintln(w, "<!DOCTYPE html>")
|
|
fmt.Fprintln(w, "<html>")
|
|
fmt.Fprintln(w, " <head>")
|
|
fmt.Fprintln(w, " <meta http-equiv=\"refresh\" content=\"3; url=https://"+HOSTNAME+"/\">")
|
|
fmt.Fprintln(w, " </head>")
|
|
fmt.Fprintln(w, " <body>")
|
|
fmt.Fprintln(w, " IPv4 MAY NOT WORK<br>")
|
|
fmt.Fprintln(w, " FORGE REQUIRES IPv6.<br>")
|
|
fmt.Fprintln(w, " <br>")
|
|
fmt.Fprintln(w, " bad url", badurl, "<a href=\"https://forge.wit.com/\">redirecting</a>")
|
|
fmt.Fprintln(w, " </body>")
|
|
fmt.Fprintln(w, "</html>")
|
|
}
|