Skip to content

Commit

Permalink
Refactor templates to have access to rekwest template separately
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-keller committed Feb 7, 2022
1 parent 3f3fed1 commit 3bd0c6e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
19 changes: 13 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,31 @@ func (b Bin) Timestamp() time.Time {

type Rekwest struct {
RekwestId primitive.ObjectID
// Method string
// Host string
// Path string
// Created string // timestamp
// Parameters map[string]string
Method string
Host string
Path string
Params map[string][]string
// Headers map[string][]string
// Body string
Raw string
}

func (rekwest Rekwest) Timestamp() time.Time {
return rekwest.RekwestId.Timestamp()
}

func NewRekwest(r *http.Request) (Rekwest, error) {
dump, err := httputil.DumpRequest(r, true)

checkAndFail(err)
queryParams := r.URL.Query()

return Rekwest{
RekwestId: primitive.NewObjectIDFromTimestamp(time.Now()),
Method: r.Method,
Host: r.Host,
Path: r.URL.Path,
Raw: string(dump),
Params: queryParams,
}, nil
}

Expand Down
14 changes: 9 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ func run(args []string) error {

type server struct {
mux *http.ServeMux
tmpl *template.Template
tmpl map[string]*template.Template
db *Database
}

func newServer() (*server, error) {
srv := &server{
mux: http.NewServeMux(),
tmpl: template.Must(template.ParseGlob("templates/*.html")),
db: NewDatabase("rekwest-bin", "bins"),
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")),
},

db: NewDatabase("rekwest-bin", "bins"),
}

srv.routes()
Expand All @@ -65,7 +69,7 @@ func (s *server) handleIndex() http.HandlerFunc {
}

func (s *server) renderTemplate(writer http.ResponseWriter, tmpl string, bin *Bin) {
err := s.tmpl.ExecuteTemplate(writer, tmpl+".html", bin)
err := s.tmpl[tmpl].ExecuteTemplate(writer, tmpl+".html", bin)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
Expand Down
7 changes: 1 addition & 6 deletions templates/inspect.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
{{define "request"}}
<div class="request">
<pre>{{.Raw}}</pre>
</div>
{{end}}

<!DOCTYPE html>
<html lang="en">
<head>
<title>Rekwests: {{.BinId}}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src=""> </script>
<link href="css/style.css" rel="stylesheet">
</head>
<body>
Expand Down
25 changes: 25 additions & 0 deletions templates/rekwest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{define "request"}}
<div class="request">
<div class="request-line">
<div>
Method: {{.Method}}
Host: {{.Host}}
Path: {{.Path}}
</div>
<div>
<strong>Query Params:</strong>
<dl>
{{range $key, $val := .Params}}
<strong> {{ $key }} </strong>:
{{range $val}}
{{ . }},
{{end}}
{{else}}
None
{{end}}
</dl>
<div>Request Made At: {{.Timestamp}}</div>
</div>
<pre>{{.Raw}}</pre>
</div>
{{end}}

0 comments on commit 3bd0c6e

Please sign in to comment.