You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dws/bg/server.go

44 lines
844 B
Go

package bg
import (
"fmt"
"log"
"net/http"
"github.com/jordanorelli/dws/events"
)
type server struct {
port int
root string
out chan events.BackgroundEvent
}
func (s *server) listen() {
addr := fmt.Sprintf("0.0.0.0:%d", s.port)
log.Printf("server listening on addr: %s\n", addr)
if err := http.ListenAndServe(addr, s); err != nil {
panic(err)
}
}
func (s *server) setRoot(path string) {
log.Printf("server setting root to %s\n", path)
s.root = path
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.out <- events.BeginRequestEvent{}
if s.root == "" {
writeNotInitializedResponse(w)
return
}
fmt.Fprintf(w, "root: %s", s.root)
s.out <- events.EndRequestEvent{}
}
func writeNotInitializedResponse(w http.ResponseWriter) {
w.WriteHeader(500)
fmt.Fprintln(w, "no root directory selected")
}