Skip to content

Commit 17a8152

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 17a8152

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

info.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func infoHandler(c *gin.Context) {
3131
c.JSON(200, gin.H{
3232
"version": version,
3333
"http": "http://" + host + port,
34-
"https": "https://localhost" + portSSL,
34+
"https": "https://localhost" + port,
3535
"ws": "ws://" + host + port,
36-
"wss": "wss://localhost" + portSSL,
36+
"wss": "wss://localhost" + port,
3737
"origins": origins,
3838
"update_url": updateURL,
3939
"os": runtime.GOOS + ":" + runtime.GOARCH,

main.go

+28-38
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ 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
51+
https = true
5252
requiredToolsAPILevel = "v1"
5353
)
5454

@@ -144,6 +144,9 @@ func main() {
144144
Hibernate: *hibernate,
145145
Version: version + "-" + commit,
146146
DebugURL: func() string {
147+
if https {
148+
return "https://" + *address + port
149+
}
147150
return "http://" + *address + port
148151
},
149152
AdditionalConfig: *additionalConfig,
@@ -196,7 +199,7 @@ func loop() {
196199
log.SetLevel(log.InfoLevel)
197200
log.SetOutput(os.Stdout)
198201

199-
// the important forlders of the agent
202+
// the important folders of the agent
200203
src, _ := os.Executable()
201204
srcPath := paths.New(src) // The path of the agent's binary
202205
srcDir := srcPath.Parent() // The directory of the agent's binary
@@ -420,45 +423,32 @@ func loop() {
420423
goa := v2.Server(agentDir.String())
421424
r.Any("/v2/*path", gin.WrapH(goa))
422425

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

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

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

0 commit comments

Comments
 (0)