matrixprotobuf

git clone git://archive.git.mtrnord.blog/MTRNord/matrixprotobuf.git
Log | Files | Refs | README | LICENSE

commit 14321c990199b2898e633ae6ee4709f65f7e57c9
parent f146bc430f743b8b6b3275ac871a128340777406
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sat, 16 Sep 2017 23:54:59 +0200

Add Versions RPC Service

Signed-off-by: MTRNord <mtrnord1@gmail.com>

Diffstat:
Msrc/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.pb.go | 84++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.proto | 2+-
Msrc/github.com/Nordgedanken/MatrixProtoBuf/protobufS/protoServer.go | 9+++++----
Msrc/github.com/Nordgedanken/MatrixProtoBuf/rpc/rpcServer.go | 38++++++++++++++++++++++++++++++++++++--
Msrc/github.com/Nordgedanken/mpbRPCClient/client.go | 22+++++++++++++++++-----
5 files changed, 101 insertions(+), 54 deletions(-)

diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.pb.go b/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.pb.go @@ -204,64 +204,64 @@ var _ grpc.ClientConn // is compatible with the grpc package it is being compiled against. const _ = grpc.SupportPackageIsVersion4 -// Client API for VersioService service +// Client API for VersionsService service -type VersioServiceClient interface { +type VersionsServiceClient interface { Versions(ctx context.Context, in *VersionRequest, opts ...grpc.CallOption) (*VersionsResponse, error) } -type versioServiceClient struct { +type versionsServiceClient struct { cc *grpc.ClientConn } -func NewVersioServiceClient(cc *grpc.ClientConn) VersioServiceClient { - return &versioServiceClient{cc} +func NewVersionsServiceClient(cc *grpc.ClientConn) VersionsServiceClient { + return &versionsServiceClient{cc} } -func (c *versioServiceClient) Versions(ctx context.Context, in *VersionRequest, opts ...grpc.CallOption) (*VersionsResponse, error) { +func (c *versionsServiceClient) Versions(ctx context.Context, in *VersionRequest, opts ...grpc.CallOption) (*VersionsResponse, error) { out := new(VersionsResponse) - err := grpc.Invoke(ctx, "/matrixProtos.VersioService/Versions", in, out, c.cc, opts...) + err := grpc.Invoke(ctx, "/matrixProtos.VersionsService/Versions", in, out, c.cc, opts...) if err != nil { return nil, err } return out, nil } -// Server API for VersioService service +// Server API for VersionsService service -type VersioServiceServer interface { +type VersionsServiceServer interface { Versions(context.Context, *VersionRequest) (*VersionsResponse, error) } -func RegisterVersioServiceServer(s *grpc.Server, srv VersioServiceServer) { - s.RegisterService(&_VersioService_serviceDesc, srv) +func RegisterVersionsServiceServer(s *grpc.Server, srv VersionsServiceServer) { + s.RegisterService(&_VersionsService_serviceDesc, srv) } -func _VersioService_Versions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { +func _VersionsService_Versions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(VersionRequest) if err := dec(in); err != nil { return nil, err } if interceptor == nil { - return srv.(VersioServiceServer).Versions(ctx, in) + return srv.(VersionsServiceServer).Versions(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: "/matrixProtos.VersioService/Versions", + FullMethod: "/matrixProtos.VersionsService/Versions", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(VersioServiceServer).Versions(ctx, req.(*VersionRequest)) + return srv.(VersionsServiceServer).Versions(ctx, req.(*VersionRequest)) } return interceptor(ctx, in, info, handler) } -var _VersioService_serviceDesc = grpc.ServiceDesc{ - ServiceName: "matrixProtos.VersioService", - HandlerType: (*VersioServiceServer)(nil), +var _VersionsService_serviceDesc = grpc.ServiceDesc{ + ServiceName: "matrixProtos.VersionsService", + HandlerType: (*VersionsServiceServer)(nil), Methods: []grpc.MethodDesc{ { MethodName: "Versions", - Handler: _VersioService_Versions_Handler, + Handler: _VersionsService_Versions_Handler, }, }, Streams: []grpc.StreamDesc{}, @@ -336,27 +336,27 @@ func init() { proto.RegisterFile("matrix.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ // 357 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0x4f, 0x4b, 0xf3, 0x40, - 0x10, 0xc6, 0xc9, 0xdb, 0x36, 0xe9, 0x3b, 0x4d, 0x5f, 0x5e, 0x16, 0xff, 0x2c, 0x51, 0xb4, 0xa6, - 0x97, 0xe2, 0xa1, 0x60, 0xfd, 0x02, 0x5e, 0x04, 0x05, 0x0f, 0xa5, 0x8a, 0xe0, 0xa9, 0xc4, 0x64, - 0xb4, 0x41, 0xd2, 0x8d, 0x3b, 0x69, 0xd5, 0xbb, 0x5f, 0xcc, 0x6f, 0x26, 0x9b, 0x9d, 0x0d, 0x2d, - 0xf4, 0xb6, 0xcf, 0x6f, 0x9f, 0x99, 0x3c, 0x99, 0x59, 0x08, 0x8b, 0xa4, 0xd2, 0xf9, 0xe7, 0xb8, - 0xd4, 0xaa, 0x52, 0x82, 0xd5, 0xd4, 0x08, 0x8a, 0xaf, 0xe1, 0xff, 0x23, 0x6a, 0xca, 0xd5, 0x92, - 0x66, 0x48, 0xa5, 0x5a, 0x12, 0x8a, 0x0b, 0xe8, 0xae, 0x99, 0x49, 0x6f, 0xd0, 0x1a, 0xf5, 0x26, - 0xfb, 0xe3, 0xcd, 0xa2, 0x31, 0x57, 0xcc, 0x1a, 0x5b, 0x3c, 0x84, 0x80, 0xa1, 0x90, 0x10, 0x30, - 0x96, 0xde, 0xc0, 0x1b, 0xfd, 0x9d, 0x39, 0x19, 0x9f, 0xc3, 0x3f, 0x57, 0x89, 0xef, 0x2b, 0xa4, - 0xca, 0x78, 0x93, 0x2c, 0xd3, 0x48, 0xe4, 0xbc, 0x2c, 0xe3, 0x6f, 0x0f, 0xc2, 0x3b, 0xf5, 0x9a, - 0x37, 0x56, 0x01, 0xed, 0xea, 0xab, 0x44, 0xf6, 0xd5, 0x67, 0xc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 0xb4, 0x02, 0x00, 0x00, + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0xc1, 0x4e, 0xe3, 0x30, + 0x10, 0x86, 0x95, 0x6d, 0x9b, 0x74, 0xa7, 0xe9, 0xee, 0xca, 0x5a, 0xc0, 0x0a, 0x08, 0x4a, 0x7a, + 0xa9, 0x38, 0x54, 0xa2, 0xbc, 0x00, 0x17, 0x24, 0x90, 0x38, 0x54, 0x05, 0x71, 0xe1, 0x50, 0x85, + 0x64, 0xa0, 0x11, 0x4a, 0x1d, 0x3c, 0x69, 0x81, 0x3b, 0x2f, 0xc6, 0x9b, 0x21, 0xc7, 0xe3, 0xa8, + 0x95, 0x7a, 0xf3, 0xff, 0xcd, 0x3f, 0x93, 0x3f, 0x1e, 0x43, 0x58, 0x24, 0x95, 0xce, 0x3f, 0xc6, + 0xa5, 0x56, 0x95, 0x12, 0xac, 0xa6, 0x46, 0x50, 0x7c, 0x05, 0xff, 0x1e, 0x50, 0x53, 0xae, 0x96, + 0x34, 0x43, 0x2a, 0xd5, 0x92, 0x50, 0x9c, 0x43, 0x77, 0xcd, 0x4c, 0x7a, 0x83, 0xd6, 0xa8, 0x37, + 0xd9, 0x1b, 0x6f, 0x36, 0x8d, 0xb9, 0x63, 0xd6, 0xd8, 0xe2, 0x21, 0x04, 0x0c, 0x85, 0x84, 0x80, + 0xb1, 0xf4, 0x06, 0xde, 0xe8, 0xf7, 0xcc, 0xc9, 0xf8, 0x0c, 0xfe, 0xb8, 0x4e, 0x7c, 0x5b, 0x21, + 0x55, 0xc6, 0x9b, 0x64, 0x99, 0x46, 0x22, 0xe7, 0x65, 0x19, 0x7f, 0x79, 0x10, 0xde, 0xaa, 0x97, + 0xbc, 0xb1, 0x0a, 0x68, 0x57, 0x9f, 0x25, 0xb2, 0xaf, 0x3e, 0x1b, 0xb6, 0x22, 0xd4, 0xf2, 0x97, + 0x65, 0xe6, 0x2c, 0x22, 0xe8, 0x96, 0x09, 0xd1, 0xbb, 0xd2, 0x99, 0x6c, 0xd5, 0xbc, 0xd1, 0x62, + 0x1f, 0xfc, 0x02, 0xb3, 0x7c, 0x55, 0xc8, 0x76, 0x5d, 0x61, 0xb5, 0x19, 0xa3, 0xb3, 0x1d, 0xe3, + 0xdb, 0x83, 0x3e, 0xc7, 0xe0, 0xcb, 0x39, 0x85, 0x30, 0x49, 0x53, 0x24, 0x9a, 0x57, 0xea, 0x15, + 0xdd, 0x3f, 0xf6, 0x2c, 0xbb, 0x37, 0x48, 0x9c, 0x40, 0x6f, 0xa1, 0x0a, 0x9c, 0x13, 0xea, 0x75, + 0x93, 0x0e, 0x0c, 0xba, 0xab, 0x89, 0x38, 0x80, 0xc0, 0x64, 0x9d, 0xe7, 0x2e, 0xa2, 0x6f, 0xe4, + 0x4d, 0x26, 0x86, 0xd0, 0xd7, 0xf8, 0xac, 0x91, 0x16, 0x3c, 0xdd, 0xe6, 0x0c, 0x19, 0xda, 0xf1, + 0x12, 0x02, 0xd4, 0x3a, 0x55, 0x19, 0xba, 0xb4, 0x2c, 0xc5, 0x7f, 0xe8, 0xa0, 0xd6, 0x4a, 0x4b, + 0xbf, 0xe6, 0x56, 0x4c, 0x1e, 0xe1, 0xaf, 0x5b, 0xb1, 0xf9, 0x7e, 0x9e, 0xa2, 0xb8, 0x86, 0xae, + 0x43, 0xe2, 0x68, 0xf7, 0x6e, 0xed, 0xb5, 0x47, 0xc7, 0x3b, 0xab, 0xcd, 0x5b, 0x99, 0x4c, 0x79, + 0x4d, 0x6e, 0xf2, 0x25, 0x74, 0x6a, 0x2d, 0xa2, 0xed, 0xc6, 0xcd, 0x5d, 0x46, 0x87, 0x3b, 0x6b, + 0x76, 0xe2, 0x93, 0x5f, 0x3f, 0xd3, 0x8b, 0x9f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x58, 0xc4, 0x47, + 0xdc, 0xb6, 0x02, 0x00, 0x00, } diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.proto b/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.proto @@ -1,7 +1,7 @@ syntax = "proto3"; package matrixProtos; -service VersioService { +service VersionsService { rpc Versions (VersionRequest) returns (VersionsResponse); } diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/protobufS/protoServer.go b/src/github.com/Nordgedanken/MatrixProtoBuf/protobufS/protoServer.go @@ -13,7 +13,11 @@ import ( ) func StartProtoServer() { - log.Println("Started ProtoBuf Server") + log.Println("Started ProtoBuf Proxy") + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadRequest) + }) http.HandleFunc("/versions", func(w http.ResponseWriter, r *http.Request) { var versionRequest pb.VersionRequest @@ -36,14 +40,11 @@ func StartProtoServer() { fmt.Println(err) } - fmt.Println(hsVersions) - resp := &pb.VersionsResponse{ Versions: []*pb.Version{}, } for _, k := range hsVersions.Versions { - fmt.Println(k) resp.Versions = append(resp.Versions, &pb.Version{Version: k}) } diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/rpc/rpcServer.go b/src/github.com/Nordgedanken/MatrixProtoBuf/rpc/rpcServer.go @@ -1,26 +1,59 @@ package rpc import ( + "encoding/json" + "fmt" + "github.com/Nordgedanken/MatrixProtoBuf/jsonTypes" pb "github.com/Nordgedanken/MatrixProtoBuf/matrixProtos" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/reflection" + "io/ioutil" "log" "net" + "net/http" ) const ( port = ":50051" ) -// server is used to implement helloworld.LoginServiceServer. +// server is used to implement pb.LoginServiceServer. type loginServ struct{} -// Login implements helloworld.LoginServiceServer +// Login implements pb.VersionsServiceServer func (s *loginServ) Login(_ context.Context, r *pb.LoginRequest) (*pb.LoginResponse, error) { return &pb.LoginResponse{"test", "localhost", r.User, "test", "", ""}, nil } +// server is used to implement pb.LoginServiceServer. +type versionsServ struct{} + +// Login implements pb.VersionsServiceServer +func (s *versionsServ) Versions(_ context.Context, r *pb.VersionRequest) (*pb.VersionsResponse, error) { + var hsVersions jsonTypes.HSVersions + reqresp, err := http.Get("https://" + r.Address + "/_matrix/client/versions") + if err != nil { + panic(err) + } + defer reqresp.Body.Close() + body, err := ioutil.ReadAll(reqresp.Body) + + if err = json.Unmarshal(body, &hsVersions); err != nil { + fmt.Println(err) + } + + resp := &pb.VersionsResponse{ + Versions: []*pb.Version{}, + } + + for _, k := range hsVersions.Versions { + resp.Versions = append(resp.Versions, &pb.Version{Version: k}) + } + + return resp, nil +} + func StartRPCServer() { log.Println("Started RPC Proxy") lis, err := net.Listen("tcp", port) @@ -29,6 +62,7 @@ func StartRPCServer() { } s := grpc.NewServer() pb.RegisterLoginServiceServer(s, &loginServ{}) + pb.RegisterVersionsServiceServer(s, &versionsServ{}) // Register reflection service on gRPC server. reflection.Register(s) if err := s.Serve(lis); err != nil { diff --git a/src/github.com/Nordgedanken/mpbRPCClient/client.go b/src/github.com/Nordgedanken/mpbRPCClient/client.go @@ -1,11 +1,10 @@ package main import ( - "log" - pb "github.com/Nordgedanken/MatrixProtoBuf/matrixProtos" "golang.org/x/net/context" "google.golang.org/grpc" + "log" ) const ( @@ -19,12 +18,25 @@ func main() { log.Fatalf("did not connect: %v", err) } defer conn.Close() - c := pb.NewLoginServiceClient(conn) + lc := pb.NewLoginServiceClient(conn) // Contact the server and print out its response. - r, err := c.Login(context.Background(), &pb.LoginRequest{"m.password", "test", "", "", ""}) + lr, err := lc.Login(context.Background(), &pb.LoginRequest{"m.password", "test", "", "", ""}) if err != nil { log.Fatalf("could not greet: %v", err) } - log.Printf("Greeting: %s", r.String()) + log.Printf("Greeting: %s", lr.String()) + + vc := pb.NewVersionsServiceClient(conn) + + // Contact the server and print out its response. + vr, err := vc.Versions(context.Background(), &pb.VersionRequest{Address: "matrix.org"}) + if err != nil { + log.Fatalf("could not greet: %v", err) + } + + log.Println("Versions:") + for _, k := range vr.Versions { + log.Println(k.Version) + } }