Skip to content

Commit 8016a01

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 8016a01

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

main.go

+25-38
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ var (
4848
version = "x.x.x-dev" //don't modify it, Jenkins will take care
4949
commit = "xxxxxxxx" //don't modify it, Jenkins will take care
5050
port string
51-
portSSL string
5251
requiredToolsAPILevel = "v1"
5352
)
5453

@@ -196,7 +195,7 @@ func loop() {
196195
log.SetLevel(log.InfoLevel)
197196
log.SetOutput(os.Stdout)
198197

199-
// the important forlders of the agent
198+
// the important folders of the agent
200199
src, _ := os.Executable()
201200
srcPath := paths.New(src) // The path of the agent's binary
202201
srcDir := srcPath.Parent() // The directory of the agent's binary
@@ -420,45 +419,33 @@ func loop() {
420419
goa := v2.Server(agentDir.String())
421420
r.Any("/v2/*path", gin.WrapH(goa))
422421

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-
}
422+
// check if certificates exist; if not, use plain http
423+
https := true
424+
if srcDir.Join("cert.pem").NotExist() {
425+
log.Error("Could not find HTTPS certificate. Using plain HTTP only.")
426+
https = false
427+
}
428+
go startServer(r, https, srcDir)
429+
}
429430

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-
}
431+
func startServer(r *gin.Engine, https bool, certDir *paths.Path) {
432+
start := 8990
433+
end := 9000
434+
i := start
435+
for i < end {
436+
i = i + 1
437+
port = ":" + strconv.Itoa(i)
438+
var err error
439+
if https {
440+
err = r.RunTLS(*address+port, certDir.Join("cert.pem").String(), certDir.Join("key.pem").String())
441+
} else {
442+
err = r.Run(*address + port)
443443
}
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-
}
444+
if err != nil {
445+
log.Printf("Error trying to bind to port: %v, so exiting...", err)
446+
continue
460447
}
461-
}()
448+
}
462449
}
463450

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

0 commit comments

Comments
 (0)