⚠ Migrate TwinProduction/gatus to TwiN/gatus
This commit is contained in:
1
vendor/github.com/TwiN/health/.gitattributes
generated
vendored
Normal file
1
vendor/github.com/TwiN/health/.gitattributes
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
2
vendor/github.com/TwiN/health/.gitignore
generated
vendored
Normal file
2
vendor/github.com/TwiN/health/.gitignore
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.idea
|
||||
*.iml
|
9
vendor/github.com/TwiN/health/LICENSE.md
generated
vendored
Normal file
9
vendor/github.com/TwiN/health/LICENSE.md
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 TwiN
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
70
vendor/github.com/TwiN/health/README.md
generated
vendored
Normal file
70
vendor/github.com/TwiN/health/README.md
generated
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
# health
|
||||

|
||||
[](https://goreportcard.com/report/github.com/TwiN/health)
|
||||
[](https://codecov.io/gh/TwiN/health)
|
||||
[](https://github.com/TwiN/health)
|
||||
[](https://pkg.go.dev/github.com/TwiN/health)
|
||||
|
||||
Health is a library used for creating a very simple health endpoint.
|
||||
|
||||
While implementing a health endpoint is very simple, I've grown tired of implementing
|
||||
it over and over again.
|
||||
|
||||
|
||||
## Installation
|
||||
```
|
||||
go get -u github.com/TwiN/health
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
To retrieve the handler, you must use `health.Handler()` and are expected to pass it to the router like so:
|
||||
```go
|
||||
router := http.NewServeMux()
|
||||
router.Handle("/health", health.Handler())
|
||||
server := &http.Server{
|
||||
Addr: ":8080",
|
||||
Handler: router,
|
||||
}
|
||||
```
|
||||
|
||||
By default, the handler will return `UP` when the status is down, and `DOWN` when the status is down.
|
||||
If you prefer using JSON, however, you may initialize the health handler like so:
|
||||
```go
|
||||
router.Handle("/health", health.Handler().WithJSON(true))
|
||||
```
|
||||
The above will cause the response body to become `{"status":"UP"}` and `{"status":"DOWN"}` for both status respectively.
|
||||
|
||||
To change the health of the application, you can use `health.SetStatus(<status>)` where `<status>` is one `health.Up`
|
||||
or `health.Down`:
|
||||
|
||||
```go
|
||||
health.SetStatus(health.Up)
|
||||
health.SetStatus(health.Down)
|
||||
```
|
||||
|
||||
|
||||
### Complete example
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/TwiN/health"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := http.NewServeMux()
|
||||
router.Handle("/health", health.Handler())
|
||||
server := &http.Server{
|
||||
Addr: "0.0.0.0:8080",
|
||||
Handler: router,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
IdleTimeout: 15 * time.Second,
|
||||
}
|
||||
server.ListenAndServe()
|
||||
}
|
||||
```
|
53
vendor/github.com/TwiN/health/health.go
generated
vendored
Normal file
53
vendor/github.com/TwiN/health/health.go
generated
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
package health
|
||||
|
||||
import "net/http"
|
||||
|
||||
var (
|
||||
handler = &healthHandler{
|
||||
useJSON: false,
|
||||
status: Up,
|
||||
}
|
||||
)
|
||||
|
||||
// healthHandler is the HTTP handler for serving the health endpoint
|
||||
type healthHandler struct {
|
||||
useJSON bool
|
||||
status Status
|
||||
}
|
||||
|
||||
// WithJSON configures whether the handler should output a response in JSON or in raw text
|
||||
//
|
||||
// Defaults to false
|
||||
func (h *healthHandler) WithJSON(v bool) *healthHandler {
|
||||
h.useJSON = v
|
||||
return h
|
||||
}
|
||||
|
||||
// ServeHTTP serves the HTTP request for the health handler
|
||||
func (h healthHandler) ServeHTTP(writer http.ResponseWriter, _ *http.Request) {
|
||||
var status int
|
||||
var body []byte
|
||||
if h.status == Up {
|
||||
status = http.StatusOK
|
||||
} else {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
if h.useJSON {
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
body = []byte(`{"status":"` + h.status + `"}`)
|
||||
} else {
|
||||
body = []byte(h.status)
|
||||
}
|
||||
writer.WriteHeader(status)
|
||||
_, _ = writer.Write(body)
|
||||
}
|
||||
|
||||
// Handler retrieves the health handler
|
||||
func Handler() *healthHandler {
|
||||
return handler
|
||||
}
|
||||
|
||||
// SetStatus sets the status to be reflected by the health handler
|
||||
func SetStatus(status Status) {
|
||||
handler.status = status
|
||||
}
|
8
vendor/github.com/TwiN/health/status.go
generated
vendored
Normal file
8
vendor/github.com/TwiN/health/status.go
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
package health
|
||||
|
||||
type Status string
|
||||
|
||||
var (
|
||||
Down Status = "DOWN" // For when the application is unhealthy
|
||||
Up Status = "UP" // For when the application is healthy
|
||||
)
|
Reference in New Issue
Block a user