|
|
|
@ -4,6 +4,7 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
|
|
"github.com/jordanorelli/dws/events"
|
|
|
|
|
)
|
|
|
|
@ -17,7 +18,11 @@ type server struct {
|
|
|
|
|
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 {
|
|
|
|
|
h := &eventEmittingHandler{
|
|
|
|
|
wrapped: s,
|
|
|
|
|
out: s.out,
|
|
|
|
|
}
|
|
|
|
|
if err := http.ListenAndServe(addr, h); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -28,18 +33,63 @@ func (s *server) setRoot(path string) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
s.out <- events.BeginRequestEvent{
|
|
|
|
|
Path: r.URL.Path,
|
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// trackginWriter is an http.ResponseWriter that tracks the number of bytes
|
|
|
|
|
// sent and the status sent
|
|
|
|
|
type trackingWriter struct {
|
|
|
|
|
http.ResponseWriter
|
|
|
|
|
status int // last http status written
|
|
|
|
|
wrote int // total number of bytes written
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *trackingWriter) WriteHeader(status int) {
|
|
|
|
|
t.ResponseWriter.WriteHeader(status)
|
|
|
|
|
t.status = status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *trackingWriter) Write(b []byte) (int, error) {
|
|
|
|
|
n, err := t.ResponseWriter.Write(b)
|
|
|
|
|
if t.status == 0 {
|
|
|
|
|
t.status = 200
|
|
|
|
|
}
|
|
|
|
|
t.wrote += n
|
|
|
|
|
return n, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eventEmittingHandler is an http.Handler that emits events on an event
|
|
|
|
|
// channel to report on requests and responses.
|
|
|
|
|
type eventEmittingHandler struct {
|
|
|
|
|
wrapped http.Handler
|
|
|
|
|
out chan events.BackgroundEvent
|
|
|
|
|
count uint32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *eventEmittingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
id := int(h.nextCount())
|
|
|
|
|
h.out <- events.BeginRequestEvent{
|
|
|
|
|
Seq: id,
|
|
|
|
|
Path: r.URL.Path,
|
|
|
|
|
}
|
|
|
|
|
tw := &trackingWriter{ResponseWriter: w}
|
|
|
|
|
h.wrapped.ServeHTTP(tw, r)
|
|
|
|
|
h.out <- events.EndRequestEvent{
|
|
|
|
|
Seq: id,
|
|
|
|
|
Status: tw.status,
|
|
|
|
|
Bytes: tw.wrote,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *eventEmittingHandler) nextCount() uint32 {
|
|
|
|
|
return atomic.AddUint32(&h.count, 1)
|
|
|
|
|
}
|
|
|
|
|