Allaman / gin-demo

Showcasing single binary web app with Go and Gin-Gonic

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

index.html in assets?

aphsa opened this issue · comments

commented

If index.html is itself static can you put that in assets and serve?

Yes

package main

import (
	"embed"
	"io/fs"
	"net/http"

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

//go:embed static/*
var staticFiles embed.FS

func main() {
	r := gin.Default()
	static, err := fs.Sub(staticFiles, "static")
	if err != nil {
		panic(err)
	}

	r.StaticFS("/", http.FS(static))

	// Start the server
	r.Run(":8080")
}
cat static/index.html
<html>
  <head>
    <link rel="icon" href="/favicon.png" type="image/png" />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

Test: curl localhost:8080