Skip to content

Commit 61b004b

Browse files
committed
now the HTTP server is not started if HTTPS is used. Abstract a little the implementation, and remove useless print since r.Run is blocking
1 parent 1165c4f commit 61b004b

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

main.go

+25-37
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func loop() {
196196
log.SetLevel(log.InfoLevel)
197197
log.SetOutput(os.Stdout)
198198

199-
// the important forlders of the agent
199+
// the important folders of the agent
200200
src, _ := os.Executable()
201201
srcPath := paths.New(src) // The path of the agent's binary
202202
srcDir := srcPath.Parent() // The directory of the agent's binary
@@ -420,45 +420,33 @@ func loop() {
420420
goa := v2.Server(agentDir.String())
421421
r.Any("/v2/*path", gin.WrapH(goa))
422422

423-
go func() {
424-
// check if certificates exist; if not, use plain http
425-
if srcDir.Join("cert.pem").NotExist() {
426-
log.Error("Could not find HTTPS certificate. Using plain HTTP only.")
427-
return
428-
}
423+
// check if certificates exist; if not, use plain http
424+
https := true
425+
if srcDir.Join("cert.pem").NotExist() {
426+
log.Error("Could not find HTTPS certificate. Using plain HTTP only.")
427+
https = false
428+
}
429+
go startServer(r, https, srcDir)
430+
}
429431

430-
start := 8990
431-
end := 9000
432-
i := start
433-
for i < end {
434-
i = i + 1
435-
portSSL = ":" + strconv.Itoa(i)
436-
if err := r.RunTLS(*address+portSSL, srcDir.Join("cert.pem").String(), srcDir.Join("key.pem").String()); err != nil {
437-
log.Printf("Error trying to bind to port: %v, so exiting...", err)
438-
continue
439-
} else {
440-
log.Print("Starting server and websocket (SSL) on " + *address + "" + port)
441-
break
442-
}
432+
func startServer(r *gin.Engine, https bool, certDir *paths.Path) {
433+
start := 8990
434+
end := 9000
435+
i := start
436+
for i < end {
437+
i = i + 1
438+
port = ":" + strconv.Itoa(i)
439+
var err error
440+
if https {
441+
err = r.RunTLS(*address+port, certDir.Join("cert.pem").String(), certDir.Join("key.pem").String())
442+
} else {
443+
err = r.Run(*address + port)
443444
}
444-
}()
445-
446-
go func() {
447-
start := 8990
448-
end := 9000
449-
i := start
450-
for i < end {
451-
i = i + 1
452-
port = ":" + strconv.Itoa(i)
453-
if err := r.Run(*address + port); err != nil {
454-
log.Printf("Error trying to bind to port: %v, so exiting...", err)
455-
continue
456-
} else {
457-
log.Print("Starting server and websocket on " + *address + "" + port)
458-
break
459-
}
445+
if err != nil {
446+
log.Printf("Error trying to bind to port: %v, so exiting...", err)
447+
continue
460448
}
461-
}()
449+
}
462450
}
463451

464452
func parseIni(filename string) (args []string, err error) {

0 commit comments

Comments
 (0)