我是新手go-lang,所以go-swagger。我正在关注博客go-swagger并 使用命令安装:
go get -u github.com/go-swagger/go-swagger/cmd/swagger
Run Code Online (Sandbox Code Playgroud)
我可以看到该go-swagger文件夹是在
C:\Go\bin\src\github.com\go-swagger
Run Code Online (Sandbox Code Playgroud)
现在,我将项目路径添加到$GOPATH:
echo %GOPATH%
C:\Go\bin;D:\Personal\Learning\GoLang\Project-2;D:\Personal\Learning\GoLang\swagger;
Run Code Online (Sandbox Code Playgroud)
当我跑步时
D:\Personal\Learning\GoLang\swagger>swagger ./swagger.yaml
'swagger' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)
我缺少什么?另外,如果您能为我推荐一些好的材料,我将不胜感激,因为go-swagger我发现设置所有内容都非常困难。没有太多博客可以帮助我进行HELLO WORLD一些设置
谢谢
更新1:
我尝试设置GOBIN但没有成功:
D:\Personal\Learning\GoLang\swagger>swagger validate https://raw.githubusercontent.com/swagger-api/swagger-spec/master/examples/v2.0/json/petstore-expanded.json
'swagger' is not recognized as an internal or external command,
operable program or batch file.
D:\Personal\Learning\GoLang\swagger>echo %GOBIN%
C:\Go\bin\;
Run Code Online (Sandbox Code Playgroud)
更新2:
我也按照建议尝试了绝对路径,但没有运气:
D:\Personal\Learning\GoLang\swagger>C:\Go\bin\swagger validate https://raw.githubusercontent.com/swagger-api/swagger-spec/master/examples/v2.0/json/petstore-expanded.json
'C:\Go\bin\swagger' is …Run Code Online (Sandbox Code Playgroud) 我去go-swagger生成的代码,找到以下代码:
// NewReceiveLearningLabActsParams creates a new ReceiveLearningLabActsParams object
// with the default values initialized.
func NewReceiveLearningLabActsParams() ReceiveLearningLabActsParams {
var ()
return ReceiveLearningLabActsParams{}
}
Run Code Online (Sandbox Code Playgroud)
我注意到了这里:
var ()
Run Code Online (Sandbox Code Playgroud)
我完全不明白是什么意思,任何人都可以帮我理解这段代码吗?谢谢
我在使用BearerAuthJWT令牌时使用了sw-swagger .连同实际令牌,我收到的声明包括用户名等数据.
如何在api.ItemsCreateItemHandler下面的功能中访问声明?
package restapi
func configureAPI(api *operations.MyAPI) http.Handler {
api.BearerAuth = func(token string) (interface{}, error) {
jwtToken := strings.Replace(token, "Bearer ", "", -1)
// skipped token verification
claims, _ := parsedToken.Claims.(jwt.MapClaims)
}
api.ItemsCreateItemHandler = items.CreateItemHandlerFunc(func(params items.CreateItemParams, principal interface{}) middleware.Responder {
// FIXME: Here I need to be able to access JWT claims
if err := createItem(params.Body, claims); err != nil {
return nil // handle error
}
return items.NewCreateItemCreated()
})
}
Run Code Online (Sandbox Code Playgroud) 我正在使用go-swagger来生成API服务器.我注意到swagger.yml生成的json正在保留restapi/embedded_spec.go.
公开JSON规范的最佳方法是什么,以便我的ReactJS客户端可以访问它?
到目前为止,我不得不使用swagger serve swagger.yml --port=50000并指向javacript客户端 localhost:50000/swagger.json.我正在寻找一种restapi/embedded_spec.go通过我的API 直接提供JSON的方法.
我正在使用go-swagger。Sentry 添加了性能监控我正在寻找最佳实践来在我的项目中添加此功能。
我知道,go-swagger 使用“net/http”,但对于正确的添加方式仍然存在一些争议。我将 init Sentry 添加到 configure_myapp_api.go 中
func InitSentry(cfg *configs.Config) {
err := sentry.Init(sentry.ClientOptions{
Dsn: cfg.SentryDsn,
Environment: fmt.Sprintf("[%s][%s]", "myapp-api-back", cfg.SentryEnvironment),
AttachStacktrace: true,
TracesSampleRate: 0.2,
SampleRate: 0.2,
EnableTracing: true,
})
if err != nil {
log.Fatalf("Sentry initialization failed: %v\n", err)
}
defer sentry.Recover()
}
Run Code Online (Sandbox Code Playgroud)
它应该只是一个中间件吗?
go-swagger ×6
go ×3
swagger ×3
openapi ×2
jwt ×1
makefile ×1
performance ×1
sentry ×1
ubuntu-22.04 ×1