now seems to work for the most part
This commit is contained in:
parent
e0102004eb
commit
b4a7f23815
4
Makefile
4
Makefile
@ -23,11 +23,9 @@ ifeq ($(UNAME_S),Darwin)
|
||||
endif
|
||||
|
||||
ifneq ($(UNAME_S),Darwin)
|
||||
GOFLAGS := -ldflags "-static -linkmode external -extldflags $(GOLDFLAGS)"
|
||||
GOFLAGS = -ldflags "-linkmode external -extldflags -static $(GOLDFLAGS)"
|
||||
endif
|
||||
|
||||
|
||||
|
||||
default: run
|
||||
|
||||
run: build
|
||||
|
39
merp.go
39
merp.go
@ -4,10 +4,10 @@ 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/google/uuid"
|
||||
import "github.com/rs/zerolog/log"
|
||||
import "github.com/sneak/merp/models"
|
||||
|
||||
func thingRegex() *regexp.Regexp {
|
||||
@ -38,9 +38,14 @@ func GetLatestMerp() gin.HandlerFunc {
|
||||
*/
|
||||
|
||||
func HandleNewMerp() gin.HandlerFunc {
|
||||
// server startup time
|
||||
|
||||
THING_REGEX := thingRegex()
|
||||
// establish db connection *first*, before requests
|
||||
orm := models.GetOrmObject()
|
||||
|
||||
h := func(c *gin.Context) {
|
||||
// request time
|
||||
thing := c.Param("thing")
|
||||
if THING_REGEX.MatchString(thing) == false {
|
||||
log.Debug().Msgf("%s didnt match", thing)
|
||||
@ -54,12 +59,19 @@ func HandleNewMerp() gin.HandlerFunc {
|
||||
log.Debug().Msgf("%s matched", thing)
|
||||
//web.Get(`/merp/for/([A-Za-z0-9\-\_\.]+)`, merpHandler)
|
||||
|
||||
content := gin.H{}
|
||||
// FIXME rate limit this a bit on thing+clientip+json to cut down on
|
||||
// repeated messages
|
||||
|
||||
content := make(map[string]interface{})
|
||||
respContent := gin.H{}
|
||||
// FIXME support POST data as well
|
||||
|
||||
for k, v := range c.Request.URL.Query() {
|
||||
content[k] = v[0]
|
||||
respContent[k] = v[0]
|
||||
}
|
||||
|
||||
u := uuid.New()
|
||||
at := time.Now().UTC()
|
||||
atString := at.Format(time.RFC3339)
|
||||
|
||||
@ -71,7 +83,7 @@ func HandleNewMerp() gin.HandlerFunc {
|
||||
gin.H{
|
||||
"this": "failed",
|
||||
"status": http.StatusPreconditionFailed,
|
||||
"because": fmt.Sprintf("%s", jsonerr),
|
||||
"because": jsonerr.Error(),
|
||||
},
|
||||
)
|
||||
return
|
||||
@ -81,12 +93,22 @@ func HandleNewMerp() gin.HandlerFunc {
|
||||
Created: at,
|
||||
Thing: thing,
|
||||
Content: string(serialized),
|
||||
UUID: u.String(),
|
||||
}
|
||||
|
||||
orm := models.GetOrmObject()
|
||||
res, err := orm.Insert(&merp)
|
||||
_, err := orm.Insert(&merp)
|
||||
|
||||
log.Info().Msgf("res: %s, err: %s", res, err)
|
||||
if err != nil {
|
||||
c.JSON(
|
||||
http.StatusPreconditionFailed,
|
||||
gin.H{
|
||||
"this": "failed",
|
||||
"status": http.StatusPreconditionFailed,
|
||||
"because": err.Error(),
|
||||
},
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"this": "succeeded",
|
||||
@ -95,7 +117,8 @@ func HandleNewMerp() gin.HandlerFunc {
|
||||
"with": gin.H{
|
||||
"thing": thing,
|
||||
"created": atString,
|
||||
"content": content,
|
||||
"content": respContent,
|
||||
"id": u.String(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -9,12 +9,17 @@ import _ "github.com/lib/pq"
|
||||
var ormObject orm.Ormer
|
||||
|
||||
func Initialize() {
|
||||
if os.Getenv("DEBUG") != "" {
|
||||
orm.Debug = true
|
||||
}
|
||||
|
||||
ConnectToDb()
|
||||
SyncDB()
|
||||
}
|
||||
|
||||
// ConnectToDb - Initializes the ORM and Connection to the postgres DB
|
||||
func ConnectToDb() {
|
||||
|
||||
orm.DefaultTimeLoc = time.UTC
|
||||
|
||||
POSTGRES_DB_URL := os.Getenv("POSTGRES_DB_URL")
|
||||
|
@ -3,9 +3,10 @@ package models
|
||||
import "time"
|
||||
|
||||
type Merp struct {
|
||||
ID int `json:"id" orm:"auto"`
|
||||
Content string `json:"content" orm:"type(json)"`
|
||||
Created time.Time `orm:"auto_now_add;type(datetime)"`
|
||||
RemoteIP string `json:"remoteIP" orm:"size(128)"`
|
||||
Thing string `json:"thing" orm:"size(128)"`
|
||||
ID int `json:"id" orm:"auto;column(id)"`
|
||||
Content string `json:"content" orm:"type(jsonb);column(content)"`
|
||||
Created time.Time `orm:"auto_now_add;type(datetime);column(created)"`
|
||||
RemoteIP string `json:"remoteIP" orm:"size(128);column(remoteip)"`
|
||||
Thing string `json:"thing" orm:"size(256)"`
|
||||
UUID string `json:"uuid" orm:"size(36);column(uuid)"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user