Skip to content

Commit

Permalink
Fix grpc connection
Browse files Browse the repository at this point in the history
  • Loading branch information
askolesov committed May 13, 2022
1 parent 47f2513 commit 6d75972
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
6 changes: 3 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func serve() {
e.Use(middleware.Recover())

// Services
ledgerService := services.NewLedgerService()
ledgerService := services.NewLedgerService(config.Ledger.Timeout)

networks := strings.Split(config.Resolver.Networks, ";")
networks := strings.Split(config.Ledger.Networks, ";")
for _, network := range networks {
args := strings.Split(network, "=")
name, url := args[0], args[1]
Expand Down Expand Up @@ -86,7 +86,7 @@ func serve() {

log.Debug().Msgf("Response body: %s", responseBody)

c.Response().Header().Set(echo.HeaderContentType, accept)
c.Response().Header().Set(echo.HeaderContentType, string(requestedContentType))
return c.JSONBlob(http.StatusOK, []byte(responseBody))
})

Expand Down
6 changes: 4 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
ledger:
networks: "mainnet=rpc.cheqd.net:443;testnet=159.89.208.88:443"
timeout: "5s"

resolver:
method: "cheqd"
networks: "mainnet=rpc.cheqd.net:443;testnet=159.89.208.88:443"
ledgerTimeout: "5s"

api:
listener: "0.0.0.0:1313"
Expand Down
29 changes: 20 additions & 9 deletions services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import (
cheqd "github.com/cheqd/cheqd-node/x/cheqd/types"
cheqdUtils "github.com/cheqd/cheqd-node/x/cheqd/utils"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"time"
)

type LedgerServiceI interface {
QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool, error)
GetNamespaces() []string
}
type LedgerService struct {
ledgers map[string]string // namespace -> url
ledgers map[string]string // namespace -> url
connectionTimeout time.Duration
}

func NewLedgerService() LedgerService {
ls := LedgerService{}
func NewLedgerService(connectionTimeout time.Duration) LedgerService {
ls := LedgerService{
connectionTimeout: connectionTimeout,
}
ls.ledgers = make(map[string]string)
return ls
}
Expand All @@ -33,12 +37,19 @@ func (ls LedgerService) QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool
}

log.Info().Msgf("Connecting to the ledger: %s", serverAddr)
conn, err := openGRPCConnection(serverAddr)
conn, err := ls.openGRPCConnection(serverAddr)
if err != nil {
log.Error().Err(err).Msg("QueryDIDDoc: failed connection")
return cheqd.Did{}, cheqd.Metadata{}, false, err
}
defer conn.Close()

defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.Panic().Err(err).Msg("QueryDIDDoc: failed to close connection")
panic(err)
}
}(conn)

log.Info().Msgf("Querying did doc: %s", did)
client := cheqd.NewQueryClient(conn)
Expand All @@ -65,12 +76,12 @@ func (ls *LedgerService) RegisterLedger(namespace string, url string) error {
return nil
}

func openGRPCConnection(addr string) (conn *grpc.ClientConn, err error) {
func (ls LedgerService) openGRPCConnection(addr string) (conn *grpc.ClientConn, err error) {
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
}
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("ledgerTimeout"))
ctx, cancel := context.WithTimeout(context.Background(), ls.connectionTimeout)
defer cancel()

conn, err = grpc.DialContext(ctx, addr, opts...)
Expand Down
24 changes: 12 additions & 12 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ func createJsonResolution(didDoc string, metadata string, resolutionMetadata str
}

response := struct {
didDocument json.RawMessage
didDocumentMetadata json.RawMessage
didResolutionMetadata json.RawMessage
DidDocument json.RawMessage `json:"didDocument"`
DidDocumentMetadata json.RawMessage `json:"didDocumentMetadata"`
DidResolutionMetadata json.RawMessage `json:"didResolutionMetadata"`
}{
didDocument: json.RawMessage(didDoc),
didDocumentMetadata: json.RawMessage(metadata),
didResolutionMetadata: json.RawMessage(resolutionMetadata),
DidDocument: json.RawMessage(didDoc),
DidDocumentMetadata: json.RawMessage(metadata),
DidResolutionMetadata: json.RawMessage(resolutionMetadata),
}

respJson, err := json.Marshal(&response)
Expand All @@ -203,13 +203,13 @@ func createJsonDereferencing(contentStream string, metadata string, dereferencin
}

response := struct {
contentStream json.RawMessage
contentMetadata json.RawMessage
dereferencingMetadata json.RawMessage
ContentStream json.RawMessage `json:"contentStream"`
ContentMetadata json.RawMessage `json:"contentMetadata"`
DereferencingMetadata json.RawMessage `json:"dereferencingMetadata"`
}{
contentStream: json.RawMessage(contentStream),
contentMetadata: json.RawMessage(metadata),
dereferencingMetadata: json.RawMessage(dereferencingMetadata),
ContentStream: json.RawMessage(contentStream),
ContentMetadata: json.RawMessage(metadata),
DereferencingMetadata: json.RawMessage(dereferencingMetadata),
}

respJson, err := json.Marshal(&response)
Expand Down
17 changes: 11 additions & 6 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@ package types
import (
"encoding/json"
"gopkg.in/yaml.v3"
"time"
)

type Config struct {
Ledger LedgerConfig
Resolver ResolverConfig
Api ApiConfig

LogLevel string
}

type LedgerConfig struct {
Networks string
Timeout time.Duration
}

type ResolverConfig struct {
Method string
Networks string
LedgerTimeout string
Method string
}

type ApiConfig struct {
Listener string
ResolverPath string
}

func(c *Config) MarshalYaml() (string, error) {
func (c *Config) MarshalYaml() (string, error) {
bytes, err := yaml.Marshal(c)
return string(bytes), err
}
Expand All @@ -37,7 +42,7 @@ func (c *Config) MustMarshalYaml() string {
return res
}

func(c *Config) MarshalJson() (string, error) {
func (c *Config) MarshalJson() (string, error) {
bytes, err := json.Marshal(c)
return string(bytes), err
}
Expand All @@ -49,4 +54,4 @@ func (c *Config) MustMarshalJson() string {
}

return res
}
}

0 comments on commit 6d75972

Please sign in to comment.