merp/server.go

67 lines
1.3 KiB
Go
Raw Normal View History

2019-10-03 19:30:04 +00:00
//3456789112345676892123456789312345678941234567895123456789612345678971234567898
2019-11-07 08:51:35 +00:00
package main
2019-10-03 19:30:04 +00:00
2019-10-25 15:09:31 +00:00
import "fmt"
2019-10-25 16:17:14 +00:00
import "net/http"
2019-10-25 15:09:31 +00:00
import "os"
2019-10-25 16:17:14 +00:00
import "time"
//import "github.com/rs/zerolog/log"
import "github.com/gin-gonic/gin"
import "github.com/dn365/gin-zerolog"
2019-10-03 19:30:04 +00:00
2019-10-25 15:09:31 +00:00
func serve() {
2019-11-07 08:51:35 +00:00
s := getServer()
s.ListenAndServe()
}
func getRouter() *gin.Engine {
2019-10-25 16:17:14 +00:00
if os.Getenv("DEBUG") == "" {
gin.SetMode(gin.ReleaseMode)
}
// empty router
r := gin.New()
// wrap panics:
r.Use(gin.Recovery())
// attach logger middleware
r.Use(ginzerolog.Logger("gin"))
2019-10-29 16:40:30 +00:00
r.GET("/.well-known/healthcheck.json", func(c *gin.Context) {
2019-10-25 16:17:14 +00:00
c.JSON(200, gin.H{
2019-10-29 16:40:30 +00:00
"status": "ok",
"now": time.Now().UTC().Format(time.RFC3339),
2019-10-25 16:17:14 +00:00
})
})
// call it, it returns the appropriate handler function
2019-11-07 08:51:35 +00:00
// so we can execute some code at startup time
// and not just request time
2019-10-25 16:17:14 +00:00
r.GET("/merp/for/:thing", HandleNewMerp())
2019-10-29 16:40:30 +00:00
r.GET("/get/latest/merp/for/:thing", GetLatestMerp())
r.GET("/get/merps/for/:thing", GetLatestMerps())
2019-10-25 16:17:14 +00:00
2019-11-07 08:51:35 +00:00
return r
}
func getServer() *http.Server {
r := getRouter()
2019-10-25 16:17:14 +00:00
port := "8080"
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
2019-10-25 15:09:31 +00:00
}
2019-10-03 19:30:04 +00:00
2019-10-25 16:17:14 +00:00
s := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
2019-10-03 19:30:04 +00:00
}
2019-11-07 08:51:35 +00:00
return s
2019-10-03 19:30:04 +00:00
}