Home >Backend Development >Golang >How to Serve a Static File in Gin Router?

How to Serve a Static File in Gin Router?

Susan Sarandon
Susan SarandonOriginal
2024-11-03 14:22:30654browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn