32 lines
571 B
Go
32 lines
571 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
type Server struct {
|
|
fs http.Handler
|
|
ws *WS
|
|
}
|
|
|
|
func New() *Server {
|
|
fs := http.FileServer(http.Dir(config().GetString("d")))
|
|
return &Server{
|
|
fs: fs,
|
|
ws: NewWS(),
|
|
}
|
|
}
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
log.Println("serving", r.URL)
|
|
if _, err := os.Stat(path.Join(config().GetString("d"), r.URL.Path[1:])); os.IsNotExist(err) {
|
|
s.ws.ServeHTTP(w, r)
|
|
} else {
|
|
log.Printf("Serving static %q from %q", r.URL.Path, config().GetString("d"))
|
|
s.fs.ServeHTTP(w, r)
|
|
}
|
|
}
|