store things pl0x

master
Jordan Orelli 10 years ago
parent 809776b7ce
commit f14741d7fa

@ -56,7 +56,14 @@ func setupDB() {
create table if not exists requests (
id text primary key,
host text not null,
path text not null,
path text not null
);
create table if not exists responses (
id text primary key,
status integer,
length integer, -- content-length header
content_type text, -- content-type header
duration integer -- time taken in milliseconds
);
`
@ -79,11 +86,20 @@ func saveHostname(hostname string) {
}
}
func saveRequest(id RequestId, r *http.Request, elapsed time.Duration) {
_, err := db.Exec(`insert or ignore into requests (id, host, path, duration)
values (?, ?, ?, ?)`, id.String(), r.URL.Host, r.URL.Path, elapsed.Nanoseconds()/1000000)
func saveRequest(id RequestId, r *http.Request) {
_, err := db.Exec(`insert into requests (id, host, path)
values (?, ?, ?)`, id.String(), r.URL.Host, r.URL.Path)
if err != nil {
log.Printf("unable to save request: %v", err)
return
}
}
func saveResponse(id RequestId, res *http.Response, elapsed time.Duration) {
_, err := db.Exec(`insert into responses (id, status, length, content_type, duration)
values (?, ?, ?, ?, ?)`, id.String(), res.StatusCode, res.ContentLength, res.Header.Get("Content-Type"), elapsed.Nanoseconds()/1000000)
if err != nil {
log.Printf("unable to save response: %v", err)
return
}
}

@ -68,7 +68,8 @@ func httpHandler(w http.ResponseWriter, r *http.Request) {
r.RequestURI = requestURI
fmt.Printf("elapsed: %v (%v)\n", elapsed, elapsed.Nanoseconds()/1000000)
saveRequest(id, r, elapsed)
saveRequest(id, r)
saveResponse(id, res, elapsed)
}
func bail(status int, t string, args ...interface{}) {

Loading…
Cancel
Save