mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-23 08:35:43 +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 (
|
||||
"fmt"
|
||||
"github.com/vbatts/go-httplog"
|
||||
"github.com/vbatts/imgsrv/hash"
|
||||
"github.com/vbatts/imgsrv/types"
|
||||
"github.com/vbatts/imgsrv/util"
|
||||
|
@ -60,7 +61,7 @@ func initMongo() {
|
|||
|
||||
func serverErr(w http.ResponseWriter, r *http.Request, e error) {
|
||||
log.Printf("Error: %s", e)
|
||||
util.LogRequest(r, 503)
|
||||
httplog.LogRequest(r, 503)
|
||||
fmt.Fprintf(w, "Error: %s", e)
|
||||
http.Error(w, "Service Unavailable", 503)
|
||||
return
|
||||
|
@ -92,7 +93,7 @@ func chunkURI(uri string) (chunks []string) {
|
|||
func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
||||
uriChunks := chunkURI(r.URL.Path)
|
||||
if len(uriChunks) > 2 {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
@ -114,7 +115,7 @@ func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
|||
// no filename given, show them the full listing
|
||||
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)
|
||||
if len(uriChunks) > 2 {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
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 (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)
|
||||
return
|
||||
} 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])
|
||||
if err != nil {
|
||||
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)
|
||||
if c == 0 {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
@ -186,7 +187,7 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
|||
// no filename given, show them the full listing
|
||||
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 &&
|
||||
((len(uriChunks) == 2 && len(uriChunks[1]) == 0) &&
|
||||
len(r.URL.RawQuery) == 0) {
|
||||
util.LogRequest(r, 403)
|
||||
httplog.LogRequest(r, 403)
|
||||
http.Error(w, "Not Acceptable", 403)
|
||||
return
|
||||
}
|
||||
|
@ -305,20 +306,20 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
|||
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) {
|
||||
// update the file by the name in the path and/or parameter?
|
||||
// update/add keywords from the parameters
|
||||
// look for an image in the r.Body
|
||||
util.LogRequest(r, 418)
|
||||
httplog.LogRequest(r, 418)
|
||||
}
|
||||
|
||||
func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
||||
uriChunks := chunkURI(r.URL.Path)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -335,10 +336,10 @@ func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
|||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
util.LogRequest(r, 302)
|
||||
httplog.LogRequest(r, 302)
|
||||
http.Redirect(w, r, "/", 302)
|
||||
} else {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
// 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":
|
||||
routeViewsGET(w, r)
|
||||
default:
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
@ -366,7 +367,7 @@ func routeFiles(w http.ResponseWriter, r *http.Request) {
|
|||
case r.Method == "DELETE":
|
||||
routeFilesDELETE(w, r)
|
||||
default:
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
@ -374,7 +375,7 @@ func routeFiles(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func routeRoot(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
@ -392,12 +393,12 @@ func routeRoot(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
util.LogRequest(r, 200)
|
||||
httplog.LogRequest(r, 200)
|
||||
}
|
||||
|
||||
func routeAll(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
@ -415,7 +416,7 @@ func routeAll(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
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" ||
|
||||
len(uriChunks) > 3 ||
|
||||
(len(uriChunks) == 3 && uriChunks[2] != "r") {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
} 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" {
|
||||
// TODO determine how to show a random image by keyword ...
|
||||
log.Println("random isn't built yet")
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
return
|
||||
} else if len(uriChunks) == 2 {
|
||||
log.Println(uriChunks[1])
|
||||
|
@ -465,13 +466,13 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
|||
log.Printf("error: %s", err)
|
||||
}
|
||||
|
||||
util.LogRequest(r, 200)
|
||||
httplog.LogRequest(r, 200)
|
||||
}
|
||||
|
||||
func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
||||
uriChunks := chunkURI(r.URL.Path)
|
||||
if r.Method != "GET" {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
} else if len(uriChunks) != 2 {
|
||||
|
@ -491,29 +492,29 @@ func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
|||
log.Printf("error: %s", err)
|
||||
}
|
||||
|
||||
util.LogRequest(r, 200)
|
||||
httplog.LogRequest(r, 200)
|
||||
}
|
||||
|
||||
// Show a page of file extensions, and allow paging by ext
|
||||
func routeExt(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
util.LogRequest(r, 200)
|
||||
httplog.LogRequest(r, 200)
|
||||
}
|
||||
|
||||
// Show a page of all the uploader's IPs, and the images
|
||||
func routeIPs(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
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
|
||||
*/
|
||||
func routeFavIcon(w http.ResponseWriter, r *http.Request) {
|
||||
util.LogRequest(r, 200)
|
||||
httplog.LogRequest(r, 200)
|
||||
w.Header().Set("Cache-Control", "max-age=315360000")
|
||||
}
|
||||
|
||||
|
@ -536,7 +537,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
util.LogRequest(r, 200)
|
||||
httplog.LogRequest(r, 200)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -572,7 +573,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
|||
serverErr(w, r, err)
|
||||
return
|
||||
} else if len(local_filename) == 0 {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
@ -623,11 +624,11 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
http.Redirect(w, r, fmt.Sprintf("/v/%s", stored_filename), 302)
|
||||
} else {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
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) {
|
||||
if r.Method == "GET" {
|
||||
// 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)
|
||||
if err != nil {
|
||||
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)
|
||||
} else {
|
||||
util.LogRequest(r, 404)
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue