func (s *ApiServer) Start(ctx context.Context) error {
	log := log.FromContext(ctx)
	addr := os.Getenv("API_LISTEN")
	if addr == "" {
		addr = fmt.Sprintf(":%d", listeningPort)
	}
	mux := http.NewServeMux()
	mux.Handle("/api/v1/allocate", &allocateHandler{
		client: s.client,
		config: s.config,
		scheme: s.scheme,
	})
	log.Info("serving API server", "addr", addr, "port", listeningPort)
	srv := &http.Server{
		Addr:    addr,
		Handler: mux,
	}
	done := make(chan struct{})
	go func() {
		<-ctx.Done()
		log.Info("shutting down API server")
		if err := srv.Shutdown(context.Background()); err != nil {
			log.Error(err, "error shutting down the HTTP server")
		}
		close(done)
	}()
	if crtBytes != nil && keyBytes != nil {
		log.Info("starting TLS enabled API server")
		if err := customListenAndServeTLS(srv, crtBytes, keyBytes); err != nil && err != http.ErrServerClosed {
			return err
		}
	} else {
		log.Info("starting insecure API server")
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			return err
		}
	}
	<-done
	return nil
}
Start starts the HTTP(S) API Server
if user has provided public/private cert details, it will create a TLS-auth HTTPS server
otherwise it will create a HTTP server with no auth
package http
import (
	"context"
	"crypto/tls"
	"fmt"
	"net"
	"net/http"
	"os"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/rest"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/log"
	mpsv1alpha1 "github.com/playfab/thundernetes/operator/api/v1alpha1"
)
var (
	crtBytes []byte
	keyBytes []byte
)
const (
	listeningPort = 5000
)
type ApiServer struct {
	client client.Client
	config *rest.Config
	scheme *runtime.Scheme
}
func NewApiServer(mgr ctrl.Manager, crt, key []byte) error {
	crtBytes = crt
	keyBytes = key
	server := &ApiServer{client: mgr.GetClient(), config: mgr.GetConfig(), scheme: mgr.GetScheme()}
	if err := server.setupIndexers(mgr); err != nil {
		return err
	}
	return mgr.Add(server)
}
func (s *ApiServer) setupIndexers(mgr ctrl.Manager) error {
	if err := mgr.GetFieldIndexer().IndexField(context.Background(), &mpsv1alpha1.GameServer{}, "status.state", func(rawObj client.Object) []string {
		gs := rawObj.(*mpsv1alpha1.GameServer)
		return []string{string(gs.Status.State)}
	}); err != nil {
		return err
	}
	if err := mgr.GetFieldIndexer().IndexField(context.Background(), &mpsv1alpha1.GameServer{}, "status.sessionID", func(rawObj client.Object) []string {
		gs := rawObj.(*mpsv1alpha1.GameServer)
		return []string{gs.Status.SessionID}
	}); err != nil {
		return err
	}
	if err := mgr.GetFieldIndexer().IndexField(context.Background(), &mpsv1alpha1.GameServerBuild{}, "spec.buildID", func(rawObj client.Object) []string {
		gsb := rawObj.(*mpsv1alpha1.GameServerBuild)
		return []string{gsb.Spec.BuildID}
	}); err != nil {
		return err
	}
	return nil
}
func (s *ApiServer) NeedLeaderElection() bool {
	return false
}