what is the appropriate way to link the images and CSS files to html files which used as APIs using golang
I tried the following code but the CSS and images did't render , I used the julienschmidt library for routing the APIs
var tpl *template.Template
func init() {
tpl = template.Must(template.ParseGlob("*.html"))
}
func main(){
router := httprouter.New()
router.GET("/", indexPage)
}
func indexPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params){
tpl.ExecuteTemplate(w, "index.html", nil)
}
in index.html I use the link to use external CSS file
<link rel="stylesheet" type="text/css" href="public/css/main.css" />
the file structures are
/go
/src
/github.com
/My_account
/WebServerProject
-main.go
-index.html
/public
/css
-main.css
/images
First you will need to serve static files on your web server, you can also segment your code into templates in a directory and then serve that directory.
r := gin.Default()
files, err := filepath.Glob("./app/templates/*.tmpl")
if err == nil && files != nil {
r.LoadHTMLGlob("./app/templates/*.tmpl")
}
v1 := r.Group("/v1")
v1.Static("/static", "./_assets/static")