added GAE, testing

This commit is contained in:
Jeffrey Paul 2019-10-29 09:40:30 -07:00
parent 30e5760cf6
commit 73f2860add
6 changed files with 78 additions and 18 deletions

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
merp
prod-secrets.yaml

1
.gitignore vendored
View File

@ -1 +1,2 @@
merp
prod-secrets.yaml

8
app.yaml Normal file
View File

@ -0,0 +1,8 @@
runtime: custom
env: flex
env_variables:
DEBUG: 1
includes:
- prod-secrets.yaml

62
merp.go
View File

@ -18,11 +18,55 @@ func thingRegex() *regexp.Regexp {
return THING_REGEX
}
/*
func decodeJson(in []byte) interface{} {
var out interface{}
err := json.Unmarshal(in, &out)
if err != nil {
log.Error().Msg("error decoding json")
return nil
}
return out
}
func GetLatestMerps() gin.HandlerFunc {
THING_REGEX := thingRegex()
orm := models.GetOrmObject()
h := func(c *gin.Context) {
thing := c.Param("thing")
if THING_REGEX.MatchString(thing) == false {
c.JSON(http.StatusPreconditionFailed, gin.H{
"this": "failed",
"status": http.StatusPreconditionFailed,
"because": "invalid thing format, try a-zA-Z0-9-_",
})
return
}
var merps []*models.Merp
qs := orm.QueryTable("merp").Filter("thing", thing).OrderBy("-created").Limit(50)
qs.All(&merps)
var output []map[string]interface{}
for _, merp := range merps {
outelem := make(map[string]interface{})
outelem["thing"] = merp.Thing
outjs := decodeJson([]byte(merp.Content))
if outjs == nil {
outelem["content"] = gin.H{}
} else {
outelem["content"] = outjs
}
outelem["created"] = merp.Created
output = append(output, outelem)
}
c.JSON(http.StatusOK, gin.H{
"this": "succeeded",
"by": "getting",
"the": "merps",
"with": output,
})
}
return h
}
@ -31,11 +75,21 @@ func GetLatestMerp() gin.HandlerFunc {
THING_REGEX := thingRegex()
h := func(c *gin.Context) {
thing := c.Param("thing")
if THING_REGEX.MatchString(thing) == false {
c.JSON(http.StatusPreconditionFailed, gin.H{
"this": "failed",
"status": http.StatusPreconditionFailed,
"because": "invalid thing format, try a-zA-Z0-9-_",
})
return
}
c.JSON(http.StatusOK, gin.H{
"this": "succeeded",
})
}
return h
}
*/
func HandleNewMerp() gin.HandlerFunc {
// server startup time
@ -75,7 +129,7 @@ func HandleNewMerp() gin.HandlerFunc {
at := time.Now().UTC()
atString := at.Format(time.RFC3339)
serialized, jsonerr := json.Marshal(content)
serialized, jsonerr := json.MarshalIndent(content, "", " ")
if jsonerr != nil {
c.JSON(

View File

@ -3,10 +3,10 @@ package models
import "time"
type Merp struct {
ID int `json:"id" orm:"auto;column(id)"`
ID int `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)"`
RemoteIP string `orm:"size(128);column(remoteip)"`
Thing string `json:"thing" orm:"index;size(256)"`
UUID string `orm:"size(36);column(uuid)"`
}

View File

@ -10,14 +10,6 @@ import "time"
import "github.com/gin-gonic/gin"
import "github.com/dn365/gin-zerolog"
/* func initDb() {
db, err := sql.Open("postgres", os.Getenv("POSTGRES_DB_URL"))
if err != nil {
log.Fatal().Msg(err)
}
}
*/
func serve() {
if os.Getenv("DEBUG") == "" {
gin.SetMode(gin.ReleaseMode)
@ -32,14 +24,17 @@ func serve() {
// attach logger middleware
r.Use(ginzerolog.Logger("gin"))
r.GET("/ping", func(c *gin.Context) {
r.GET("/.well-known/healthcheck.json", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
"status": "ok",
"now": time.Now().UTC().Format(time.RFC3339),
})
})
// call it, it returns the appropriate handler function
r.GET("/merp/for/:thing", HandleNewMerp())
r.GET("/get/latest/merp/for/:thing", GetLatestMerp())
r.GET("/get/merps/for/:thing", GetLatestMerps())
port := "8080"
if os.Getenv("PORT") != "" {