0

Tech-stack: HyperledgerFabric 1.4, Project-repository- FirstNetwork under FabricSamples Go – v1.15 , Goa- v3.2.5

Requirement: Using Goa framework, I need to trigger a Post request(http://localhost:8000/createChannelProfile) and it has to return the content of the file as a response. The user will input the channelName and channelProfile from frontend. Basically the routing happens this way ,triggers rest-api request-- this method has to call the script file---- the command in the script file should generate a file---that file has to be sent as a response. The point where I am stuck right now is the command in the script file is not getting executed. It is exiting with status code 127 and returning “failed” response. As part of first trial i'm just returning the string as a response instead of a generated output file. I am kinda new to go and Goa, still figuring out how to send a file as a response.If anyone knew please try to give me a hint. These are the commands i have executed as per GOA documentation: goa gen GoApp2/design, goa example GoApp2/design, go build ./cmd/fabric, ./fabric

I have tried with the similar solutions from stack overflow. But not able to resolve the error.

 design.go 
    
    package design
    
    import (
        "fmt"
        . "goa.design/goa/v3/dsl"
    )
    
    var _ = API("fabric", func() {
        Title("An api for fabric")
        Description("A simple goa service")
        Server("fabric", func() {
            Host("localhost", func() {
                URI("http://localhost:8000")
            })
        })
    })
    
    var _ = Service("fabric", func() {
        Description("The service to create a channel.tx file under channel-artifacts folder")
        //Method to add a new Channel Profile
        Method("create", func() {
            Payload(func() {
                Field(1, "channelName", String, "Name of the channel")
                Field(2, "channelProfile", String, "Name of the channel profile")
                Required("channelName", "channelProfile")
            })
            Result(String)
            Error("not_found", ErrorResult, "channelProfile not found")
            HTTP(func() {
                POST("/createChannelProfile")
                Response(StatusCreated)
            })
        })
        Files("/openapi.json", "./gen/http/openapi.json")
    })

fabric.go

//This is the file which calls the generateChannel.sh file

package fabricapi

import (
    fabric "GoApp2/gen/fabric"
    "context"
    "fmt"
    "log"
    "os/exec"
)

// fabric service example implementation.
// The example methods log the requests and return zero values.
type fabricsrvc struct {
    logger *log.Logger
}

// NewFabric returns the fabric service implementation.
func NewFabric(logger *log.Logger) fabric.Service {
    return &fabricsrvc{logger}
}

// Create implements create.
func (s *fabricsrvc) Create(ctx context.Context, p *fabric.CreatePayload) (res string, err error) {
    s.logger.Print("fabric.create")
    cmd := exec.Command("/bin/bash", "../generateChannel.sh", p.ChannelName, p.ChannelProfile)
    stdout, err := cmd.Output()
    fmt.Errorf("error %s", err)
    s.logger.Print(p.ChannelName)
    s.logger.Print(p.ChannelProfile)
    output := string(stdout)
    s.logger.Print(output)
    res = output

    return res, nil
}

generateChannel.sh file

#!/bin/bash
export CHANNELNAME="$1"
export CHANNELPROFILE="$2"

../bin/configtxgen -profile "${CHANNELPROFILE}" -outputCreateChannelTx "./channel-artifacts/${CHANNELNAME}.tx" -channelID "${CHANNELNAME}"
res=$?
echo ${res}
if [ $res -eq 0 ]; then
  echo "success"
  else
  echo "failed"
fi

Note: I have tested alone this file, it is executing perfectly. The command above in the script file looks for channelProfile and channelName which are defined in the configtx.yaml file and accordingly generates the channel.tx file.I have defined the environment variables and added the paths to the .bashrc and .profile files. Please help me where i am going wrong.

screenshot of go env screenshot of go env

Output from Postman:

Output from Postman:

Output from Terminal:

:~/workspace/fabric-samples/first-network/GoApp2$ ./fabric
[fabricapi] 12:47:59 HTTP "Create" mounted on POST /createChannelProfile
[fabricapi] 12:47:59 HTTP "./gen/http/openapi.json" mounted on GET /openapi.json
[fabricapi] 12:47:59 HTTP server listening on "localhost:8000"
[fabricapi] 12:48:05 id=95ISzo9L req=POST /createChannelProfile from=127.0.0.1
[fabricapi] 12:48:05 fabric.create
[fabricapi] 12:48:06 mychannel
[fabricapi] 12:48:06 TwoOrgsChannel
[fabricapi] 12:48:06 127
failed
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Archana
  • 1
  • 2
  • I would recommend using `cmd.CombinedOutput()` since bash is likely telling you why it's responding with a 127. You might also want to add `set -ex` to your script so that you can get better visbility into what's running. I would guess that you're in the wrong relative location for `../bin/configtxgen` to work – maxm Dec 06 '20 at 15:41
  • Thanks maxm. Yes I have resolved it after two days of posting the question.Resolved it as the same way you told by capturing stderr using Combined Output. I have posted the same question in go forum. There they have suggested to use combinedOutput. I was seeing the path of ../bin/configtxgen from the file wherein, it is defined, but actually it should be from the folder where the execution is being done. – Archana Dec 08 '20 at 07:04
  • lovely, glad to hear you got it resolved – maxm Dec 08 '20 at 13:22

0 Answers0