merp/merp.go

128 lines
2.4 KiB
Go
Raw Normal View History

2019-10-25 16:17:14 +00:00
package main
2019-10-27 12:38:03 +00:00
import "encoding/json"
2019-10-25 16:17:14 +00:00
import "net/http"
import "regexp"
import "time"
2019-10-27 12:38:03 +00:00
2019-10-25 16:17:14 +00:00
import "github.com/gin-gonic/gin"
2019-10-27 13:26:07 +00:00
import "github.com/google/uuid"
import "github.com/rs/zerolog/log"
2019-10-27 12:38:03 +00:00
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
}
2019-10-25 16:17:14 +00:00
2019-10-25 16:25:17 +00:00
/*
2019-10-25 16:17:14 +00:00
func GetLatestMerps() gin.HandlerFunc {
2019-10-27 12:38:03 +00:00
THING_REGEX := thingRegex()
h := func(c *gin.Context) {
}
return h
2019-10-25 16:17:14 +00:00
}
func GetLatestMerp() gin.HandlerFunc {
2019-10-27 12:38:03 +00:00
THING_REGEX := thingRegex()
h := func(c *gin.Context) {
}
return h
2019-10-25 16:17:14 +00:00
}
2019-10-25 16:25:17 +00:00
*/
2019-10-25 16:17:14 +00:00
func HandleNewMerp() gin.HandlerFunc {
2019-10-27 13:26:07 +00:00
// server startup time
2019-10-27 12:38:03 +00:00
THING_REGEX := thingRegex()
2019-10-27 13:26:07 +00:00
// establish db connection *first*, before requests
orm := models.GetOrmObject()
2019-10-25 16:17:14 +00:00
h := func(c *gin.Context) {
2019-10-27 13:26:07 +00:00
// request time
2019-10-25 16:17:14 +00:00
thing := c.Param("thing")
if THING_REGEX.MatchString(thing) == false {
2019-10-27 12:38:03 +00:00
log.Debug().Msgf("%s didnt match", thing)
2019-10-25 16:17:14 +00:00
c.JSON(http.StatusPreconditionFailed, gin.H{
2019-10-27 12:38:03 +00:00
"this": "failed",
"status": http.StatusPreconditionFailed,
"because": "invalid thing format, try a-zA-Z0-9-_",
2019-10-25 16:17:14 +00:00
})
return
}
2019-10-27 12:38:03 +00:00
log.Debug().Msgf("%s matched", thing)
2019-10-25 16:17:14 +00:00
//web.Get(`/merp/for/([A-Za-z0-9\-\_\.]+)`, merpHandler)
2019-10-27 12:38:03 +00:00
2019-10-27 13:26:07 +00:00
// FIXME rate limit this a bit on thing+clientip+json to cut down on
// repeated messages
content := make(map[string]interface{})
respContent := gin.H{}
2019-10-27 12:38:03 +00:00
// FIXME support POST data as well
2019-10-27 13:26:07 +00:00
2019-10-25 16:17:14 +00:00
for k, v := range c.Request.URL.Query() {
content[k] = v[0]
2019-10-27 13:26:07 +00:00
respContent[k] = v[0]
2019-10-25 16:17:14 +00:00
}
2019-10-27 13:26:07 +00:00
u := uuid.New()
2019-10-27 12:38:03 +00:00
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,
2019-10-27 13:26:07 +00:00
"because": jsonerr.Error(),
2019-10-27 12:38:03 +00:00
},
)
return
}
merp := models.Merp{
Created: at,
Thing: thing,
Content: string(serialized),
2019-10-27 13:26:07 +00:00
UUID: u.String(),
2019-10-27 12:38:03 +00:00
}
2019-10-27 13:26:07 +00:00
_, err := orm.Insert(&merp)
2019-10-27 12:38:03 +00:00
2019-10-27 13:26:07 +00:00
if err != nil {
c.JSON(
http.StatusPreconditionFailed,
gin.H{
"this": "failed",
"status": http.StatusPreconditionFailed,
"because": err.Error(),
},
)
return
}
2019-10-27 12:38:03 +00:00
2019-10-25 16:17:14 +00:00
c.JSON(http.StatusOK, gin.H{
"this": "succeeded",
"by": "merping",
"the": "merp",
"with": gin.H{
"thing": thing,
2019-10-27 12:38:03 +00:00
"created": atString,
2019-10-27 13:26:07 +00:00
"content": respContent,
"id": u.String(),
2019-10-25 16:17:14 +00:00
},
})
}
return h
}