Skip to content

Commit

Permalink
chore: reorganize go code
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Oct 10, 2019
1 parent 273c11a commit 688a109
Show file tree
Hide file tree
Showing 20 changed files with 250 additions and 202 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Expand Up @@ -37,7 +37,9 @@ jobs:
go-generate:
working_directory: /go/src/berty.tech
docker:
- image: bertytech/protoc:18
- image: bertytech/protoc:19
environment:
- GO111MODULE=on
steps:
- checkout
- run: find . -name gen.sum -delete
Expand Down
5 changes: 4 additions & 1 deletion api/bertyprotocol.proto
Expand Up @@ -195,7 +195,10 @@ message AccountGetConfigurationRequest {}
message AccountGetConfigurationReply {}

message AccountGetInformationRequest {}
message AccountGetInformationReply {}
message AccountGetInformationReply {
string peer_id = 1 [(gogoproto.customname) = "PeerID"];
repeated string listeners = 2;
}

message AccountLinkNewDeviceRequest {}
message AccountLinkNewDeviceReply {}
Expand Down
2 changes: 1 addition & 1 deletion docs/Makefile
Expand Up @@ -23,7 +23,7 @@ $(GEN_SUM): $(GEN_SRC)
--workdir="/go/src/berty.tech/docs" \
--entrypoint="sh" \
--rm \
bertytech/protoc:18 \
bertytech/protoc:19 \
-xec 'make generate_local' \
)

Expand Down
13 changes: 12 additions & 1 deletion docs/bertyprotocol.swagger.json

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

4 changes: 2 additions & 2 deletions docs/gen.sum

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

3 changes: 0 additions & 3 deletions go/.golangci.yml
Expand Up @@ -4,9 +4,6 @@ run:
skip-files:
- ".*\\.pb\\.go"
- ".*\\.gen\\.go"
- "core/sql/migrations/.*"
- "core/sql/migrations/.*/.*"
- "client/react-native/gomobile/other.go"

linters-settings:
golint:
Expand Down
2 changes: 1 addition & 1 deletion go/Makefile
Expand Up @@ -46,7 +46,7 @@ $(GEN_SUM): $(GEN_SRC)
--workdir="/go/src/berty.tech/go" \
--entrypoint="sh" \
--rm \
bertytech/protoc:18 \
bertytech/protoc:19 \
-xec 'make generate_local'; \
make tidy \
)
Expand Down
40 changes: 12 additions & 28 deletions go/cmd/bertychat/main.go
Expand Up @@ -11,9 +11,6 @@ import (

"berty.tech/go/internal/banner"
_ "berty.tech/go/internal/buildconstraints" // fail if bad go version
"berty.tech/go/internal/chatdb"
"berty.tech/go/internal/ipfsutil"
"berty.tech/go/internal/protocoldb"
"berty.tech/go/pkg/bertychat"
"berty.tech/go/pkg/bertyprotocol"
"github.com/jinzhu/gorm"
Expand Down Expand Up @@ -101,6 +98,8 @@ func main() {
return err
}

ctx := context.Background()

// protocol
var protocol bertyprotocol.Client
{
Expand All @@ -111,34 +110,20 @@ func main() {
}
defer db.Close()

// initialize datastore
db, err = protocoldb.InitMigrate(db, logger.Named("datastore"))
if err != nil {
return errors.Wrap(err, "failed to initialize datastore")
}

// initialize ipfs
coreapi, err := ipfsutil.NewInMemoryCoreAPI(context.TODO())
if err != nil {
return errors.Wrap(err, "failed to initialize ipfsutil")
}

// initialize new protocol client
protocolOpts := bertyprotocol.Opts{
opts := bertyprotocol.Opts{
Logger: logger.Named("bertyprotocol"),
}
protocol, err = bertyprotocol.New(db, coreapi, protocolOpts)
protocol, err = bertyprotocol.New(db, opts)
if err != nil {
return errors.Wrap(err, "failed to initialize protocol")
}

// log ipfs informations
protocol.LogIPFSInformations()

defer protocol.Close()
}

// chat
var chat bertychat.Client
{
// initialize sqlite3 gorm database
db, err := gorm.Open("sqlite3", *clientChatURN)
Expand All @@ -147,25 +132,24 @@ func main() {
}
defer db.Close()

// initialize datastore
db, err = chatdb.InitMigrate(db, logger.Named("datastore"))
if err != nil {
return errors.Wrap(err, "failed to initialize datastore")
}

// initialize bertychat client
chatOpts := bertychat.Opts{
Logger: logger.Named("bertychat"),
}
chat, err := bertychat.New(db, protocol, chatOpts)
chat, err = bertychat.New(db, protocol, chatOpts)
if err != nil {
return errors.Wrap(err, "failed to initialize chat")
}

defer chat.Close()
}

logger.Info("client initialized, now starting... not implemented.")
info, err := protocol.AccountGetInformation(ctx, nil)
if err != nil {
return errors.Wrap(err, "failed to get protocol information")
}

logger.Info("client initialized", zap.String("peer-id", info.PeerID), zap.Strings("listeners", info.Listeners))
return nil
},
}
Expand Down
4 changes: 2 additions & 2 deletions go/gen.sum

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

