使用goa-webFramework从Go执行bash脚本/命令未执行

技术堆栈:HyperledgerFabric 1.4,项目存储库-FabricSamples下的FirstNetwork Go – v1.15,goa- v3.2.5

需求:使用goa框架,我需要触发一个Post请求(http:// localhost:8000 / createChannelProfile),并且它必须返回文件的内容作为响应。用户将从前端输入channelName和channelProfile。基本上,路由是通过这种方式发生的,触发rest-api请求-此方法必须调用脚本文件-脚本文件中的命令应生成一个文件-该文件必须作为响应发送。我现在遇到的问题是脚本文件中的命令没有得到执行。它以状态代码127退出并返回“失败”响应。作为第一次试用的一部分,我只是返回字符串作为响应,而不是生成的输出文件。我是goa的新手,仍然在研究如何发送文件作为响应。如果有人知道,请尝试给我提示。 这些是我根据goa文档执行的命令: goa gen goapp2 / design, 果阿示例goapp2 / design, 去建立./cmd/fabric, ./fabric

我已经尝试了堆栈溢出的类似解决方案。但无法解决该错误。

 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","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

//这是调用generateChannel.sh文件的文件

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文件

#!/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

注意:我已经单独测试了此文件,它执行得很好。 脚本文件中的上述命令会查找configtx.yaml文件中定义的channelProfile和channelName,并相应地生成channel.tx文件。我已经定义了环境变量,并将路径添加到.bashrc和.profile文件。请帮助我我要去哪里了。

go env的屏幕截图 screenshot of go env

邮递员的输出

Output from Postman:

终端的输出:

:~/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
laohu1959 回答:使用goa-webFramework从Go执行bash脚本/命令未执行

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1237862.html

大家都在问