refactored some things
This commit is contained in:
parent
86db12d02d
commit
03887d4a1d
@ -71,7 +71,6 @@ func (a *FetaAPIServer) getInstanceListHandler() http.HandlerFunc {
|
||||
|
||||
func (a *FetaAPIServer) getIndexHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
index := &gin.H{
|
||||
"server": &gin.H{
|
||||
"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.Write(json)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
58
apiserver.go
58
apiserver.go
@ -1,16 +1,21 @@
|
||||
package feta
|
||||
|
||||
import "fmt"
|
||||
import "os"
|
||||
import "time"
|
||||
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 *FetaProcess
|
||||
feta *FetaProcess
|
||||
port uint
|
||||
router *gin.Engine
|
||||
server *http.Server
|
||||
debug bool
|
||||
}
|
||||
|
||||
func (self *FetaAPIServer) SetFeta(feta *FetaProcess) {
|
||||
@ -21,18 +26,31 @@ func (a *FetaAPIServer) Serve() {
|
||||
if a.feta == nil {
|
||||
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 {
|
||||
a.port = uint(s)
|
||||
}
|
||||
}
|
||||
|
||||
a.initRouter()
|
||||
a.initServer()
|
||||
err := a.server.ListenAndServe()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal().Msg("webserver failure: " + err.Error())
|
||||
log.Fatal().Err(err).Msg("webserver failure")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (a *FetaAPIServer) getRouter() *gin.Engine {
|
||||
if os.Getenv("DEBUG") == "" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
func (a *FetaAPIServer) initRouter() {
|
||||
|
||||
// empty router
|
||||
r := gin.New()
|
||||
@ -48,25 +66,21 @@ func (a *FetaAPIServer) getRouter() *gin.Engine {
|
||||
r.GET("/feta", gin.WrapF(a.getIndexHandler()))
|
||||
r.GET("/feta/list/instances", gin.WrapF(a.getInstanceListHandler()))
|
||||
|
||||
return r
|
||||
a.router = r
|
||||
}
|
||||
|
||||
func (a *FetaAPIServer) getServer() *http.Server {
|
||||
r := a.getRouter()
|
||||
|
||||
port := "8080"
|
||||
if os.Getenv("PORT") != "" {
|
||||
port = os.Getenv("PORT")
|
||||
func (a *FetaAPIServer) initServer() {
|
||||
if !a.debug {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
|
||||
log.Info().Str("port", port).Msg("starting webserver")
|
||||
log.Info().Uint("port", a.port).Msg("starting webserver")
|
||||
|
||||
s := &http.Server{
|
||||
Addr: fmt.Sprintf(":%s", port),
|
||||
Handler: r,
|
||||
a.server = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", a.port),
|
||||
Handler: a.router,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import "github.com/rs/zerolog/log"
|
||||
const HOST_DISCOVERY_PARALLELISM = 1
|
||||
|
||||
type InstanceBackend interface {
|
||||
|
||||
//FIXME
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user