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

How to Serve a Static File in Gin Router?

Susan Sarandon
Release: 2024-11-03 14:22:30
Original
660 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template