From 2991852b72db8eccc26620594f5d9b9d63692129 Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Fri, 25 Oct 2019 09:17:14 -0700 Subject: [PATCH] latest --- Makefile | 23 +++++++++++++++++------ README.md | 4 ++-- main.go | 10 ++++++++-- merp.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ server.go | 54 ++++++++++++++++++++++++++++++++++++++---------------- 5 files changed, 117 insertions(+), 26 deletions(-) create mode 100644 merp.go diff --git a/Makefile b/Makefile index 7ccc1d6..13a8c19 100644 --- a/Makefile +++ b/Makefile @@ -3,28 +3,39 @@ BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ') BUILDUSER := $(shell whoami) BUILDHOST := $(shell hostname -s) BUILDARCH := $(shell uname -m) +APPNAME := merp GOLDFLAGS += -X main.Version=$(VERSION) GOLDFLAGS += -X main.Buildtime=$(BUILDTIME) GOLDFLAGS += -X main.Builduser=$(BUILDUSER)@$(BUILDHOST) GOLDFLAGS += -X main.Buildarch=$(BUILDARCH) +GOLDFLAGS += -X main.Appname=$(APPNAME) GOFLAGS = -ldflags "$(GOLDFLAGS)" default: run run: build - DEBUG=1 ./pooteeweet + DEBUG=1 ./$(APPNAME) -build: ./pooteeweet +build: ./$(APPNAME) -./pooteeweet: *.go +./$(APPNAME): *.go go build -o $@ $(GOFLAGS) . clean: - rm pooteeweet + rm $(APPNAME) fmt: go fmt *.go -test: - docker build -t sneak/pooteeweet . +test: build-image + +build-and-push-image: push-image + +push-image: build-image + docker push sneak/$(APPNAME) + +build-image: + docker build \ + -t sneak/$(APPNAME):$(VERSION) \ + . diff --git a/README.md b/README.md index 8bff85d..afea625 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# pooteetweet +# merp -clone of a popular iot api to learn more net/http +clone of a popular iot api to learn more go # status diff --git a/main.go b/main.go index 6e13e6d..81189a1 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ var Version string var Buildtime string var Builduser string var Buildarch string -var Appname string = "pooteeweet" +var Appname string func main() { initLogging() @@ -30,10 +30,16 @@ func identify() { } func initLogging() { + + // always log in UTC + zerolog.TimestampFunc = func() time.Time { + return time.Now().UTC() + } + log.Logger = log.With().Caller().Stack().Logger() if terminal.IsTerminal(int(os.Stdout.Fd())) { - output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339} + output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339Nano} log.Logger = zerolog.New(output).With().Caller().Stack().Logger().With().Timestamp().Logger() } diff --git a/merp.go b/merp.go new file mode 100644 index 0000000..080991d --- /dev/null +++ b/merp.go @@ -0,0 +1,52 @@ +//3456789112345676892123456789312345678941234567895123456789612345678971234567898 +package main + +import "net/http" +import "regexp" +import "time" +import "github.com/rs/zerolog/log" +import "github.com/gin-gonic/gin" + +func GetLatestMerps() gin.HandlerFunc { +} + +func GetLatestMerp() gin.HandlerFunc { +} + +func HandleNewMerp() gin.HandlerFunc { + + THING_REGEX, e := regexp.Compile(`^[a-zA-Z0-9\_\-]+$`) + if e != nil { + panic(e) + } + + h := func(c *gin.Context) { + thing := c.Param("thing") + if THING_REGEX.MatchString(thing) == false { + log.Info().Msgf("%s didnt match", thing) + c.JSON(http.StatusPreconditionFailed, gin.H{ + "this": "failed", + }) + return + } + log.Info().Msgf("%s matched", thing) + //web.Get(`/merp/for/([A-Za-z0-9\-\_\.]+)`, merpHandler) + content := gin.H{} + for k, v := range c.Request.URL.Query() { + content[k] = v[0] + } + + c.JSON(http.StatusOK, gin.H{ + "this": "succeeded", + "by": "merping", + "the": "merp", + "with": gin.H{ + "thing": thing, + "created": time.Now().UTC().Format(time.RFC3339), + "content": content, + }, + }) + } + + return h +} diff --git a/server.go b/server.go index 0b20272..6430598 100644 --- a/server.go +++ b/server.go @@ -2,9 +2,13 @@ package main import "fmt" +import "net/http" import "os" -import "github.com/rs/zerolog/log" -import "github.com/hoisie/web" +import "time" + +//import "github.com/rs/zerolog/log" +import "github.com/gin-gonic/gin" +import "github.com/dn365/gin-zerolog" /* func initDb() { db, err := sql.Open("postgres", os.Getenv("POSTGRES_DB_URL")) @@ -14,22 +18,40 @@ import "github.com/hoisie/web" } */ -const defaultPort int = 8080 - func serve() { - port := os.Getenv("PORT") - if port == "" { - port = fmt.Sprintf("%d", defaultPort) + if os.Getenv("DEBUG") == "" { + gin.SetMode(gin.ReleaseMode) } - // FIXME web.SetLogger(log.Logger) - web.Get(`/dweet/for/([A-Za-z0-9\-\_\.]+)`, dweetHandler) - web.Run(fmt.Sprintf("0.0.0.0:%s", port)) -} -func dweetHandler(ctx *web.Context, val string) string { - log.Info().Msg("in hello()") - for k, v := range ctx.Params { - println(k, v) + // empty router + r := gin.New() + + // wrap panics: + r.Use(gin.Recovery()) + + // attach logger middleware + r.Use(ginzerolog.Logger("gin")) + + r.GET("/ping", func(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "pong", + }) + }) + + // call it, it returns the appropriate handler function + r.GET("/merp/for/:thing", HandleNewMerp()) + + port := "8080" + if os.Getenv("PORT") != "" { + port = os.Getenv("PORT") } - return ("hello " + val) + + s := &http.Server{ + Addr: fmt.Sprintf(":%s", port), + Handler: r, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + MaxHeaderBytes: 1 << 20, + } + s.ListenAndServe() }