Skip to content

Commit

Permalink
Add basic logging, css, other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-keller committed May 4, 2022
1 parent 632f71e commit 34bbaf8
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 54 deletions.
1 change: 1 addition & 0 deletions handle_bin_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (s *server) handleBinInspect() http.HandlerFunc {

s.renderTemplate(w, "inspect", &bin)
return

default:
http.NotFound(w, r)
return
Expand Down
1 change: 1 addition & 0 deletions handle_bin_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions handle_rekwest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 14 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"net/http"
"os"
"path/filepath"

socketio "github.com/googollee/go-socket.io"
)

func main() {
Expand All @@ -35,32 +33,25 @@ 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) {
srv := &server{
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
}
Expand All @@ -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))
}
}

Expand Down
21 changes: 20 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}
}
20 changes: 20 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 4 additions & 5 deletions routes.go
Original file line number Diff line number Diff line change
@@ -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()))
}
21 changes: 0 additions & 21 deletions sockets.go

This file was deleted.

10 changes: 2 additions & 8 deletions templates/inspect.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
<title>Rekwests: {{.BinId}}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.socket.io/4.4.1/socket.io.min.js" integrity="sha384-fKnu0iswBIqkjxrhQCTZ7qlLHOFEgNkRmK2vaO/LbTZSXdJfAu6ewRBdwHPhBo/H" crossorigin="anonymous"></script>
<script>
const socket = io();
socket.on("connect", () => {
socket.send("hello");
});
</script>
<link href="css/style.css" rel="stylesheet">
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
<link href="/css/style.css" rel="stylesheet">
</head>
<body>
<h1>Requests to {{.BinId}}</h1>
Expand Down
9 changes: 5 additions & 4 deletions templates/rekwest.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{{define "request"}}
<div class="request">
<div class="request-line">
<div>
Method: {{.Method}}
Host: {{.Host}}
Path: {{.Path}}
<dl>
<dt>Method:</dt> <dd>{{.Method}}</dd>
<dt>Host:</dt> <dd>{{.Host}}</dd>
<dt>Path:</dt> <dd>{{.Path}}</dd>
</dl>
</div>
<div>
<strong>Query Params:</strong>
Expand Down

0 comments on commit 34bbaf8

Please sign in to comment.