This commit is contained in:
Jeffrey Paul 2019-11-07 00:51:35 -08:00
parent 2947ec68c1
commit ca5a726e75
6 changed files with 63 additions and 35 deletions

View File

@ -1,8 +1,5 @@
runtime: custom
env: flex
env_variables:
DEBUG: 1
includes:
- prod-secrets.yaml

13
indexpage.go Normal file
View File

@ -0,0 +1,13 @@
package main
const basePage = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{.Title}}</title>
{{.Head}}
</head>
<body>
{{.Body}}
</body>
</html>`

28
logging.go Normal file
View File

@ -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)
}
}

26
main.go
View File

@ -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)
}
}

10
merp.go
View File

@ -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(),
},
})
}

View File

@ -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
}