我考虑过用Go为我的家里创建一个迷你文件服务器。通常情况下,http.FileServer服务器、文件和脏文件都很难看,如下所示:

是否可以将CSS添加到此站点?例如,改变颜色。提前感谢您的帮助!
发布于 2018-08-16 17:25:46
有一个麻烦的解决方案,它利用了这样一个事实:在http.ResponseWriter完成工作之后,您可以继续编写它。一般不建议,但在这种情况下可能是可以接受的。
package main
import (
"io"
"log"
"net/http"
)
const (
link = `<link rel="stylesheet" href="/path/to/style.css">`
)
func main() {
fs := http.FileServer(http.Dir("/tmp"))
var handler http.HandlerFunc
handler = func(w http.ResponseWriter, r *http.Request) {
var (
url = r.URL.Path
isDir = url[len(url)-1] == '/'
)
fs.ServeHTTP(w, r)
if isDir {
io.WriteString(w, link)
}
}
log.Fatal(http.ListenAndServe(":8080", handler))
}https://stackoverflow.com/questions/51881361
复制相似问题