Merge remote-tracking branch 'origin/master'

Squashing dummy index file

Conflicts:
	templates/index.html
This commit is contained in:
Zachary Elliott 2014-07-08 21:12:16 -04:00
commit 45b51dbb66
1 changed files with 14 additions and 9 deletions

23
main.go
View File

@ -3,14 +3,18 @@ package main
import ( import (
"fmt" "fmt"
"net" "net"
"net/http"
"os" "os"
"path"
"strings" "strings"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http"
"path"
) )
// Logger is a simple log handler, out puts in the standard of apache access log common
// http://httpd.apache.org/docs/2.2/logs.html#accesslog
func Logger() gin.HandlerFunc { func Logger() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
t := time.Now() t := time.Now()
@ -23,11 +27,9 @@ func Logger() gin.HandlerFunc {
c.Next() c.Next()
// after request // after request
var user string user := "-"
if c.Req.URL.User != nil { if c.Req.URL.User != nil {
user = c.Req.URL.User.Username() user = c.Req.URL.User.Username()
} else {
user = "-"
} }
latency := time.Since(t) latency := time.Since(t)
@ -39,7 +41,6 @@ func Logger() gin.HandlerFunc {
} }
} }
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.Req.RemoteAddr)
@ -53,7 +54,7 @@ func mainHandler(c *gin.Context) {
c.Set("encoding", c.Req.Header.Get("Accept-Encoding")) c.Set("encoding", c.Req.Header.Get("Accept-Encoding"))
hostnames, err := net.LookupAddr(ip.IP.String()) hostnames, err := net.LookupAddr(ip.IP.String())
if err != nil { if err != nil {
c.Set("host", "") c.Set("host", "")
} else { } else {
c.Set("host", hostnames[0]) c.Set("host", hostnames[0])
@ -71,7 +72,7 @@ func mainHandler(c *gin.Context) {
if ua[0] == "curl" { if ua[0] == "curl" {
c.String(200, fmt.Sprintln(ip.IP)) c.String(200, fmt.Sprintln(ip.IP))
} else { } else {
c.String(200, "Page Coming Soon") c.HTML(200, "index.html", c.Keys)
} }
return return
case "request": case "request":
@ -94,11 +95,14 @@ func mainHandler(c *gin.Context) {
} }
// FileServer is a basic file serve handler, this is just here as an example.
// gin.Static() should be used instead
func FileServer(root string) gin.HandlerFunc { func FileServer(root string) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
file := c.Params.ByName("file") file := c.Params.ByName("file")
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.Req, path.Join(root, path.Clean(file)))
} }
@ -108,6 +112,7 @@ func main() {
r := gin.New() r := gin.New()
r.Use(gin.Recovery()) r.Use(gin.Recovery())
r.Use(Logger()) r.Use(Logger())
r.LoadHTMLTemplates("templates/*")
r.GET("/:field", mainHandler) r.GET("/:field", mainHandler)
r.GET("/", mainHandler) r.GET("/", mainHandler)