gpt4 book ai didi

go - 无法使用swagger.yaml的应用程序启动swagger ui

转载 作者:行者123 更新时间:2023-12-01 22:21:45 31 4
gpt4 key购买 nike

一直在谷歌搜索和堆栈溢出,但是找不到有关使用哪些工具或yaml标记来配置swagger UI的示例。
还是要在我的摇头休息应用程序上指定哪个URL来显示UI(也许它已经存在并且我没有意识到?)
这是起作用的:
/ docs显示所有端点

/swagger.json显示路径的所有JSON
其余端点正常工作
我的Yaml:

swagger: "2.0"

consumes:
- application/io.goswagger.examples.todo-list.v1+json
info:
description: descr
title: My Title
version: 1.0.0

definitions:
message:
type: object
required:
- message
properties:
message:
type: string
minLength: 1

Order:
type: object
properties:
name:
type: string
items:
type: array
items:
type: object
properties:
name:
type: string
quantity:
type: string
description:
type: string
price:
type: string


error:
type: object
required:
- message
properties:
code:
type: integer
format: int64
message:
type: string

schemes:
- http

paths:
/version:
get:
description: version
operationId: version
tags:
- message
responses:
200:
description: standard message response
schema:
$ref: "#/definitions/message"

/health:
get:
description: health of service
operationId: health
responses:
200:
description: healthy service
503:
description: service in bad state

/v1/orders:
get:
description: list
operationId: list
responses:
200:
description: list successful
schema:
type: array
items:
$ref: "#/definitions/Order"
500:
description: list error
schema:
$ref: "#/definitions/message"
default:
description: error
schema:
$ref: "#/definitions/error"



produces:
- application/io.goswagger.examples.todo-list.v1+json

最佳答案

colm.anseo提出的解决方案行得通,但此外,我还必须如下修改configure_myapp_reservices.go方法:
添加:



func uiMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Shortcut helpers for swagger-ui
if r.URL.Path == "/swagger-ui" || r.URL.Path == "/api/help" {
http.Redirect(w, r, "/swagger-ui/", http.StatusFound)
return
}
// Serving ./swagger-ui/
if strings.Index(r.URL.Path, "/swagger-ui/") == 0 {
http.StripPrefix("/swagger-ui/", http.FileServer(http.Dir("swagger-ui"))).ServeHTTP(w, r)
return
}
handler.ServeHTTP(w, r)
})
}

// and modified setupGlobalMiddleware as follows:

func setupGlobalMiddleware(handler http.Handler) http.Handler {
return uiMiddleware(handler)
}

注意:添加了Docker命令以将dist复制到我的pod中的/ swagger-ui目录
例如
复制/ build / dist / swagger-ui
在本地/ build / dist目录中将index.html修改为指向:
网址:“./ swagger.json”,
还必须获取swagger.json文件并将其粘贴到/ build / dist目录中
(我可以从http://service-route/swagger.json下载swagger.json)

关于go - 无法使用swagger.yaml的应用程序启动swagger ui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63062888/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com