From 73f2860add70b6650f35783018bbf4cd69f0bd69 Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Tue, 29 Oct 2019 09:40:30 -0700 Subject: [PATCH] added GAE, testing --- .dockerignore | 2 ++ .gitignore | 1 + app.yaml | 8 +++++++ merp.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++---- models/merp.go | 8 +++---- server.go | 15 ++++-------- 6 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 .dockerignore create mode 100644 app.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9d7d247 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +merp +prod-secrets.yaml diff --git a/.gitignore b/.gitignore index 6759a66..9d7d247 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ merp +prod-secrets.yaml diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..79506c9 --- /dev/null +++ b/app.yaml @@ -0,0 +1,8 @@ +runtime: custom +env: flex + +env_variables: + DEBUG: 1 + +includes: + - prod-secrets.yaml diff --git a/merp.go b/merp.go index 92ab0c3..4cecd3f 100644 --- a/merp.go +++ b/merp.go @@ -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( diff --git a/models/merp.go b/models/merp.go index e6f5ea1..d6e0eb4 100644 --- a/models/merp.go +++ b/models/merp.go @@ -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)"` } diff --git a/server.go b/server.go index 6430598..590f13a 100644 --- a/server.go +++ b/server.go @@ -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") != "" {