GO·NOTE

一份 Go 开发工程师的学习笔记

0%

http服务启动检车 check.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package check

import (
"errors"
"log"
"net/http"
"time"
)

func ApiCheck() {
if err := PingServer(); err != nil {
log.Fatal("The router has no response, or it might took too long to start up.")
}
log.Println("The router has been deployed successfully.")
}

func PingServer() error {
for i := 0; i < 10; i++ {
URL := "http://localhost:8080"
resp, err := http.Get(URL + "/health")
if err == nil && resp.StatusCode == 200 {
return nil
}
time.Sleep(time.Second)
}
return errors.New("cannot connect to the router")
}