Created
August 31, 2018 12:59
-
-
Save KageShiron/dd54d3168d0b629c40cb30d5f2d7c2a8 to your computer and use it in GitHub Desktop.
Post RequestBin 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"log" | |
"net/http" | |
"strconv" | |
"time" | |
"github.com/gin-gonic/gin/render" | |
"github.com/gin-gonic/gin" | |
) | |
type AccessRecord struct { | |
Time time.Time | |
Method string | |
Header http.Header | |
Url string | |
Body string | |
IpAddress string | |
} | |
var Save map[string][]AccessRecord = map[string][]AccessRecord{} | |
const Domain = "test.local" | |
func Logger() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
} | |
} | |
/* | |
func DynamicSubDomain() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
if c.Request.Host != Domain && c.Request.Host != "127.0.0.1:8000" { | |
log.Println(c.Request.URL) | |
director := func(req *http.Request) { | |
req = c.Request | |
req.Host = "127.0.0.1:8080" | |
req.URL.Host = req.Host | |
req.URL.Path = "/v1/log/" + c.Request.Host + c.Request.URL.Path | |
log.Println(req.URL) | |
} | |
proxy := &httputil.ReverseProxy{Director: director} | |
proxy.ServeHTTP(c.Writer, c.Request) | |
return | |
} | |
} | |
}*/ | |
// /:id/in/*path | |
func inHandler(c *gin.Context) { | |
id := c.Param("id") | |
_, ok := Save[id] | |
if !ok { | |
c.JSON(404, map[string]string{"Error": "NotFound"}) | |
} | |
//sd := c.Request.Host[:(len(c.Request.Host) - len(Domain) - 1)] | |
buf := new(bytes.Buffer) | |
buf.ReadFrom(c.Request.Body) | |
Save[id] = append(Save[id], AccessRecord{ | |
Header: c.Request.Header, | |
Url: c.Request.URL.String(), | |
Method: c.Request.Method, | |
Time: time.Now(), | |
Body: buf.String(), | |
IpAddress: c.ClientIP(), | |
}) | |
c.String(200, "ok") | |
} | |
func main() { | |
r := gin.New() | |
r.Use(Logger()) | |
//r.Use(DynamicSubDomain()) | |
r.RedirectTrailingSlash = true | |
v1 := r.Group("/v1") | |
{ | |
v1.Any("/:id/in/*path", inHandler) | |
v1.GET("/:id/items/:num", func(c *gin.Context) { | |
id := c.Param("id") | |
vals, ok := Save[id] | |
if !ok { | |
c.JSON(404, map[string]string{"Error": "Not Created"}) | |
return | |
} | |
index, err := strconv.Atoi(c.Param("name")) | |
if err != nil { | |
c.JSON(400, map[string]string{"Error": `"num" is not a number.`}) | |
return | |
} | |
val := vals[index] | |
c.JSON(200, val) | |
}) | |
v1.GET("/:id/items", func(c *gin.Context) { | |
//sd := c.Request.Host[:(len(c.Request.Host) - len(Domain) - 1)] | |
id := c.Param("id") | |
val, ok := Save[id] | |
if !ok { | |
c.JSON(404, map[string]string{"Error": "Not Created"}) | |
return | |
} | |
x := render.JSON{Data: val} | |
log.Println(x) | |
c.JSON(200, val) | |
}) | |
v1.POST("/:id/create", func(c *gin.Context) { | |
id := c.Param("id") | |
_, ok := Save[id] | |
if ok { | |
c.JSON(400, map[string]string{"Error": `"{id}" already exists.`}) | |
return | |
} | |
Save[id] = new() | |
}) | |
} | |
// Listen and serve on 0.0.0.0:8080 | |
r.Run("127.0.0.1:8080") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment