mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-27 10:35:41 +00:00
using github.com/vbatts/go-httplog for HTTP logging
This commit is contained in:
parent
1ed6696abf
commit
921fee9529
1 changed files with 38 additions and 37 deletions
75
server.go
75
server.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/vbatts/go-httplog"
|
||||||
"github.com/vbatts/imgsrv/hash"
|
"github.com/vbatts/imgsrv/hash"
|
||||||
"github.com/vbatts/imgsrv/types"
|
"github.com/vbatts/imgsrv/types"
|
||||||
"github.com/vbatts/imgsrv/util"
|
"github.com/vbatts/imgsrv/util"
|
||||||
|
@ -60,7 +61,7 @@ func initMongo() {
|
||||||
|
|
||||||
func serverErr(w http.ResponseWriter, r *http.Request, e error) {
|
func serverErr(w http.ResponseWriter, r *http.Request, e error) {
|
||||||
log.Printf("Error: %s", e)
|
log.Printf("Error: %s", e)
|
||||||
util.LogRequest(r, 503)
|
httplog.LogRequest(r, 503)
|
||||||
fmt.Fprintf(w, "Error: %s", e)
|
fmt.Fprintf(w, "Error: %s", e)
|
||||||
http.Error(w, "Service Unavailable", 503)
|
http.Error(w, "Service Unavailable", 503)
|
||||||
return
|
return
|
||||||
|
@ -92,7 +93,7 @@ func chunkURI(uri string) (chunks []string) {
|
||||||
func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
||||||
uriChunks := chunkURI(r.URL.Path)
|
uriChunks := chunkURI(r.URL.Path)
|
||||||
if len(uriChunks) > 2 {
|
if len(uriChunks) > 2 {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -114,7 +115,7 @@ func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
||||||
// no filename given, show them the full listing
|
// no filename given, show them the full listing
|
||||||
http.Redirect(w, r, "/all", 302)
|
http.Redirect(w, r, "/all", 302)
|
||||||
}
|
}
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -127,7 +128,7 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
uriChunks := chunkURI(r.URL.Path)
|
uriChunks := chunkURI(r.URL.Path)
|
||||||
if len(uriChunks) > 2 {
|
if len(uriChunks) > 2 {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -140,11 +141,11 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// if the Request got here by a delete request, confirm it
|
// if the Request got here by a delete request, confirm it
|
||||||
if (len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true") && (len(r.Form["confirm"]) > 0 && r.Form["confirm"][0] == "true") {
|
if (len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true") && (len(r.Form["confirm"]) > 0 && r.Form["confirm"][0] == "true") {
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
routeFilesDELETE(w, r)
|
routeFilesDELETE(w, r)
|
||||||
return
|
return
|
||||||
} else if len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true" {
|
} else if len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true" {
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
err = DeleteFilePage(w, uriChunks[1])
|
err = DeleteFilePage(w, uriChunks[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
|
@ -165,7 +166,7 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
log.Printf("Results for [%s] = %d", uriChunks[1], c)
|
log.Printf("Results for [%s] = %d", uriChunks[1], c)
|
||||||
if c == 0 {
|
if c == 0 {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -186,7 +187,7 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
// no filename given, show them the full listing
|
// no filename given, show them the full listing
|
||||||
http.Redirect(w, r, "/all", 302)
|
http.Redirect(w, r, "/all", 302)
|
||||||
}
|
}
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -200,7 +201,7 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
||||||
if len(uriChunks) > 2 &&
|
if len(uriChunks) > 2 &&
|
||||||
((len(uriChunks) == 2 && len(uriChunks[1]) == 0) &&
|
((len(uriChunks) == 2 && len(uriChunks[1]) == 0) &&
|
||||||
len(r.URL.RawQuery) == 0) {
|
len(r.URL.RawQuery) == 0) {
|
||||||
util.LogRequest(r, 403)
|
httplog.LogRequest(r, 403)
|
||||||
http.Error(w, "Not Acceptable", 403)
|
http.Error(w, "Not Acceptable", 403)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -305,20 +306,20 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
||||||
io.WriteString(w, fmt.Sprintf("/f/%s\n", filename))
|
io.WriteString(w, fmt.Sprintf("/f/%s\n", filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
func routeFilesPUT(w http.ResponseWriter, r *http.Request) {
|
func routeFilesPUT(w http.ResponseWriter, r *http.Request) {
|
||||||
// update the file by the name in the path and/or parameter?
|
// update the file by the name in the path and/or parameter?
|
||||||
// update/add keywords from the parameters
|
// update/add keywords from the parameters
|
||||||
// look for an image in the r.Body
|
// look for an image in the r.Body
|
||||||
util.LogRequest(r, 418)
|
httplog.LogRequest(r, 418)
|
||||||
}
|
}
|
||||||
|
|
||||||
func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
||||||
uriChunks := chunkURI(r.URL.Path)
|
uriChunks := chunkURI(r.URL.Path)
|
||||||
if (len(uriChunks) > 2) || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
if (len(uriChunks) > 2) || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
||||||
util.LogRequest(r, 400)
|
httplog.LogRequest(r, 400)
|
||||||
http.Error(w, "Bad Syntax", 400)
|
http.Error(w, "Bad Syntax", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -335,10 +336,10 @@ func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.LogRequest(r, 302)
|
httplog.LogRequest(r, 302)
|
||||||
http.Redirect(w, r, "/", 302)
|
http.Redirect(w, r, "/", 302)
|
||||||
} else {
|
} else {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
// delete the name in the path and/or parameter?
|
// delete the name in the path and/or parameter?
|
||||||
|
@ -349,7 +350,7 @@ func routeViews(w http.ResponseWriter, r *http.Request) {
|
||||||
case r.Method == "GET":
|
case r.Method == "GET":
|
||||||
routeViewsGET(w, r)
|
routeViewsGET(w, r)
|
||||||
default:
|
default:
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -366,7 +367,7 @@ func routeFiles(w http.ResponseWriter, r *http.Request) {
|
||||||
case r.Method == "DELETE":
|
case r.Method == "DELETE":
|
||||||
routeFilesDELETE(w, r)
|
routeFilesDELETE(w, r)
|
||||||
default:
|
default:
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -374,7 +375,7 @@ func routeFiles(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func routeRoot(w http.ResponseWriter, r *http.Request) {
|
func routeRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" {
|
if r.Method != "GET" {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -392,12 +393,12 @@ func routeRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: %s", err)
|
log.Printf("error: %s", err)
|
||||||
}
|
}
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
func routeAll(w http.ResponseWriter, r *http.Request) {
|
func routeAll(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" {
|
if r.Method != "GET" {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -415,7 +416,7 @@ func routeAll(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: %s", err)
|
log.Printf("error: %s", err)
|
||||||
}
|
}
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -432,7 +433,7 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" ||
|
if r.Method != "GET" ||
|
||||||
len(uriChunks) > 3 ||
|
len(uriChunks) > 3 ||
|
||||||
(len(uriChunks) == 3 && uriChunks[2] != "r") {
|
(len(uriChunks) == 3 && uriChunks[2] != "r") {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
} else if len(uriChunks) == 1 || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
} else if len(uriChunks) == 1 || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
||||||
|
@ -446,7 +447,7 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
||||||
if uriChunks[len(uriChunks)-1] == "r" {
|
if uriChunks[len(uriChunks)-1] == "r" {
|
||||||
// TODO determine how to show a random image by keyword ...
|
// TODO determine how to show a random image by keyword ...
|
||||||
log.Println("random isn't built yet")
|
log.Println("random isn't built yet")
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
return
|
return
|
||||||
} else if len(uriChunks) == 2 {
|
} else if len(uriChunks) == 2 {
|
||||||
log.Println(uriChunks[1])
|
log.Println(uriChunks[1])
|
||||||
|
@ -465,13 +466,13 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("error: %s", err)
|
log.Printf("error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
||||||
uriChunks := chunkURI(r.URL.Path)
|
uriChunks := chunkURI(r.URL.Path)
|
||||||
if r.Method != "GET" {
|
if r.Method != "GET" {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
} else if len(uriChunks) != 2 {
|
} else if len(uriChunks) != 2 {
|
||||||
|
@ -491,29 +492,29 @@ func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("error: %s", err)
|
log.Printf("error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show a page of file extensions, and allow paging by ext
|
// Show a page of file extensions, and allow paging by ext
|
||||||
func routeExt(w http.ResponseWriter, r *http.Request) {
|
func routeExt(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" {
|
if r.Method != "GET" {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show a page of all the uploader's IPs, and the images
|
// Show a page of all the uploader's IPs, and the images
|
||||||
func routeIPs(w http.ResponseWriter, r *http.Request) {
|
func routeIPs(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" {
|
if r.Method != "GET" {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -522,7 +523,7 @@ func routeIPs(w http.ResponseWriter, r *http.Request) {
|
||||||
Set an unruly cache on this path, so the browser does not constantly ask for it
|
Set an unruly cache on this path, so the browser does not constantly ask for it
|
||||||
*/
|
*/
|
||||||
func routeFavIcon(w http.ResponseWriter, r *http.Request) {
|
func routeFavIcon(w http.ResponseWriter, r *http.Request) {
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
w.Header().Set("Cache-Control", "max-age=315360000")
|
w.Header().Set("Cache-Control", "max-age=315360000")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -536,7 +537,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: %s", err)
|
log.Printf("error: %s", err)
|
||||||
}
|
}
|
||||||
util.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -572,7 +573,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
} else if len(local_filename) == 0 {
|
} else if len(local_filename) == 0 {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -623,11 +624,11 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
http.Redirect(w, r, fmt.Sprintf("/v/%s", stored_filename), 302)
|
http.Redirect(w, r, fmt.Sprintf("/v/%s", stored_filename), 302)
|
||||||
} else {
|
} else {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.LogRequest(r, 200) // if we make it this far, then log success
|
httplog.LogRequest(r, 200) // if we make it this far, then log success
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -637,7 +638,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
func routeUpload(w http.ResponseWriter, r *http.Request) {
|
func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
// Show the upload form
|
// Show the upload form
|
||||||
util.LogRequest(r, 200) // if we make it this far, then log success
|
httplog.LogRequest(r, 200) // if we make it this far, then log success
|
||||||
err := UploadPage(w)
|
err := UploadPage(w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: %s", err)
|
log.Printf("error: %s", err)
|
||||||
|
@ -710,11 +711,11 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
http.Redirect(w, r, fmt.Sprintf("/v/%s", filename), 302)
|
http.Redirect(w, r, fmt.Sprintf("/v/%s", filename), 302)
|
||||||
} else {
|
} else {
|
||||||
util.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.LogRequest(r, 200) // if we make it this far, then log success
|
httplog.LogRequest(r, 200) // if we make it this far, then log success
|
||||||
}
|
}
|
||||||
|
|
||||||
func routeAssets(w http.ResponseWriter, r *http.Request) {
|
func routeAssets(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
Loading…
Reference in a new issue