Trouble serving static files other than .html files

67 Views Asked by At
package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    fmt.Println("Server running on port 3000!")

    // Serve static files
    r.Static("/static/videos", "./static/videos")
    r.Static("/static/css", "./static/css")
    r.Static("/static/img", "./static/images")
    r.Static("/static/fonts", "./static/fonts")
    r.Static("/static/js", "./static/js")

    // Load HTML templates
    r.LoadHTMLGlob("./static/*.html")

    // Handling the routes
    r.GET("/", RootRoute)
    r.GET("/reservation", ReservationRoute)

    log.Fatal(r.Run(":3000"))
}

func RootRoute(c *gin.Context) {
    c.HTML(http.StatusOK, "index.html", gin.H{})
}

func ReservationRoute(c *gin.Context) {
    c.HTML(http.StatusOK, "reservation.html", gin.H{})
}

This is the code for a simple web server I tried to create using Gin. The files structure is: file structure

The problem I am facing is in the web page, only the html file is getting loaded and none of the static files.

Here I am explicitly mentioned every static files in a trial and error basis as nothing was working. I am new to Go so any help would be appreciated.

0

There are 0 best solutions below