Serve files with servecontent so that ETags etc should work.
parent
8b775cb39f
commit
fa7e5b5baf
30
routes.go
30
routes.go
|
@ -3,6 +3,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -21,9 +24,8 @@ func init() {
|
||||||
app.router.GET("/", app.Index)
|
app.router.GET("/", app.Index)
|
||||||
app.router.GET("/play/:playlist", app.Play)
|
app.router.GET("/play/:playlist", app.Play)
|
||||||
app.router.GET("/reset", app.Reset)
|
app.router.GET("/reset", app.Reset)
|
||||||
|
app.router.GET("/public/*js", app.ServeFiles)
|
||||||
app.router.ServeFiles("/js/*filepath", http.Dir("public"))
|
app.router.GET("/css/*css", app.ServeFiles)
|
||||||
app.router.ServeFiles("/css/*filepath", http.Dir("css"))
|
|
||||||
|
|
||||||
app.router.HandlerFunc("GET", "/ws", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
app.router.HandlerFunc("GET", "/ws", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("WS connection from %v\n", r.RemoteAddr)
|
log.Printf("WS connection from %v\n", r.RemoteAddr)
|
||||||
|
@ -49,6 +51,28 @@ type IndexData struct {
|
||||||
Ambiance []string
|
Ambiance []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app App) ServeFiles(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
|
filePath := filepath.Join(".", r.URL.Path)
|
||||||
|
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, "no such file", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
fileStat, err := os.Stat(filePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, "unable to get file stat", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, filename := path.Split(filePath)
|
||||||
|
t := fileStat.ModTime()
|
||||||
|
http.ServeContent(w, r, filename, t, file)
|
||||||
|
}
|
||||||
|
|
||||||
func (app App) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
func (app App) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
playlists, err := app.GetPlaylists()
|
playlists, err := app.GetPlaylists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -74,6 +74,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script src="/js/script.js"></script>
|
<script src="/public/script.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue