now has an ORM and saves data (kinda)

This commit is contained in:
Jeffrey Paul 2019-10-27 05:38:03 -07:00
parent 9e9f49f139
commit e0102004eb
4 changed files with 147 additions and 18 deletions

View File

@ -14,16 +14,28 @@ GOLDFLAGS += -X main.Buildtime=$(BUILDTIME)
GOLDFLAGS += -X main.Builduser=$(BUILDUSER)@$(BUILDHOST)
GOLDFLAGS += -X main.Buildarch=$(BUILDARCH)
GOLDFLAGS += -X main.Appname=$(APPNAME)
GOFLAGS = -ldflags "-linkmode external -extldflags -static $(GOLDFLAGS)"
UNAME_S := $(shell uname -s)
# osx can't statically link apparently?!
ifeq ($(UNAME_S),Darwin)
GOFLAGS := -ldflags "$(GOLDFLAGS)"
endif
ifneq ($(UNAME_S),Darwin)
GOFLAGS := -ldflags "-static -linkmode external -extldflags $(GOLDFLAGS)"
endif
default: run
run: build
DEBUG=1 ./$(APPNAME)
DEBUG=1 PORT=1111 ./$(APPNAME)
build: ./$(APPNAME)
./$(APPNAME): *.go
./$(APPNAME): *.go models/*.go
go build -o $@ $(GOFLAGS) .
clean:

80
merp.go
View File

@ -1,50 +1,100 @@
//3456789112345676892123456789312345678941234567895123456789612345678971234567898
package main
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/sneak/merp/models"
/*
func GetLatestMerps() gin.HandlerFunc {
}
func GetLatestMerp() gin.HandlerFunc {
}
*/
func HandleNewMerp() gin.HandlerFunc {
func thingRegex() *regexp.Regexp {
THING_REGEX, e := regexp.Compile(`^[a-zA-Z0-9\_\-]+$`)
if e != nil {
panic(e)
}
return THING_REGEX
}
/*
func GetLatestMerps() gin.HandlerFunc {
THING_REGEX := thingRegex()
h := func(c *gin.Context) {
}
return h
}
func GetLatestMerp() gin.HandlerFunc {
THING_REGEX := thingRegex()
h := func(c *gin.Context) {
}
return h
}
*/
func HandleNewMerp() gin.HandlerFunc {
THING_REGEX := thingRegex()
h := func(c *gin.Context) {
thing := c.Param("thing")
if THING_REGEX.MatchString(thing) == false {
log.Info().Msgf("%s didnt match", thing)
log.Debug().Msgf("%s didnt match", thing)
c.JSON(http.StatusPreconditionFailed, gin.H{
"this": "failed",
"this": "failed",
"status": http.StatusPreconditionFailed,
"because": "invalid thing format, try a-zA-Z0-9-_",
})
return
}
log.Info().Msgf("%s matched", thing)
log.Debug().Msgf("%s matched", thing)
//web.Get(`/merp/for/([A-Za-z0-9\-\_\.]+)`, merpHandler)
content := gin.H{}
// FIXME support POST data as well
for k, v := range c.Request.URL.Query() {
content[k] = v[0]
}
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,
"because": fmt.Sprintf("%s", jsonerr),
},
)
return
}
merp := models.Merp{
Created: at,
Thing: thing,
Content: string(serialized),
}
orm := models.GetOrmObject()
res, err := orm.Insert(&merp)
log.Info().Msgf("res: %s, err: %s", res, err)
c.JSON(http.StatusOK, gin.H{
"this": "succeeded",
"by": "merping",
"the": "merp",
"with": gin.H{
"thing": thing,
"created": time.Now().UTC().Format(time.RFC3339),
"created": atString,
"content": content,
},
})

56
models/db.go Normal file
View File

@ -0,0 +1,56 @@
package models
import "os"
import "time"
import "github.com/astaxie/beego/orm"
import "github.com/rs/zerolog/log"
import _ "github.com/lib/pq"
var ormObject orm.Ormer
func Initialize() {
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")
orm.RegisterDriver("postgres", orm.DRPostgres)
orm.RegisterDataBase("default", "postgres", POSTGRES_DB_URL)
orm.SetMaxIdleConns("default", 1)
orm.SetMaxOpenConns("default", 5)
orm.RegisterModel(new(Merp))
ormObject = orm.NewOrm()
ormObject.Using("default")
}
func SyncDB() {
// Database alias.
name := "default"
// Drop table and re-create.
force := false
// Print log.
verbose := true
// Error.
err := orm.RunSyncdb(name, force, verbose)
if err != nil {
log.Fatal().Msg(err.Error())
}
}
// GetOrmObject - Getter function for the ORM object with which we can query the database
func GetOrmObject() orm.Ormer {
if ormObject == nil {
Initialize()
}
return ormObject
}

11
models/merp.go Normal file
View File

@ -0,0 +1,11 @@
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)"`
}