Check performs checks using c according to its configuration. An error is only returned if there is a configuration error.
()
| 87 | // Check performs checks using c according to its configuration. |
| 88 | // An error is only returned if there is a configuration error. |
| 89 | func (c Checker) Check() (types.Result, error) { |
| 90 | if c.Attempts < 1 { |
| 91 | c.Attempts = 1 |
| 92 | } |
| 93 | if c.Client == nil { |
| 94 | c.Client = DefaultHTTPClient |
| 95 | } |
| 96 | if c.UpStatus == 0 { |
| 97 | c.UpStatus = http.StatusOK |
| 98 | } |
| 99 | |
| 100 | result := types.NewResult() |
| 101 | result.Title = c.Name |
| 102 | result.Endpoint = c.URL |
| 103 | |
| 104 | req, err := http.NewRequest("GET", c.URL, nil) |
| 105 | if err != nil { |
| 106 | return result, err |
| 107 | } |
| 108 | |
| 109 | if c.Headers != nil { |
| 110 | for key, header := range c.Headers { |
| 111 | req.Header.Add(key, strings.Join(header, ", ")) |
| 112 | // net/http has special Host field which we'll fill out |
| 113 | if strings.ToLower(key) == "host" { |
| 114 | req.Host = header[0] |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | result.Times = c.doChecks(req) |
| 120 | |
| 121 | return c.conclude(result), nil |
| 122 | } |
| 123 | |
| 124 | // doChecks executes req using c.Client and returns each attempt. |
| 125 | func (c Checker) doChecks(req *http.Request) types.Attempts { |