Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
[FAB-13285] Switch logger.Fatal to fmt.Print
Browse files Browse the repository at this point in the history
 - the fatal causes the error messages and usage information to be more
 difficult to parse.
 - Change references to FabProxy to Fab3.
 - remove descriptions in getEnvVar as not being used

Change-Id: I4636d65ed7c0de6aa815fbfc3e73760ce177dffc
Signed-off-by: Swetha Repakula <srepaku@us.ibm.com>
  • Loading branch information
swetharepakula committed Dec 18, 2018
1 parent 6bb8493 commit 94d314d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
44 changes: 25 additions & 19 deletions fabproxy/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package main

import (
"fmt"
"os"
"strconv"

Expand All @@ -19,7 +20,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
)

const usage = `FabProxy uses environment variables to be able to start communicating with a Fabric network
const usage = `Fab3 uses environment variables to be able to start communicating with a Fabric network
Required Environment Variables:
FABPROXY_CONFIG - Path to a compatible Fabric SDK Go config file
FABPROXY_USER - User identity being used for the proxy (Matches the users names in the crypto-config directory specified in the config)
Expand All @@ -28,67 +29,72 @@ const usage = `FabProxy uses environment variables to be able to start communica
FABPROXY_CCID - ID of the EVM Chaincode deployed in your fabric network
Other Environment Variables:
PORT - Port the FabProxy will be running on. Default is 5000
PORT - Port the Fab3 will be running on. Default is 5000
`

var logger *zap.SugaredLogger

func main() {
rawLogger, _ := zap.NewProduction()
logger := rawLogger.Named("fab3").Sugar()
logger = rawLogger.Named("fab3").Sugar()

cfg := grabEnvVar("FABPROXY_CONFIG", true, "Path to the Fabric SDK GO config file")
org := grabEnvVar("FABPROXY_ORG", true, "Org of the user specified")
user := grabEnvVar("FABPROXY_USER", true, "User to be used for proxy")
ch := grabEnvVar("FABPROXY_CHANNEL", true, "Channel transactions will be sent on")
ccid := grabEnvVar("FABPROXY_CCID", true, "Chaincode ID of the EVM chaincode")
port := grabEnvVar("PORT", false, "")
cfg := grabEnvVar("FABPROXY_CONFIG", true)
org := grabEnvVar("FABPROXY_ORG", true)
user := grabEnvVar("FABPROXY_USER", true)
ch := grabEnvVar("FABPROXY_CHANNEL", true)
ccid := grabEnvVar("FABPROXY_CCID", true)
port := grabEnvVar("PORT", false)

portNumber := 5000
if port != "" {
var err error
portNumber, err = strconv.Atoi(port)
if err != nil {
logger.Fatalf("Failed to convert the environment variable `PORT`, %s, to an int\n", port)
fmt.Fprintf(os.Stderr, "Failed to convert the environment variable `PORT`, %s, to an int\n", port)
os.Exit(1)
}
}

sdk, err := fabsdk.New(config.FromFile(cfg))
if err != nil {
logger.Fatalf("Failed to create Fabric SDK Client: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "Failed to create Fabric SDK Client: %s\n", err)
os.Exit(1)
}
defer sdk.Close()

clientChannelContext := sdk.ChannelContext(ch, fabsdk.WithUser(user), fabsdk.WithOrg(org))
client, err := channel.New(clientChannelContext)
if err != nil {
logger.Fatalf("Failed to create Fabric SDK Channel Client: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "Failed to create Fabric SDK Channel Client: %s\n", err)
os.Exit(1)
}

ledger, err := ledger.New(clientChannelContext)
if err != nil {
logger.Fatalf("Failed to create Fabric SDK Ledger Client: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "Failed to create Fabric SDK Ledger Client: %s\n", err)
os.Exit(1)
}

ethService := fabproxy.NewEthService(client, ledger, ch, ccid, logger)

logger.Infof("Starting Fab Proxy on port %d\n", portNumber)
logger.Infof("Starting Fab3 on port %d\n", portNumber)
proxy := fabproxy.NewFabProxy(ethService)
err = proxy.Start(portNumber)
if err != nil {
logger.Fatal(err)
fmt.Fprintf(os.Stderr, "Error starting Fab3: %s", err)
}
defer func() {
logger.Info("Shutting down the Fab Proxy")
logger.Info("Shutting down Fab3")
proxy.Shutdown()
logger.Info("Fab Proxy has exited")
logger.Info("Fab3 has exited")
}()
}

func grabEnvVar(varName string, required bool, description string) string {
func grabEnvVar(varName string, required bool) string {
envVar := os.Getenv(varName)
if required && envVar == "" {
logger.Fatalf("Fab Proxy requires the environment variable %s to be set\n\n%s\n\n", varName, usage)
fmt.Fprintf(os.Stderr, "Fab3 requires the environment variable %s to be set\n\n%s\n\n", varName, usage)
os.Exit(1)
}
return envVar
}
2 changes: 1 addition & 1 deletion integration/helpers/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func FabProxyRunner(fabproxyBinPath, proxyConfig, org, user, channel, ccid strin
config := ginkgomon.Config{
Name: fmt.Sprintf("fabproxy-%s-%s", org, user),
Command: cmd,
StartCheck: "Starting Fab Proxy on port",
StartCheck: "Starting Fab3 on port",
StartCheckTimeout: 15 * time.Second,
}

Expand Down

0 comments on commit 94d314d

Please # to comment.