From 5f92240466ccca3731aa12d619aec73b1b9b294e Mon Sep 17 00:00:00 2001
From: Fabian Seidl <fabian.seidl@h-da.de>
Date: Wed, 17 Apr 2024 11:46:41 +0200
Subject: [PATCH] rename run and change where http listen and serve gets
 called, pass through error

---
 controller/controller.go |  7 +++++--
 controller/http.go       | 21 +++++++++++----------
 controller/http_test.go  |  9 ++++++---
 3 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/controller/controller.go b/controller/controller.go
index 7a1adbc81..d0d6ceee1 100644
--- a/controller/controller.go
+++ b/controller/controller.go
@@ -173,8 +173,11 @@ func initialize() error {
 	}
 
 	coreLock.Lock()
-	startHttpServer()
-	coreLock.Unlock()
+	defer coreLock.Unlock()
+	err = startHttpServer()
+	if err != nil {
+		return err
+	}
 
 	return nil
 }
diff --git a/controller/http.go b/controller/http.go
index 1261577ac..9270eebce 100644
--- a/controller/http.go
+++ b/controller/http.go
@@ -35,7 +35,7 @@ func stopHttpServer() error {
 	return err
 }
 
-func run() error {
+func setupHttpServer() error {
 	ctx := context.Background()
 	ctx, cancel := context.WithCancel(ctx)
 	defer cancel()
@@ -47,7 +47,6 @@ func run() error {
 	)
 
 	err := registerHttpHandler(mux)
-
 	if err != nil {
 		return err
 	}
@@ -90,18 +89,20 @@ func run() error {
 
 	// Set the HTTP server of core to the new server
 	c.httpServer = &http.Server{Addr: ":8080", Handler: mux}
-	// Start HTTP server (and proxy calls to gRPC server endpoint)
-	return c.httpServer.ListenAndServe()
+	return nil
 }
 
-func startHttpServer() {
-	go func() {
-		if err := run(); err != nil {
-			log.Info(err)
-		}
-	}()
+func startHttpServer() error {
+	if err := setupHttpServer(); err != nil {
+		log.Info(err)
+		return err
+	}
+
+	// Start HTTP server (and proxy calls to gRPC server endpoint)
+	go c.httpServer.ListenAndServe() //nolint:errcheck
 
 	log.Info("Server exiting")
+	return nil
 }
 
 func registerHttpHandler(mux *runtime.ServeMux) error {
diff --git a/controller/http_test.go b/controller/http_test.go
index 936c5d09f..6d2455936 100644
--- a/controller/http_test.go
+++ b/controller/http_test.go
@@ -6,6 +6,8 @@ import (
 	"net/http"
 	"testing"
 	"time"
+
+	"github.com/stretchr/testify/assert"
 )
 
 func Test_httpApi(t *testing.T) {
@@ -36,10 +38,11 @@ func Test_httpApi(t *testing.T) {
 	}
 
 	coreLock.Lock()
-	startHttpServer()
-	coreLock.Unlock()
+	defer coreLock.Unlock()
+	err := startHttpServer()
+	assert.NoError(t, err)
 
-	err := waitForHTTPServer()
+	err = waitForHTTPServer()
 	if err != nil {
 		t.Errorf("httpApi() error = %v", err)
 		return
-- 
GitLab