2014-07-08 23:00:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2014-07-09 00:15:09 +00:00
|
|
|
"net/http"
|
2014-07-09 15:21:12 +00:00
|
|
|
"net/http/fcgi"
|
2014-07-09 00:15:09 +00:00
|
|
|
"path"
|
2014-07-08 23:00:49 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2014-07-09 00:15:09 +00:00
|
|
|
|
2014-07-08 23:00:49 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2014-07-09 01:27:05 +00:00
|
|
|
// Logger is a simple log handler, out puts in the standard of apache access log common.
|
|
|
|
// See http://httpd.apache.org/docs/2.2/logs.html#accesslog
|
2014-07-08 23:00:49 +00:00
|
|
|
func Logger() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
t := time.Now()
|
|
|
|
ip, err := net.ResolveTCPAddr("tcp", c.Req.RemoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
c.Abort(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
// before request
|
|
|
|
c.Next()
|
|
|
|
// after request
|
|
|
|
|
2014-07-09 00:32:45 +00:00
|
|
|
user := "-"
|
2014-07-08 23:00:49 +00:00
|
|
|
if c.Req.URL.User != nil {
|
|
|
|
user = c.Req.URL.User.Username()
|
|
|
|
}
|
|
|
|
|
|
|
|
latency := time.Since(t)
|
|
|
|
|
|
|
|
// 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",
|
|
|
|
ip.IP, user, t.Format(time.RFC3339), c.Req.Method, c.Req.URL.Path,
|
|
|
|
c.Req.Proto, c.Writer.Status(), c.Req.ContentLength, latency)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mainHandler(c *gin.Context) {
|
|
|
|
fields := strings.Split(c.Params.ByName("field"), ".")
|
|
|
|
ip, err := net.ResolveTCPAddr("tcp", c.Req.RemoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
c.Abort(500)
|
|
|
|
}
|
|
|
|
c.Set("ip", ip.IP.String())
|
|
|
|
c.Set("port", ip.Port)
|
|
|
|
c.Set("ua", c.Req.UserAgent())
|
|
|
|
c.Set("lang", c.Req.Header.Get("Accept-Language"))
|
|
|
|
c.Set("encoding", c.Req.Header.Get("Accept-Encoding"))
|
2014-07-09 19:57:43 +00:00
|
|
|
c.Set("method", c.Req.Method)
|
|
|
|
c.Set("mime", c.Req.Header.Get("Accept"))
|
|
|
|
c.Set("referer", c.Req.Header.Get("Referer"))
|
|
|
|
c.Set("forwarded", c.Req.Header.Get("X-Forwarded-For"))
|
2014-07-08 23:00:49 +00:00
|
|
|
|
|
|
|
hostnames, err := net.LookupAddr(ip.IP.String())
|
2014-07-09 00:15:09 +00:00
|
|
|
if err != nil {
|
2014-07-08 23:00:49 +00:00
|
|
|
c.Set("host", "")
|
|
|
|
} else {
|
|
|
|
c.Set("host", hostnames[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
wantsJSON := false
|
|
|
|
if len(fields) >= 2 && fields[1] == "json" {
|
|
|
|
wantsJSON = true
|
|
|
|
}
|
|
|
|
|
|
|
|
ua := strings.Split(c.Req.UserAgent(), "/")
|
|
|
|
switch fields[0] {
|
|
|
|
case "":
|
|
|
|
//If the user is using curl, then we should just return the IP, else we show the home page.
|
|
|
|
if ua[0] == "curl" {
|
|
|
|
c.String(200, fmt.Sprintln(ip.IP))
|
|
|
|
} else {
|
2014-07-09 00:15:09 +00:00
|
|
|
c.HTML(200, "index.html", c.Keys)
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
case "request":
|
|
|
|
c.JSON(200, c.Req)
|
|
|
|
return
|
|
|
|
case "all":
|
|
|
|
if wantsJSON {
|
|
|
|
c.JSON(200, c.Keys)
|
|
|
|
} else {
|
|
|
|
c.String(200, "%v", c.Keys)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldResult, err := c.Get(fields[0])
|
|
|
|
if err != nil {
|
|
|
|
c.String(404, "Not Found")
|
|
|
|
}
|
|
|
|
c.String(200, fmt.Sprintln(fieldResult))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-09 00:32:45 +00:00
|
|
|
// FileServer is a basic file serve handler, this is just here as an example.
|
|
|
|
// gin.Static() should be used instead
|
2014-07-08 23:00:49 +00:00
|
|
|
func FileServer(root string) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
file := c.Params.ByName("file")
|
|
|
|
if !strings.HasPrefix(file, "/") {
|
2014-07-09 00:15:09 +00:00
|
|
|
file = "/" + file
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
http.ServeFile(c.Writer, c.Req, path.Join(root, path.Clean(file)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := gin.New()
|
|
|
|
r.Use(gin.Recovery())
|
|
|
|
r.Use(Logger())
|
2014-07-09 00:15:09 +00:00
|
|
|
r.LoadHTMLTemplates("templates/*")
|
2014-07-08 23:00:49 +00:00
|
|
|
|
|
|
|
r.GET("/:field", mainHandler)
|
|
|
|
r.GET("/", mainHandler)
|
|
|
|
|
2014-07-09 15:21:12 +00:00
|
|
|
// Create a listener for FCGI
|
|
|
|
fcgi_listen, err := net.Listen("tcp", "127.0.0.1:4000")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
2014-07-09 15:21:12 +00:00
|
|
|
err = fcgi.Serve(fcgi_listen, r)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//port := os.Getenv("PORT")
|
|
|
|
//host := os.Getenv("HOST")
|
|
|
|
//if port == "" {
|
|
|
|
// port = "8080"
|
|
|
|
//}
|
|
|
|
//r.Run(host + ":" + port)
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|