2019-11-08 12:35:27 +00:00
|
|
|
package merp
|
2019-10-03 19:30:04 +00:00
|
|
|
|
2019-11-08 13:01:57 +00:00
|
|
|
//3456789112345676892123456789312345678941234567895123456789612345678971234567898
|
|
|
|
|
2019-11-07 19:19:04 +00:00
|
|
|
import "encoding/json"
|
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"
|
2019-11-08 12:53:57 +00:00
|
|
|
import "github.com/didip/tollbooth"
|
|
|
|
import "github.com/didip/tollbooth_gin"
|
2019-10-25 16:17:14 +00:00
|
|
|
import "github.com/gin-gonic/gin"
|
|
|
|
import "github.com/dn365/gin-zerolog"
|
2019-11-08 12:35:27 +00:00
|
|
|
import "github.com/thoas/stats"
|
2019-10-03 19:30:04 +00:00
|
|
|
|
2019-11-08 13:01:57 +00:00
|
|
|
// ServeForever causes merp to serve http forever
|
2019-11-08 12:35:27 +00:00
|
|
|
func ServeForever() {
|
2019-11-07 08:51:35 +00:00
|
|
|
s := getServer()
|
|
|
|
s.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
2019-11-07 19:19:04 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:35:27 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 08:51:35 +00:00
|
|
|
func getRouter() *gin.Engine {
|
|
|
|
|
2019-10-25 16:17:14 +00:00
|
|
|
if os.Getenv("DEBUG") == "" {
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:53:57 +00:00
|
|
|
limiter := tollbooth.NewLimiter(5, nil)
|
|
|
|
|
2019-11-08 12:35:27 +00:00
|
|
|
statsMiddleware := stats.New()
|
|
|
|
|
2019-10-25 16:17:14 +00:00
|
|
|
// empty router
|
|
|
|
r := gin.New()
|
|
|
|
|
|
|
|
// wrap panics:
|
|
|
|
r.Use(gin.Recovery())
|
|
|
|
|
|
|
|
// attach logger middleware
|
|
|
|
r.Use(ginzerolog.Logger("gin"))
|
|
|
|
|
2019-11-08 12:35:27 +00:00
|
|
|
r.Use(func(c *gin.Context) {
|
|
|
|
beginning, recorder := statsMiddleware.Begin(c.Writer)
|
|
|
|
c.Next()
|
|
|
|
statsMiddleware.End(beginning, stats.WithRecorder(recorder))
|
|
|
|
})
|
|
|
|
|
2019-11-07 19:19:04 +00:00
|
|
|
r.GET("/.well-known/healthcheck.json", gin.WrapF(getHealthCheckHandler()))
|
|
|
|
r.GET("/admin/healthcheck.json", gin.WrapF(getHealthCheckHandler()))
|
2019-11-08 12:35:27 +00:00
|
|
|
r.GET("/admin/stats.json", gin.WrapF(getStatsHandler(statsMiddleware)))
|
2019-11-08 13:01:57 +00:00
|
|
|
r.GET("/admin/other.json", gin.WrapF(getStatsHandler(statsMiddleware)))
|
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-11-08 12:53:57 +00:00
|
|
|
r.GET("/merp/for/:thing", tollbooth_gin.LimitHandler(limiter), handleNewMerp())
|
|
|
|
r.GET("/get/latest/merp/for/:thing", tollbooth_gin.LimitHandler(limiter), getLatestMerp())
|
|
|
|
r.GET("/get/latest/merps", tollbooth_gin.LimitHandler(limiter), getLatestMerps())
|
|
|
|
r.GET("/get/merps/for/:thing", tollbooth_gin.LimitHandler(limiter), 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
|
|
|
}
|