87 lines
1.6 KiB
Go
87 lines
1.6 KiB
Go
package feta
|
|
|
|
import "fmt"
|
|
import "net/http"
|
|
import "os"
|
|
import "strconv"
|
|
import "time"
|
|
|
|
import "github.com/rs/zerolog/log"
|
|
import "github.com/gin-gonic/gin"
|
|
import "github.com/dn365/gin-zerolog"
|
|
|
|
type fetaAPIServer struct {
|
|
feta *Process
|
|
port uint
|
|
router *gin.Engine
|
|
server *http.Server
|
|
debug bool
|
|
}
|
|
|
|
func (a *fetaAPIServer) setFeta(feta *Process) {
|
|
a.feta = feta
|
|
}
|
|
|
|
func (a *fetaAPIServer) Serve() {
|
|
if a.feta == nil {
|
|
panic("must have feta app from which to serve stats")
|
|
}
|
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
a.debug = true
|
|
}
|
|
|
|
a.port = 8080
|
|
|
|
if os.Getenv("PORT") != "" {
|
|
s, err := strconv.ParseUint(os.Getenv("PORT"), 10, 64)
|
|
if err != nil {
|
|
a.port = uint(s)
|
|
}
|
|
}
|
|
|
|
a.initRouter()
|
|
a.initServer()
|
|
err := a.server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("webserver failure")
|
|
return
|
|
}
|
|
}
|
|
|
|
func (a *fetaAPIServer) initRouter() {
|
|
|
|
// 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", gin.WrapF(a.getHealthCheckHandler()))
|
|
r.GET("/", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/feta") })
|
|
r.GET("/feta", gin.WrapF(a.getIndexHandler()))
|
|
r.GET("/feta/list/instances", gin.WrapF(a.getInstanceListHandler()))
|
|
|
|
a.router = r
|
|
}
|
|
|
|
func (a *fetaAPIServer) initServer() {
|
|
if !a.debug {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
|
|
log.Info().Uint("port", a.port).Msg("starting webserver")
|
|
|
|
a.server = &http.Server{
|
|
Addr: fmt.Sprintf(":%d", a.port),
|
|
Handler: a.router,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
MaxHeaderBytes: 1 << 20,
|
|
}
|
|
}
|