refactored some things

This commit is contained in:
Jeffrey Paul 2019-11-08 22:13:19 -08:00
parent 86db12d02d
commit 03887d4a1d
3 changed files with 37 additions and 24 deletions

View File

@ -71,7 +71,6 @@ func (a *FetaAPIServer) getInstanceListHandler() http.HandlerFunc {
func (a *FetaAPIServer) getIndexHandler() http.HandlerFunc { func (a *FetaAPIServer) getIndexHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
index := &gin.H{ index := &gin.H{
"server": &gin.H{ "server": &gin.H{
"now": time.Now().UTC().Format(time.RFC3339), "now": time.Now().UTC().Format(time.RFC3339),
@ -94,7 +93,6 @@ func (a *FetaAPIServer) getIndexHandler() http.HandlerFunc {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.Write(json) w.Write(json)
} }
} }

View File

@ -1,9 +1,10 @@
package feta package feta
import "fmt" import "fmt"
import "os"
import "time"
import "net/http" import "net/http"
import "os"
import "strconv"
import "time"
import "github.com/rs/zerolog/log" import "github.com/rs/zerolog/log"
import "github.com/gin-gonic/gin" import "github.com/gin-gonic/gin"
@ -11,6 +12,10 @@ import "github.com/dn365/gin-zerolog"
type FetaAPIServer struct { type FetaAPIServer struct {
feta *FetaProcess feta *FetaProcess
port uint
router *gin.Engine
server *http.Server
debug bool
} }
func (self *FetaAPIServer) SetFeta(feta *FetaProcess) { func (self *FetaAPIServer) SetFeta(feta *FetaProcess) {
@ -21,18 +26,31 @@ func (a *FetaAPIServer) Serve() {
if a.feta == nil { if a.feta == nil {
panic("must have feta app from which to serve stats") panic("must have feta app from which to serve stats")
} }
s := a.getServer()
err := s.ListenAndServe() 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 { if err != nil {
log.Fatal().Msg("webserver failure: " + err.Error()) a.port = uint(s)
}
}
a.initRouter()
a.initServer()
err := a.server.ListenAndServe()
if err != nil {
log.Fatal().Err(err).Msg("webserver failure")
return return
} }
} }
func (a *FetaAPIServer) getRouter() *gin.Engine { func (a *FetaAPIServer) initRouter() {
if os.Getenv("DEBUG") == "" {
gin.SetMode(gin.ReleaseMode)
}
// empty router // empty router
r := gin.New() r := gin.New()
@ -48,25 +66,21 @@ func (a *FetaAPIServer) getRouter() *gin.Engine {
r.GET("/feta", gin.WrapF(a.getIndexHandler())) r.GET("/feta", gin.WrapF(a.getIndexHandler()))
r.GET("/feta/list/instances", gin.WrapF(a.getInstanceListHandler())) r.GET("/feta/list/instances", gin.WrapF(a.getInstanceListHandler()))
return r a.router = r
} }
func (a *FetaAPIServer) getServer() *http.Server { func (a *FetaAPIServer) initServer() {
r := a.getRouter() if !a.debug {
gin.SetMode(gin.ReleaseMode)
port := "8080"
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
} }
log.Info().Str("port", port).Msg("starting webserver") log.Info().Uint("port", a.port).Msg("starting webserver")
s := &http.Server{ a.server = &http.Server{
Addr: fmt.Sprintf(":%s", port), Addr: fmt.Sprintf(":%d", a.port),
Handler: r, Handler: a.router,
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20, MaxHeaderBytes: 1 << 20,
} }
return s
} }

View File

@ -10,6 +10,7 @@ import "github.com/rs/zerolog/log"
const HOST_DISCOVERY_PARALLELISM = 1 const HOST_DISCOVERY_PARALLELISM = 1
type InstanceBackend interface { type InstanceBackend interface {
//FIXME //FIXME
} }