package main import "encoding/json" import "net/http" import "regexp" import "time" import "fmt" import "github.com/rs/zerolog/log" import "github.com/gin-gonic/gin" import "github.com/sneak/merp/models" func thingRegex() *regexp.Regexp { THING_REGEX, e := regexp.Compile(`^[a-zA-Z0-9\_\-]+$`) if e != nil { panic(e) } return THING_REGEX } /* func GetLatestMerps() gin.HandlerFunc { THING_REGEX := thingRegex() h := func(c *gin.Context) { } return h } func GetLatestMerp() gin.HandlerFunc { THING_REGEX := thingRegex() h := func(c *gin.Context) { } return h } */ func HandleNewMerp() gin.HandlerFunc { THING_REGEX := thingRegex() h := func(c *gin.Context) { thing := c.Param("thing") if THING_REGEX.MatchString(thing) == false { log.Debug().Msgf("%s didnt match", thing) c.JSON(http.StatusPreconditionFailed, gin.H{ "this": "failed", "status": http.StatusPreconditionFailed, "because": "invalid thing format, try a-zA-Z0-9-_", }) return } log.Debug().Msgf("%s matched", thing) //web.Get(`/merp/for/([A-Za-z0-9\-\_\.]+)`, merpHandler) content := gin.H{} // FIXME support POST data as well for k, v := range c.Request.URL.Query() { content[k] = v[0] } at := time.Now().UTC() atString := at.Format(time.RFC3339) serialized, jsonerr := json.Marshal(content) if jsonerr != nil { c.JSON( http.StatusPreconditionFailed, gin.H{ "this": "failed", "status": http.StatusPreconditionFailed, "because": fmt.Sprintf("%s", jsonerr), }, ) return } merp := models.Merp{ Created: at, Thing: thing, Content: string(serialized), } orm := models.GetOrmObject() res, err := orm.Insert(&merp) log.Info().Msgf("res: %s, err: %s", res, err) c.JSON(http.StatusOK, gin.H{ "this": "succeeded", "by": "merping", "the": "merp", "with": gin.H{ "thing": thing, "created": atString, "content": content, }, }) } return h }