You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
645 B
23 lines
645 B
// Package web 嵌入前端 HTML 模板,用 //go:embed 把 templates/*.html 编译进二进制。
|
|
package web
|
|
|
|
import (
|
|
"embed"
|
|
"net/http"
|
|
)
|
|
|
|
//go:embed templates/*.html
|
|
var templatesFS embed.FS
|
|
|
|
// IndexHandler 返回 index.html。
|
|
func IndexHandler() http.Handler {
|
|
data, err := templatesFS.ReadFile("templates/index.html")
|
|
if err != nil {
|
|
// embed 失败意味着代码 bug,直接 panic 更早暴露。
|
|
panic("web: failed to read templates/index.html: " + err.Error())
|
|
}
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
_, _ = w.Write(data)
|
|
})
|
|
}
|
|
|