This commit is contained in:
George Shammas 2014-08-10 19:02:21 +00:00
parent 8f8395a59a
commit 054faa87d5
1 changed files with 16 additions and 16 deletions

32
main.go
View File

@ -17,7 +17,7 @@ import (
func Logger() gin.HandlerFunc { func Logger() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
t := time.Now() t := time.Now()
ip, err := net.ResolveTCPAddr("tcp", c.Req.RemoteAddr) ip, err := net.ResolveTCPAddr("tcp", c.Request.RemoteAddr)
if err != nil { if err != nil {
c.Abort(500) c.Abort(500)
} }
@ -27,16 +27,16 @@ func Logger() gin.HandlerFunc {
// after request // after request
user := "-" user := "-"
if c.Req.URL.User != nil { if c.Request.URL.User != nil {
user = c.Req.URL.User.Username() user = c.Request.URL.User.Username()
} }
latency := time.Since(t) latency := time.Since(t)
// This is the format of Apache Log Common, with an additional field of latency // This is the format of Apache Log Common, with an additional field of latency
fmt.Printf("%v - %v [%v] \"%v %v %v\" %v %v %v\n", fmt.Printf("%v - %v [%v] \"%v %v %v\" %v %v %v\n",
ip.IP, user, t.Format(time.RFC3339), c.Req.Method, c.Req.URL.Path, ip.IP, user, t.Format(time.RFC3339), c.Request.Method, c.Request.URL.Path,
c.Req.Proto, c.Writer.Status(), c.Req.ContentLength, latency) c.Request.Proto, c.Writer.Status(), c.Request.ContentLength, latency)
} }
} }
@ -51,19 +51,19 @@ func stringInSlice(a string, list []string) bool {
func mainHandler(c *gin.Context) { func mainHandler(c *gin.Context) {
fields := strings.Split(c.Params.ByName("field"), ".") fields := strings.Split(c.Params.ByName("field"), ".")
ip, err := net.ResolveTCPAddr("tcp", c.Req.RemoteAddr) ip, err := net.ResolveTCPAddr("tcp", c.Request.RemoteAddr)
if err != nil { if err != nil {
c.Abort(500) c.Abort(500)
} }
c.Set("ip", ip.IP.String()) c.Set("ip", ip.IP.String())
c.Set("port", ip.Port) c.Set("port", ip.Port)
c.Set("ua", c.Req.UserAgent()) c.Set("ua", c.Request.UserAgent())
c.Set("lang", c.Req.Header.Get("Accept-Language")) c.Set("lang", c.Request.Header.Get("Accept-Language"))
c.Set("encoding", c.Req.Header.Get("Accept-Encoding")) c.Set("encoding", c.Request.Header.Get("Accept-Encoding"))
c.Set("method", c.Req.Method) c.Set("method", c.Request.Method)
c.Set("mime", c.Req.Header.Get("Accept")) c.Set("mime", c.Request.Header.Get("Accept"))
c.Set("referer", c.Req.Header.Get("Referer")) c.Set("referer", c.Request.Header.Get("Referer"))
c.Set("forwarded", c.Req.Header.Get("X-Forwarded-For")) c.Set("forwarded", c.Request.Header.Get("X-Forwarded-For"))
// Only lookup hostname if the results are going to need it. // Only lookup hostname if the results are going to need it.
if stringInSlice(fields[0], []string{"", "all", "host"}) { if stringInSlice(fields[0], []string{"", "all", "host"}) {
@ -80,7 +80,7 @@ func mainHandler(c *gin.Context) {
wantsJSON = true wantsJSON = true
} }
ua := strings.Split(c.Req.UserAgent(), "/") ua := strings.Split(c.Request.UserAgent(), "/")
switch fields[0] { switch fields[0] {
case "": case "":
//If the user is using curl, then we should just return the IP, else we show the home page. //If the user is using curl, then we should just return the IP, else we show the home page.
@ -91,7 +91,7 @@ func mainHandler(c *gin.Context) {
} }
return return
case "request": case "request":
c.JSON(200, c.Req) c.JSON(200, c.Request)
return return
case "all": case "all":
if wantsJSON { if wantsJSON {
@ -118,7 +118,7 @@ func FileServer(root string) gin.HandlerFunc {
if !strings.HasPrefix(file, "/") { if !strings.HasPrefix(file, "/") {
file = "/" + file file = "/" + file
} }
http.ServeFile(c.Writer, c.Req, path.Join(root, path.Clean(file))) http.ServeFile(c.Writer, c.Request, path.Join(root, path.Clean(file)))
} }
} }