Home >Backend Development >Golang >How to Serve a Static File in Gin Router?
Serving Static Files in Gin Router
Serving static files in Gin router involves utilizing the Static, StaticFS, StaticFile, and StaticFileFS methods. These methods allow you to define routes that serve specific static files or directories.
In your case, you want to serve the web.json file. To do this, you need to define a route that points to the file's location. Add the following route to your main.go file:
<code class="go">router.StaticFile("/web.json", "./templates/web.json")</code>
This route will make the web.json file available at the /web.json path. You can now reference this file in your index.html file using the provided URL:
<code class="html"><script> const ui = SwaggerUIBundle({ url: "{{ .url }}", dom_id: '#swagger-ui', // ... }) window.ui = ui </script></code>
The above is the detailed content of How to Serve a Static File in Gin Router?. For more information, please follow other related articles on the PHP Chinese website!