/
notarizeserver.go
84 lines (70 loc) · 2.49 KB
/
notarizeserver.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"fmt"
"net/http"
"github.com/hashicorp/serf/cmd/serf/command/agent"
"github.com/vsekhar/fabula/pkg/api/servicepb"
pb "github.com/vsekhar/fabula/pkg/api/servicepb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// TODO: notarize server accepts requests from the public and forwards to the
// pack service
type notarizeServer struct {
*http.ServeMux
agent *agent.Agent
servicepb.UnimplementedFabulaServer
}
func newNotarizeServer(name string, a *agent.Agent) *notarizeServer {
mux := http.NewServeMux()
// TODO: url format
// TODO: view handlers: packs, entries, proofs
// notarization handlers
// TODO: POST handler for new notarization
// - do notarization
// - redirect to canonical URL for response
mux.HandleFunc("/v1/notarize", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.Header().Add("Allow", "POST")
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// TODO: notarization_sha3512 = hash(document, salt)
// TODO: lookup pack service for prefix = notarization_sha3512[:prefixLen]
// TODO: submit to Pack Service, block until done
// TODO: if fail, return error
// TODO: if success, get timestamp, finish commit wait, return success
// TODO: response includes: salt, notarization, prefix, timestamp, info
// about packs, debug info (commit-wait length), canonical URL
// TODO: if redirect=true, redirect to canonical URL for response
// perhaps using HTML5 window.history.replaceState()
fmt.Fprintf(w, "Hello world, from %s\n", name)
})
// system handlers
mux.HandleFunc("/v1/system/peers", func(w http.ResponseWriter, r *http.Request) {
members := a.Serf().Members()
for _, m := range members {
fmt.Fprintf(w, "%+v\n", m)
}
})
// liveness probe
mux.HandleFunc("/_liveness", func(w http.ResponseWriter, r *http.Request) {
// TODO: check for liveness
return
})
return ¬arizeServer{
ServeMux: mux,
agent: a,
}
}
func (s *notarizeServer) Notarize(ctx context.Context, r *pb.NotarizeRequest) (*pb.NotarizeResponse, error) {
// TODO: notarization_sha3512 = hash(prior, document, timestamp)
// TODO: submit to Pack Service, block until done
// TODO: if fail, return error
// TODO: if success, get timestamp, finish commit wait, return success
return nil, status.Error(codes.Unimplemented, "unimplemented")
}
func (s *notarizeServer) Root(ctx context.Context, r *pb.RootRequest) (*pb.RootResponse, error) {
return &pb.RootResponse{Message: "Root"}, nil
}