Skip to content

Commit

Permalink
feat(core): initial version of the client library (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jul 28, 2018
1 parent dc22e3c commit 4a803a0
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 90 deletions.
23 changes: 19 additions & 4 deletions core/Makefile
Expand Up @@ -3,13 +3,18 @@ GOPATH ?= $(HOME)/go
BIN = $(GOPATH)/bin/berty
CODE_PATHS = $(filter-out node_modules/,$(wildcard */))
PROTOS = $(call rwildcard, $(CODE_PATHS), *.proto)
GENERATED_FILES = $(patsubst %.proto,%.pb.go,$(PROTOS))
SERVICE_PROTOS = $(call rwildcard, api, *.proto)
GENERATED_FILES = $(patsubst %.proto,%.pb.go,$(PROTOS)) client/berty.node.service.gen.go $(rwildcard $(CODE_PATHS),*.gen.go)
PROTOC_OPTS = --proto_path=../vendor:../vendor/github.com/gogo/protobuf:.
CGO_LDFLAGS ?= -L/usr/local/opt/openssl/lib
CGO_CPPFLAGS ?= -I/usr/local/opt/openssl/include
BUILD_ENV ?= CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CPPFLAGS="$(CGO_CPPFLAGS)"
RUN_DAEMON_OPTS ?= --log-level=debug

##
## phonies
##

.PHONY: all
all: run

Expand All @@ -27,9 +32,6 @@ $(BIN): $(GENERATED_FILES)
test: generate
$(BUILD_ENV) go test -test.timeout 30s -v ./...

%.pb.go: %.proto
protoc $(PROTOC_OPTS) --gofast_out=plugins=grpc:"$(GOPATH)/src" "$(dir $<)"/*.proto

.PHONY: clean
clean:
rm -f $(GENERATED_FILES) $(BIN)
Expand All @@ -41,3 +43,16 @@ generate: $(GENERATED_FILES)
_ci_prepare:
@# touching files to avoid regenerating files based on modification date
touch $(GENERATED_FILES)

##
## file-based rules
##

%.pb.go: %.proto
protoc $(PROTOC_OPTS) --gofast_out=plugins=grpc:"$(GOPATH)/src" "$(dir $<)"/*.proto

client/berty.node.service.gen.go: $(PROTOS)
for protodir in $(dir $(SERVICE_PROTOS)); do \
protoc $(PROTOC_OPTS) --gotemplate_out=debug=false,all=false,single-package-mode=true,template_dir=./client:./client $$protodir/*.proto; \
done
goimports -w ./client/
44 changes: 22 additions & 22 deletions core/api/node/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/api/node/service.proto
Expand Up @@ -3,7 +3,7 @@ syntax = "proto3";
package berty.node;

import "api/p2p/event.proto";
import "api/entity/contact.proto";
import "entity/contact.proto";

option go_package = "github.com/berty/berty/core/api/node";

Expand Down
53 changes: 26 additions & 27 deletions core/api/p2p/kind.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/api/p2p/kind.proto
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

package berty.p2p;

import "api/entity/contact.proto";
import "entity/contact.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/berty/berty/core/api/p2p";
Expand Down
12 changes: 12 additions & 0 deletions core/client/berty.node.service.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions core/client/berty.p2p.service.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions core/client/client.go
@@ -0,0 +1,11 @@
package client

import "google.golang.org/grpc"

type Client struct {
conn *grpc.ClientConn
}

func New(conn *grpc.ClientConn) *Client {
return &Client{conn: conn}
}
18 changes: 18 additions & 0 deletions core/client/{{.File.Package}}.{{.Service.Name|lower}}.gen.go.tmpl
@@ -0,0 +1,18 @@
// this file was generated by protoc-gen-gotemplate

package client

{{- $file := .File}}

import (
"github.com/berty/berty/core/{{.File.Name|dir}}"
)

{{- range $svc := .File.Service}}
{{- if eq ($svc.Name | lower) ($.Service.Name | lower)}}
// {{$svc.Name}} returns the native gRPC client
func (c *Client) {{$file.Package|splitList "."|last|title}}() {{$.File.Package|splitList "."|last}}.{{$svc.Name}}Client {
return {{$.File.Package|splitList "."|last}}.New{{$svc.Name}}Client(c.conn)
}

{{end}}{{end}}
64 changes: 32 additions & 32 deletions core/api/entity/contact.pb.go → core/entity/contact.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

package berty.entity;

option go_package = "github.com/berty/berty/core/api/entity";
option go_package = "github.com/berty/berty/core/entity";

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
Expand Down
2 changes: 1 addition & 1 deletion core/node/nodeapi.go
Expand Up @@ -6,8 +6,8 @@ import (

"google.golang.org/grpc"

"github.com/berty/berty/core/api/entity"
"github.com/berty/berty/core/api/node"
"github.com/berty/berty/core/entity"
)

// WithNodeGrpcServer registers the Node as a 'berty.node' protobuf server implementation
Expand Down
2 changes: 1 addition & 1 deletion core/sql/gorm.go
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/go-gormigrate/gormigrate"
"github.com/jinzhu/gorm"

"github.com/berty/berty/core/api/entity"
"github.com/berty/berty/core/entity"
)

// Init configures an active gorm connection
Expand Down

0 comments on commit 4a803a0

Please sign in to comment.