I want to serve a json file with gin server. And set some customize values in the html file. Use JavaScript in it to call the json file.
My application structure:
.
├── main.go
└── templates
├── index.html
└── web.json
I put these basic source into main.go
file:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
var router *gin.Engine
func main() {
router = gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/web", func(c *gin.Context) {
c.HTML(
http.StatusOK,
"index.html",
gin.H{
"title": "Web",
"url": "./web.json",
},
)
})
router.Run()
}
Some code in templates/index.html
file:
<!doctype html>
<html>
<head>
<title>{{ .title }}</title>
// ...
</head>
<body>
<div id="swagger-ui"></div>
// ...
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "{{ .url }}",
dom_id: '#swagger-ui',
// ...
})
// End Swagger UI call region
window.ui = ui
}
</script>
</body>
</html>
When run the application, got fetch error: Not Found ./web.json
. So how to serve the web.json
file to be accessed in the gin internal server?