merp/server.go

100 lines
2.3 KiB
Go

//3456789112345676892123456789312345678941234567895123456789612345678971234567898
package merp
import "encoding/json"
import "fmt"
import "net/http"
import "os"
import "time"
//import "github.com/rs/zerolog/log"
import "github.com/gin-gonic/gin"
import "github.com/dn365/gin-zerolog"
import "github.com/thoas/stats"
func ServeForever() {
s := getServer()
s.ListenAndServe()
}
func getHealthCheckHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
result := gin.H{
"status": "ok",
"now": time.Now().UTC().Format(time.RFC3339),
}
json, err := json.Marshal(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
}
}
func getStatsHandler(middleware *stats.Stats) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
stats := middleware.Data()
b, _ := json.Marshal(stats)
w.Write(b)
}
}
func getRouter() *gin.Engine {
if os.Getenv("DEBUG") == "" {
gin.SetMode(gin.ReleaseMode)
}
statsMiddleware := stats.New()
// empty router
r := gin.New()
// wrap panics:
r.Use(gin.Recovery())
// attach logger middleware
r.Use(ginzerolog.Logger("gin"))
r.Use(func(c *gin.Context) {
beginning, recorder := statsMiddleware.Begin(c.Writer)
c.Next()
statsMiddleware.End(beginning, stats.WithRecorder(recorder))
})
r.GET("/.well-known/healthcheck.json", gin.WrapF(getHealthCheckHandler()))
r.GET("/admin/healthcheck.json", gin.WrapF(getHealthCheckHandler()))
r.GET("/admin/stats.json", gin.WrapF(getStatsHandler(statsMiddleware)))
// 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")
}
s := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
return s
}