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