Skip to content

Commit

Permalink
add endpoints for swagger ui and json.
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyofx committed Jan 17, 2024
1 parent 141fa39 commit 3e8ed66
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
13 changes: 7 additions & 6 deletions examples/simpleweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
webHost.Run()
}

//CreateCustomBuilder Create the builder of Web host
// CreateCustomBuilder Create the builder of Web host
func CreateCustomBuilder() *abstractions.HostBuilder {
//config := nacosconfig.RemoteConfig("config")
//config := apollo.RemoteConfig("config")
Expand Down Expand Up @@ -94,7 +94,7 @@ func CreateCustomBuilder() *abstractions.HostBuilder {

//*/

//region router config function
// region router config function
func registerEndpointRouterConfig(rb router.IRouterBuilder) {
endpoints.UseHealth(rb)
endpoints.UseViz(rb)
Expand All @@ -104,6 +104,7 @@ func registerEndpointRouterConfig(rb router.IRouterBuilder) {
endpoints.UseLiveness(rb)
endpoints.UseJwt(rb)
endpoints.UseRouteInfo(rb)
endpoints.UseSwaggerUI(rb)

rb.GET("/error", func(ctx *context.HttpContext) {
panic("http get error")
Expand Down Expand Up @@ -145,8 +146,8 @@ type UserInfo struct {
Id string `param:"id"`
}

//HttpGet request: /info or /v1/api/info
//bind UserInfo for id,q1,username
// HttpGet request: /info or /v1/api/info
// bind UserInfo for id,q1,username
func GetInfo(ctx *context.HttpContext) {
ctx.JSON(200, context.H{"info": "ok"})
}
Expand All @@ -160,8 +161,8 @@ func GetInfoByIOC(ctx *context.HttpContext) {
})
}

//bootstrap binding
//HttpPost request: /info/:id ?q1=abc&username=123
// bootstrap binding
// HttpPost request: /info/:id ?q1=abc&username=123
func PostInfo(ctx *context.HttpContext) {
qs_q1 := ctx.Input.Query("q1")
pd_name := ctx.Input.Param("username")
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package yoyogo

const (
//Application Version, such as v1.x.x pre-release
Version = "v1.8.7.release"
Version = "v1.8.8.release"
//Application logo
//Logo = "IF8gICAgIF8gICAgICAgICAgICAgICAgICAgIF9fXyAgICAgICAgICAKKCApICAgKCApICAgICAgICAgICAgICAgICAgKCAgX2BcICAgICAgICAKYFxgXF8vJy8nXyAgICBfICAgXyAgICBfICAgfCAoIChfKSAgIF8gICAKICBgXCAvJy8nX2BcICggKSAoICkgLydfYFwgfCB8X19fICAvJ19gXCAKICAgfCB8KCAoXykgKXwgKF8pIHwoIChfKSApfCAoXywgKSggKF8pICkKICAgKF8pYFxfX18vJ2BcX18sIHxgXF9fXy8nKF9fX18vJ2BcX19fLycKICAgICAgICAgICAgICggKV98IHwgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgIGBcX19fLycgICAgICAgICAgICBMaWdodCBhbmQgZmFzdC4gIA=="
Logo = `
Expand Down
90 changes: 90 additions & 0 deletions web/endpoints/swagger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package endpoints

import (
"github.com/yoyofx/yoyogo/abstractions/xlog"
"github.com/yoyofx/yoyogo/web/actionresult"
"github.com/yoyofx/yoyogo/web/context"
"github.com/yoyofx/yoyogo/web/router"
)

func UseSwaggerUI(router router.IRouterBuilder) {
xlog.GetXLogger("Endpoint").Debug("loaded swagger ui endpoint.")

router.GET("/swagger.json", func(ctx *context.HttpContext) {
swaggerJson := `{
"swagger": "2.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"host": "localhost:8080",
"basePath": "/",
"schemes": ["http"],
"paths": {
"/users": {
"get": {
"summary": "List users",
"description": "Returns a list of users.",
"responses": {
"200": {
"description": "A list of users",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/User"
}
}
}
}
}
}
},
"definitions": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
}
}
}
}`
ctx.Render(200, actionresult.Data{ContentType: "application/json; charset=utf-8", Data: []byte(swaggerJson)})
})

router.GET("/swagger", func(ctx *context.HttpContext) {
swaggerUIHTML := `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.52.5/swagger-ui.css">
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.52.5/swagger-ui-bundle.js"></script>
</head>
<body>
<div id="swagger-ui"></div>
<script>
window.onload = function() {
const ui = SwaggerUIBundle({
url: "http://localhost:8080/app/swagger.json",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
],
})
}
</script>
</body>
</html>`
ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
_, _ = ctx.Output.Write([]byte(swaggerUIHTML))
ctx.Output.SetStatus(200)

})
}

0 comments on commit 3e8ed66

Please sign in to comment.