server.go 2.34 KB
Newer Older
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
// Package server is the mock server, all functions must be redefined
package server

import (
	"crypto/rsa"
	"crypto/x509"
	"fmt"

	"dfss/dfssp/api"
	"dfss/mockp/fixtures"
	"dfss/net"

	"golang.org/x/net/context"
	"google.golang.org/grpc"
)

// mockServer empty struct to use grpc server
type mockServer struct{}

// Register handler
//
// Handle incoming RegisterRequest messages
func (s *mockServer) Register(ctx context.Context, in *api.RegisterRequest) (*api.ErrorCode, error) {
	if response, ok := fixtures.RegisterFixture[in.Email]; ok {
		return response, nil
	}
	return fixtures.RegisterFixture["default"], nil
}

// Auth handler
//
// Handle incoming AuthRequest messages
func (s *mockServer) Auth(ctx context.Context, in *api.AuthRequest) (*api.RegisteredUser, error) {
Axel's avatar
Axel committed
34
	return fixtures.AuthFixture["default"], nil
35
36
37
38
39
40
41
42
43
44
45
46
47
48
}

// Unregister handler
//
// Handle incoming UnregisterRequest messages
func (s *mockServer) Unregister(ctx context.Context, in *api.Empty) (*api.ErrorCode, error) {
	// TODO
	return nil, nil
}

// PostContract handler
//
// Handle incoming PostContractRequest messages
func (s *mockServer) PostContract(ctx context.Context, in *api.PostContractRequest) (*api.ErrorCode, error) {
Loïck Bonniot's avatar
Loïck Bonniot committed
49
	return fixtures.CreateFixture[in.Comment], nil
50
51
}

Loïck Bonniot's avatar
Loïck Bonniot committed
52
53
54
55
// GetContract handler
//
// Handle incoming GetContractRequest messages
func (s *mockServer) GetContract(ctx context.Context, in *api.GetContractRequest) (*api.Contract, error) {
Loïck Bonniot's avatar
Loïck Bonniot committed
56
	return fixtures.FetchFixture[in.Uuid], nil
Loïck Bonniot's avatar
Loïck Bonniot committed
57
58
}

59
60
61
// JoinSignature handler
//
// Handle incoming JoinSignatureRequest messages
62
func (s *mockServer) JoinSignature(in *api.JoinSignatureRequest, stream api.Platform_JoinSignatureServer) error {
63
	// TODO
64
	return nil
65
66
67
68
69
}

// ReadySign handler
//
// Handle incoming ReadySignRequest messages
70
func (s *mockServer) ReadySign(ctx context.Context, in *api.ReadySignRequest) (*api.LaunchSignature, error) {
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	// TODO
	return nil, nil
}

// GetServer returns the GRPC server associated with the platform
func GetServer(ca *x509.Certificate, pkey *rsa.PrivateKey) *grpc.Server {
	server := net.NewServer(ca, pkey, ca)
	api.RegisterPlatformServer(server, &mockServer{})
	return server
}

// Run the mock server on provided address and with provided ca
func Run(ca *x509.Certificate, pkey *rsa.PrivateKey, addrPort string) {
	srv := GetServer(ca, pkey)
	err := net.Listen(addrPort, srv)
	if err != nil {
		fmt.Println(err)
	}
}