feta/api.go

93 lines
1.9 KiB
Go

package feta
import "fmt"
import "os"
import "time"
import "net/http"
import "github.com/rs/zerolog/log"
import "github.com/gin-gonic/gin"
import "github.com/dn365/gin-zerolog"
type TootArchiverAPIServer struct {
archiver *TootArchiver
}
func (self *TootArchiverAPIServer) SetArchiver(archiver *TootArchiver) {
self.archiver = archiver
}
func (a *TootArchiverAPIServer) Serve() {
if a.archiver == nil {
panic("must have archiver from which to serve stats")
}
s := a.getServer()
err := s.ListenAndServe()
if err != nil {
log.Fatal().Msg("webserver failure: " + err.Error())
return
}
}
func (a *TootArchiverAPIServer) getRouter() *gin.Engine {
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"))
r.GET("/.well-known/healthcheck.json", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "ok",
"now": time.Now().UTC().Format(time.RFC3339),
"uptime": a.archiver.Uptime().String(),
})
})
r.GET("/", func(c *gin.Context) {
ir := a.archiver.manager.instanceSummaryReport()
il := a.archiver.manager.instanceListForApi()
c.JSON(200, gin.H{
"status": "ok",
"now": time.Now().UTC().Format(time.RFC3339),
"uptime": a.archiver.Uptime().String(),
"instanceSummary": gin.H{
"total": ir.total,
"up": ir.up,
"identified": ir.identified,
},
"instanceList": il,
})
})
return r
}
func (a *TootArchiverAPIServer) getServer() *http.Server {
r := a.getRouter()
port := "8080"
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
log.Info().Str("port", port).Msg("starting webserver")
s := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
return s
}