-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from jelmersnoeck/add-healthz-check
Add health endpoint.
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package httpsvc | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// Health represents a Health server which can be used to expose a health check. | ||
type Health struct { | ||
Server | ||
} | ||
|
||
// Start starts the Health Server. | ||
func (s *Health) Start(stopCh <-chan struct{}) error { | ||
registerHealthCheck(&s.ServeMux) | ||
|
||
return s.Server.Start(stopCh) | ||
} | ||
|
||
func registerHealthCheck(mux *http.ServeMux) { | ||
mux.HandleFunc("/_healthz", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintln(w, "OK") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package httpsvc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// Server is a wrapper around a http.ServeMux which sets up necessary endpoints | ||
// to run our operator. | ||
type Server struct { | ||
http.ServeMux | ||
|
||
Addr string | ||
Port int | ||
} | ||
|
||
// Start starts the HTTP Server. | ||
func (s *Server) Start(stopCh <-chan struct{}) error { | ||
srv := http.Server{ | ||
Addr: fmt.Sprintf("%s:%d", s.Addr, s.Port), | ||
Handler: &s.ServeMux, | ||
ReadTimeout: 10 * time.Second, | ||
WriteTimeout: 10 * time.Second, | ||
} | ||
|
||
go func() { | ||
<-stopCh | ||
|
||
ctx := context.Background() | ||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
defer cancel() | ||
srv.Shutdown(ctx) | ||
}() | ||
|
||
return srv.ListenAndServe() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters