2019-09-02 18:42:27 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-05 22:25:03 -07:00
|
|
|
"github.com/spf13/afero"
|
2019-09-02 18:42:27 -06:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Theme struct {
|
2019-12-05 22:25:03 -07:00
|
|
|
fs afero.Fs
|
2019-09-02 18:42:27 -06:00
|
|
|
}
|
|
|
|
|
2019-12-05 22:25:03 -07:00
|
|
|
func NewTheme(fs afero.Fs) *Theme {
|
2019-09-02 18:42:27 -06:00
|
|
|
return &Theme{
|
2019-12-05 22:25:03 -07:00
|
|
|
fs: fs,
|
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"
|
|
|
|
}
|
|
|
|
|
2019-12-05 22:25:03 -07:00
|
|
|
f, err := t.fs.Open(path)
|
2019-09-02 18:42:27 -06:00
|
|
|
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)
|
|
|
|
}
|