moth/cmd/mothd/theme.go

46 lines
672 B
Go
Raw Normal View History

2019-09-02 18:42:27 -06:00
package main
import (
"net/http"
"os"
"strings"
)
type Theme struct {
2019-09-02 19:47:24 -06:00
Component
2019-09-02 18:42:27 -06:00
}
2019-09-02 19:47:24 -06:00
func NewTheme(baseDir string) *Theme {
2019-09-02 18:42:27 -06:00
return &Theme{
2019-09-02 19:47:24 -06:00
Component: Component{
baseDir: baseDir,
},
2019-09-02 18:42:27 -06:00
}
}
func (t *Theme) staticHandler(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path
if strings.Contains(path, "/.") {
2019-09-02 19:47:24 -06:00
http.Error(w, "Invalid path", http.StatusBadRequest)
2019-09-02 18:42:27 -06:00
return
}
if path == "/" {
path = "/index.html"
}
f, err := os.Open(t.path(path))
if err != nil {
http.NotFound(w, req)
return
}
defer f.Close()
d, err := f.Stat()
if err != nil {
http.NotFound(w, req)
return
}
http.ServeContent(w, req, path, d.ModTime(), f)
}