summaryrefslogtreecommitdiff
path: root/handlePatches.go
blob: 904da497709c72d7b94c4375fd2315eb0edaf6b3 (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
package main

import (
	"io/ioutil"
	"net/http"
	"strings"

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

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

	route := pb.HttpRequest.Route
	if strings.HasPrefix(route, "/patches/old") {
		processPatchesPB(r, pb)
	} else if strings.HasPrefix(route, "/patches/old") {
		log.Info("add new patches")
	} else {
		log.Info("unknown route", route)
	}

	return nil
}

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

func processPatchesPB(r *http.Request, pb *forgepb.Patches) error {
	log.Info("send error back to user")
	return nil
}

func marshalPatchesPB(r *http.Request) (*forgepb.Patches, error) {
	pb := forgepb.NewPatches()
	msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
	defer r.Body.Close()
	if err != nil {
		return pb, err
	}

	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
}