Files
helm-dashboard/pkg/dashboard/server.go
Andrey Pokhilko 967d499742 Initial features 4 (#5)
* Select works fine

* Context switching works

* Going to expand fns

* Diff and value

* Progressing

* fix click events

* highlight code

* Add revision age info

* Values diff

* remove forgotten

* Refactor data layer, add flag

* UDV flag works

* Diff with prev works
2022-09-05 17:31:25 +01:00

59 lines
1.3 KiB
Go

package dashboard
import (
"context"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net/http"
"os"
)
func StartServer() (string, ControlChan) {
data := DataLayer{}
err := data.CheckConnectivity()
if err != nil {
log.Errorf("Failed to check that Helm is operational, cannot continue. The error was: %s", err)
os.Exit(1) // TODO: propagate error instead?
}
address := os.Getenv("HD_BIND")
if address == "" {
address = "localhost"
}
if os.Getenv("HD_PORT") == "" {
address += ":8080" // TODO: better default port to clash less?
} else {
address += ":" + os.Getenv("HD_PORT")
}
abort := make(ControlChan)
api := NewRouter(abort, &data)
done := startBackgroundServer(address, api, abort)
return "http://" + address, done
}
func startBackgroundServer(addr string, routes *gin.Engine, abort ControlChan) ControlChan {
done := make(ControlChan)
server := &http.Server{Addr: addr, Handler: routes}
go func() {
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
panic(err) // TODO: in case of "port busy", check that it's another instance of us and just open browser
}
done <- struct{}{}
}()
go func() {
<-abort
err := server.Shutdown(context.Background())
if err != nil {
log.Warnf("Had problems shutting down the server: %s", err)
}
}()
return done
}