summaryrefslogtreecommitdiff
path: root/handlePatches.go
blob: f270d74c6bc2b088edf0b930fbc2b8c963698d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main

import (
	"net/http"
	"strings"

	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/lib/protobuf/httppb"
	"go.wit.com/log"
)

func handlePatches(w http.ResponseWriter, r *http.Request, data []byte) error {
	pb, err := marshalPatchesPB(r, data)
	if err != nil {
		return sendPatchesError(w, pb, err)
	}

	route := pb.HttpRequest.Route
	if route == "" {
		route = cleanURL(r.URL.Path)
	}
	if route == "" {
		route = "route was blank"
	}
	log.Info("GOT PATCHES ROUTE", route, "with # patches =", pb.Len())
	if strings.HasPrefix(route, "/patches/old") {
		processPatchesOldPB(r, pb)
	} else if strings.HasPrefix(route, "/patches/new") {
		log.Info("add new patches")
	} else {
		log.Info("unknown route", route)
	}

	return nil
}

func makePatchesPB(reqPB *httppb.HttpRequest) (*forgepb.Patches, error) {
	pb := forgepb.NewPatches()
	err := pb.Unmarshal(reqPB.Body)
	return pb, err
}

func sendPatchesError(w http.ResponseWriter, r *forgepb.Patches, err error) error {
	log.Info("send error back to user", err)
	return nil
}

func processPatchesOldPB(r *http.Request, pb *forgepb.Patches) error {
	log.Info("check out these patches")
	pb.PrintTable()
	return nil
}

func marshalPatchesPB(r *http.Request, msg []byte) (*forgepb.Patches, error) {
	pb := forgepb.NewPatches()

	if err := pb.Unmarshal(msg); err != nil {
		log.Info("proto.Unmarshal() failed on wire message len", len(msg), err)
		// add the header
		pb.AddHttpToPB(r)
		return pb, err
	}
	// add the header
	pb.AddHttpToPB(r)
	return pb, nil
}