diff --git a/app.yaml b/app.yaml index 79506c9..1e30fe4 100644 --- a/app.yaml +++ b/app.yaml @@ -1,8 +1,5 @@ runtime: custom env: flex -env_variables: - DEBUG: 1 - includes: - prod-secrets.yaml diff --git a/indexpage.go b/indexpage.go new file mode 100644 index 0000000..270e3ae --- /dev/null +++ b/indexpage.go @@ -0,0 +1,13 @@ +package main + +const basePage = ` + + + + {{.Title}} + {{.Head}} + + + {{.Body}} + +` diff --git a/logging.go b/logging.go new file mode 100644 index 0000000..d42ee83 --- /dev/null +++ b/logging.go @@ -0,0 +1,28 @@ +//3456789112345676892123456789312345678941234567895123456789612345678971234567898 +package main + +import "os" +import "time" +import "github.com/rs/zerolog" +import "github.com/rs/zerolog/log" +import "golang.org/x/crypto/ssh/terminal" + +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.RFC3339Nano} + log.Logger = zerolog.New(output).With().Caller().Stack().Logger().With().Timestamp().Logger() + } + + zerolog.SetGlobalLevel(zerolog.InfoLevel) + if os.Getenv("DEBUG") != "" { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } +} diff --git a/main.go b/main.go index 4d04e20..8c331f0 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,7 @@ //3456789112345676892123456789312345678941234567895123456789612345678971234567898 -package merp +package main -import "os" -import "time" -import "github.com/rs/zerolog" import "github.com/rs/zerolog/log" -import "golang.org/x/crypto/ssh/terminal" var Version string var Buildtime string @@ -28,23 +24,3 @@ func identify() { Str("builduser", Builduser). Msg("starting") } - -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.RFC3339Nano} - log.Logger = zerolog.New(output).With().Caller().Stack().Logger().With().Timestamp().Logger() - } - - zerolog.SetGlobalLevel(zerolog.InfoLevel) - if os.Getenv("DEBUG") != "" { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } -} diff --git a/merp.go b/merp.go index 07f4e49..7f87b29 100644 --- a/merp.go +++ b/merp.go @@ -1,4 +1,4 @@ -package merp +package main import "encoding/json" import "net/http" @@ -169,10 +169,10 @@ func HandleNewMerp() gin.HandlerFunc { "by": "merping", "the": "merp", "with": gin.H{ - "thing": thing, - "created": atString, - "content": respContent, - "id": u.String(), + "thing": thing, + "created": atString, + "content": respContent, + "transaction": u.String(), }, }) } diff --git a/server.go b/server.go index 16b7aaa..1a3fd28 100644 --- a/server.go +++ b/server.go @@ -1,5 +1,5 @@ //3456789112345676892123456789312345678941234567895123456789612345678971234567898 -package merp +package main import "fmt" import "net/http" @@ -11,6 +11,12 @@ import "github.com/gin-gonic/gin" import "github.com/dn365/gin-zerolog" func serve() { + s := getServer() + s.ListenAndServe() +} + +func getRouter() *gin.Engine { + if os.Getenv("DEBUG") == "" { gin.SetMode(gin.ReleaseMode) } @@ -32,10 +38,18 @@ func serve() { }) // call it, it returns the appropriate handler function + // so we can execute some code at startup time + // and not just request time r.GET("/merp/for/:thing", HandleNewMerp()) r.GET("/get/latest/merp/for/:thing", GetLatestMerp()) r.GET("/get/merps/for/:thing", GetLatestMerps()) + return r +} + +func getServer() *http.Server { + r := getRouter() + port := "8080" if os.Getenv("PORT") != "" { port = os.Getenv("PORT") @@ -48,5 +62,5 @@ func serve() { WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } - s.ListenAndServe() + return s }