使用Goa-Web框架执行Bash脚本时遇到命令未执行的问题 - Golang技术讨论
使用Goa-Web框架执行Bash脚本时遇到命令未执行的问题 - Golang技术讨论 技术栈:HyperledgerFabric 1.4,项目仓库 - FabricSamples 下的 FirstNetwork,Go – v1.15,Goa- v3.2.5
需求:使用Goa框架,我需要触发一个POST请求(http://localhost:8000/createChannelProfile),并且它必须返回文件内容作为响应。用户将从前端输入channelName和channelProfile。基本上,路由是这样发生的:触发rest-api请求 -> 该方法必须调用脚本文件 -> 脚本文件中的命令应生成一个文件 -> 该文件必须作为响应发送。我目前卡住的地方是脚本文件中的命令没有执行。它以状态码127退出并返回“failed”响应。作为第一次尝试的一部分,我只是返回字符串作为响应,而不是生成的输出文件。我对Go和Goa有点陌生,仍在弄清楚如何将文件作为响应发送。如果有人知道,请尝试给我一个提示。这些是我根据GOA文档执行的命令:goa gen GoApp2/design,goa example GoApp2/design,go build ./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", 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
注意:我已经单独测试过这个文件,它执行得很完美。脚本文件中的上述命令会查找configtx.yaml文件中定义的channelProfile和channelName,并相应地生成channel.tx文件。我已经定义了环境变量,并将路径添加到了.bashrc和.profile文件中。请帮我看看我哪里出错了。
go env 截图
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
更多关于使用Goa-Web框架执行Bash脚本时遇到命令未执行的问题 - Golang技术讨论的实战教程也可以访问 https://www.itying.com/category-94-b0.html
错误已解决。现在工作正常。
更多关于使用Goa-Web框架执行Bash脚本时遇到命令未执行的问题 - Golang技术讨论的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
是的,脚本文件本身执行得非常好。是的,bash脚本文件的位置是正确的。是的,bash脚本是可执行的。
是的,它现在找不到它的配置了。我会尝试将配置作为命令输入,然后检查一下。
看起来,127 不一定是 bash 创建的退出代码,而可能是从你的脚本传播的退出代码。根据显示的输出来看,脚本似乎找不到其配置文件。
附言,刚刚意识到你复制的堆栈跟踪并没有明确提示退出代码……
能否请你尝试提供一些更简单的输出,同时使用 Markdown 代码块来格式化这些输出?
Archana1:
脚本文件中的命令未被执行。它以状态码127退出并返回“失败”响应。
你是否尝试过在不涉及所有API相关代码的情况下直接执行?你确定Bash脚本位于你认为的位置吗(在你Go代码执行位置的上层目录)?Bash脚本是否具有可执行权限?
它能够选取 generateChannel.sh 文件。脚本文件中命令 …/bin/configtxgen -profile “${CHANNELPROFILE}” -outputCreateChannelTx “./channel-artifacts/${CHANNELNAME}.tx” -channelID “${CHANNELNAME}” 的执行结果是以状态码 127 退出的。单独执行该脚本文件时,它工作正常。我曾使用 nodejs-Express 执行相同的功能,当时工作正常。
你按照我说的检查过你Go程序的标准输出吗?Bash在那里打印了什么内容吗?具体是什么?请分享你的项目结构,并告诉我们你从哪里运行了哪些命令。
如果有疑问,请在一个最小的git仓库中重现这个问题,并告诉我们URL,在README中提供说明,说明要从哪里运行什么命令来重现这个错误。
如果bash以127退出,这确实意味着它无法找到某些东西,它会在标准错误输出中告诉你它无法找到什么。
请检查 Stderr,如果你不想对你的程序做太多改动,只需使用 CombinedOutput 而不是 Output。
它会告诉你它无法找到你的 ../generateChannel.sh,因为这就是退出码 127 的含义。
请记住,相对路径是根据你运行程序的位置来解析的,而不是根据你存放源文件的位置。
因此,如果你从项目根目录运行 go run,那么脚本必须位于项目根目录的上一级。
func main() {
fmt.Println("hello world")
}


