2019-12-19 14:24:26 +00:00
|
|
|
package process
|
2019-11-02 06:56:17 +00:00
|
|
|
|
|
|
|
import "fmt"
|
2019-11-09 06:13:19 +00:00
|
|
|
import "net/http"
|
2019-11-02 06:56:17 +00:00
|
|
|
import "os"
|
2019-11-09 06:13:19 +00:00
|
|
|
import "strconv"
|
2019-11-02 06:56:17 +00:00
|
|
|
import "time"
|
|
|
|
|
|
|
|
import "github.com/rs/zerolog/log"
|
|
|
|
import "github.com/gin-gonic/gin"
|
|
|
|
import "github.com/dn365/gin-zerolog"
|
|
|
|
|
2019-12-19 14:24:26 +00:00
|
|
|
// Server is the HTTP webserver object
|
|
|
|
type Server struct {
|
|
|
|
feta *Feta
|
2019-11-09 06:13:19 +00:00
|
|
|
port uint
|
|
|
|
router *gin.Engine
|
|
|
|
server *http.Server
|
|
|
|
debug bool
|
2019-11-02 06:56:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 14:24:26 +00:00
|
|
|
// SetFeta tells the http Server where to find the Process object so that it
|
|
|
|
// can pull stats and other information for serving via http
|
|
|
|
func (a *Server) SetFeta(feta *Feta) {
|
2019-12-14 15:24:42 +00:00
|
|
|
a.feta = feta
|
2019-11-05 02:57:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 14:24:26 +00:00
|
|
|
// Serve is the entrypoint for the Server, which should run in its own
|
|
|
|
// goroutine (started by the Process)
|
|
|
|
func (a *Server) Serve() {
|
2019-11-06 01:15:31 +00:00
|
|
|
if a.feta == nil {
|
|
|
|
panic("must have feta app from which to serve stats")
|
2019-11-02 06:56:17 +00:00
|
|
|
}
|
2019-11-09 06:13:19 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2019-11-02 06:56:17 +00:00
|
|
|
if err != nil {
|
2019-11-09 06:13:19 +00:00
|
|
|
log.Fatal().Err(err).Msg("webserver failure")
|
2019-11-02 06:56:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 14:24:26 +00:00
|
|
|
func (a *Server) initRouter() {
|
2019-11-02 06:56:17 +00:00
|
|
|
|
|
|
|
// empty router
|
|
|
|
r := gin.New()
|
|
|
|
|
|
|
|
// wrap panics:
|
|
|
|
r.Use(gin.Recovery())
|
|
|
|
|
|
|
|
// attach logger middleware
|
|
|
|
r.Use(ginzerolog.Logger("gin"))
|
|
|
|
|
2019-11-05 23:32:09 +00:00
|
|
|
r.GET("/.well-known/healthcheck.json", gin.WrapF(a.getHealthCheckHandler()))
|
2019-11-06 07:03:42 +00:00
|
|
|
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()))
|
2019-11-02 06:56:17 +00:00
|
|
|
|
2019-11-09 06:13:19 +00:00
|
|
|
a.router = r
|
2019-11-02 06:56:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 14:24:26 +00:00
|
|
|
func (a *Server) initServer() {
|
2019-11-09 06:13:19 +00:00
|
|
|
if !a.debug {
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
2019-11-02 06:56:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 06:13:19 +00:00
|
|
|
log.Info().Uint("port", a.port).Msg("starting webserver")
|
2019-11-02 06:56:17 +00:00
|
|
|
|
2019-11-09 06:13:19 +00:00
|
|
|
a.server = &http.Server{
|
|
|
|
Addr: fmt.Sprintf(":%d", a.port),
|
|
|
|
Handler: a.router,
|
2019-11-02 06:56:17 +00:00
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
MaxHeaderBytes: 1 << 20,
|
|
|
|
}
|
|
|
|
}
|