diff --git a/handle_bin_inspect.go b/handle_bin_inspect.go index 7eeab0d..1000c9d 100644 --- a/handle_bin_inspect.go +++ b/handle_bin_inspect.go @@ -19,6 +19,7 @@ func (s *server) handleBinInspect() http.HandlerFunc { s.renderTemplate(w, "inspect", &bin) return + default: http.NotFound(w, r) return diff --git a/handle_bin_new.go b/handle_bin_new.go index d4cfc9c..ad78e00 100644 --- a/handle_bin_new.go +++ b/handle_bin_new.go @@ -7,6 +7,7 @@ import ( func (s *server) handleBinNew() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + fmt.Println("handleBinNew") switch r.Method { case "POST": bin, _ := s.db.NewBin() diff --git a/handle_rekwest.go b/handle_rekwest.go index 43f3896..3a03faa 100644 --- a/handle_rekwest.go +++ b/handle_rekwest.go @@ -7,6 +7,7 @@ import ( func (s *server) handleRequest() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + fmt.Println("handleRequest") binID := r.URL.Path[len("/r/"):] fmt.Println("Request made: ", binID) diff --git a/main.go b/main.go index 6cd4e7b..b53a2d5 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,6 @@ import ( "net/http" "os" "path/filepath" - - socketio "github.com/googollee/go-socket.io" ) func main() { @@ -35,18 +33,14 @@ func run(args []string) error { srv.db.Connect() defer srv.db.Disconnect() - go srv.sockets.Serve() - defer srv.sockets.Close() - fmt.Printf("Rekwest Bin listening on :%d\n", *port) return http.ListenAndServe(addr, srv) } type server struct { - mux *http.ServeMux - tmpl map[string]*template.Template - db *Database - sockets *socketio.Server + mux *http.ServeMux + tmpl map[string]*template.Template + db *Database } func newServer() (*server, error) { @@ -54,13 +48,10 @@ func newServer() (*server, error) { mux: http.NewServeMux(), tmpl: map[string]*template.Template{ "inspect": template.Must(template.ParseFiles("templates/inspect.html", "templates/rekwest.html")), - "rekwest": template.Must(template.ParseFiles("templates/rekwest.html")), }, - sockets: socketio.NewServer(nil), - db: NewDatabase("rekwest-bin", "bins"), + db: NewDatabase("rekwest-bin", "bins"), } - srv.socketRoutes() srv.routes() return srv, nil } @@ -69,9 +60,17 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.mux.ServeHTTP(w, r) } -func (s *server) handleIndex() http.HandlerFunc { +func (s *server) handleRoot() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, filepath.Join("public", "index.html")) + filename := "" + + if r.URL.Path == "/" { + filename = "index.html" + } else { + filename = r.URL.Path + } + + http.ServeFile(w, r, filepath.Join("public", filename)) } } diff --git a/middleware.go b/middleware.go index 18896e2..4b795f8 100644 --- a/middleware.go +++ b/middleware.go @@ -1,6 +1,10 @@ package main -import "net/http" +import ( + "fmt" + "net/http" + "time" +) func (s *server) fixIPAddress(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -26,3 +30,18 @@ func (s *server) fixIPAddress(h http.HandlerFunc) http.HandlerFunc { h(w, r) } } + +func (s *server) withLogging(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + + uri := r.RequestURI + method := r.Method + h.ServeHTTP(w, r) // serve the original request + + duration := time.Since(start) + + // log request details + fmt.Println(uri, method, duration) + } +} diff --git a/public/css/style.css b/public/css/style.css new file mode 100644 index 0000000..fca0dc0 --- /dev/null +++ b/public/css/style.css @@ -0,0 +1,20 @@ +html { + padding: 3em 1em; + margin: auto; + line-height: 1.75; + font-size: 1.25em; +} + +h1,h2,h3,h4,h5,h6 { + margin: 3em 0 1em; +} + +p,ul,ol { + margin-bottom: 2em; + color: #1d1d1d; + font-family: sans-serif; +} + +dt, dd { + display: inline; +} diff --git a/routes.go b/routes.go index 3aa8ac3..677c98a 100644 --- a/routes.go +++ b/routes.go @@ -1,9 +1,8 @@ package main func (s *server) routes() { - s.mux.Handle("/socket.io/", s.sockets) - s.mux.HandleFunc("/", s.handleIndex()) - s.mux.HandleFunc("/new/", s.handleBinNew()) - s.mux.HandleFunc("/r/", s.fixIPAddress(s.handleRequest())) - s.mux.HandleFunc("/inspect/", s.handleBinInspect()) + s.mux.HandleFunc("/", s.withLogging(s.handleRoot())) + s.mux.HandleFunc("/new/", s.withLogging(s.handleBinNew())) + s.mux.HandleFunc("/r/", s.withLogging(s.fixIPAddress(s.handleRequest()))) + s.mux.HandleFunc("/inspect/", s.withLogging(s.handleBinInspect())) } diff --git a/sockets.go b/sockets.go deleted file mode 100644 index b4d7aad..0000000 --- a/sockets.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "fmt" - - socketio "github.com/googollee/go-socket.io" -) - -func (srv *server) socketRoutes() { - srv.sockets.OnConnect("/", func(sock socketio.Conn) error { - sock.SetContext("") - fmt.Println("connected:", sock.ID()) - return nil - }) - - srv.sockets.OnEvent("/", "hello", func(sock socketio.Conn, msg string) string { - sock.SetContext(msg) - fmt.Println("Message: ", msg) - return "recv " + msg - }) -} diff --git a/templates/inspect.html b/templates/inspect.html index 6b75f62..ba0567d 100644 --- a/templates/inspect.html +++ b/templates/inspect.html @@ -4,14 +4,8 @@ Rekwests: {{.BinId}} - - - + +

Requests to {{.BinId}}

diff --git a/templates/rekwest.html b/templates/rekwest.html index ed92b95..4d20690 100644 --- a/templates/rekwest.html +++ b/templates/rekwest.html @@ -1,10 +1,11 @@ {{define "request"}}
-
- Method: {{.Method}} - Host: {{.Host}} - Path: {{.Path}} +
+
Method:
{{.Method}}
+
Host:
{{.Host}}
+
Path:
{{.Path}}
+
Query Params: