latest
This commit is contained in:
parent
e2adbd1629
commit
2991852b72
23
Makefile
23
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) \
|
||||
.
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
10
main.go
10
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()
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
54
server.go
54
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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue