Reputation: 769
I've thought about creating a mini file server for my home with Go. Normally http.FileServer
servers files and dirs ugly like this:
Is it possible to add CSS to this site? For example change colors. Thanks in advance for the help!
Upvotes: 2
Views: 986
Reputation: 2329
There is a hacky solution, making use of the fact that you can keep writing to a http.ResponseWriter
after http.FileServer
has done its work. Not recommended in general, but in this situation it might be acceptable.
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))
}
Upvotes: 2