17 changes: 17 additions & 0 deletions go/go.sum

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

3 changes: 1 addition & 2 deletions go/internal/ipfsutil/api_inmemory.go
Expand Up @@ -14,13 +14,12 @@ import (
ipfs_libp2p "github.com/ipfs/go-ipfs/core/node/libp2p"
ipfs_repo "github.com/ipfs/go-ipfs/repo"
ipfs_interface "github.com/ipfs/interface-go-ipfs-core"

libp2p_ci "github.com/libp2p/go-libp2p-crypto" // nolint:staticcheck
libp2p_peer "github.com/libp2p/go-libp2p-peer" // nolint:staticcheck

"github.com/pkg/errors"
)

// NewInMemoryCoreAPI returns an IPFS CoreAPI based on an opininated ipfs_node.BuildCfg
func NewInMemoryCoreAPI(ctx context.Context) (ipfs_interface.CoreAPI, error) {
cfg, err := createBuildConfig()
if err != nil {
Expand Down
Expand Up @@ -2,25 +2,27 @@ package ipfsutil

import (
"context"
"testing"

ipfs_core "github.com/ipfs/go-ipfs/core"
ipfs_coreapi "github.com/ipfs/go-ipfs/core/coreapi"
ipfs_mock "github.com/ipfs/go-ipfs/core/mock"
ipfs_interface "github.com/ipfs/interface-go-ipfs-core"

libp2p_mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"

"github.com/pkg/errors"
)

type MockedCoreAPI interface {
// CoreAPIMock implements ipfs.CoreAPI and adds some debugging helpers
type CoreAPIMock interface {
ipfs_interface.CoreAPI

MockNetwork() libp2p_mocknet.Mocknet
MockNode() *ipfs_core.IpfsNode
}

func NewMockCoreAPI(ctx context.Context) (MockedCoreAPI, error) {
// TestingCoreAPI returns a fully initialized mocked Core API
func TestingCoreAPI(ctx context.Context, t *testing.T) CoreAPIMock {
t.Helper()

mocknet := libp2p_mocknet.New(ctx)
node, err := ipfs_core.NewNode(ctx, &ipfs_core.BuildCfg{
Online: true,
Expand All @@ -29,30 +31,29 @@ func NewMockCoreAPI(ctx context.Context) (MockedCoreAPI, error) {
"pubsub": true,
},
})

if err != nil {
return nil, errors.Wrap(err, "failed to mocked ipfs node")
t.Fatalf("failed to initialize IPFS node mock: %v", err)
}

coreapi, err := ipfs_coreapi.NewCoreAPI(node)
if err != nil {
return nil, errors.Wrap(err, "failed to init coreapi")
t.Fatalf("failed to initialize IPFS Core API mock: %v", err)
}

return &mockedCoreAPI{coreapi, mocknet, node}, nil
return &coreAPIMock{coreapi, mocknet, node}
}

type mockedCoreAPI struct {
type coreAPIMock struct {
ipfs_interface.CoreAPI

mocknet libp2p_mocknet.Mocknet
node *ipfs_core.IpfsNode
}

func (m *mockedCoreAPI) MockNetwork() libp2p_mocknet.Mocknet {
func (m *coreAPIMock) MockNetwork() libp2p_mocknet.Mocknet {
return m.mocknet
}

func (m *mockedCoreAPI) MockNode() *ipfs_core.IpfsNode {
func (m *coreAPIMock) MockNode() *ipfs_core.IpfsNode {
return m.node
}

0 comments on commit 688a109

Please sign in to comment.