Add health check at /status

There are times where it would be good to monitor that osbuild-composer
is up and running. Add a very simple status check that always returns
200/OK. This can be expanded later to verify that other parts of
osbuild-composer are working properly.

Signed-off-by: Major Hayden <major@redhat.com>
This commit is contained in:
Major Hayden 2020-05-26 14:39:43 -05:00 committed by Major Hayden
parent e503b0a4d4
commit 0921643fa3
3 changed files with 23 additions and 0 deletions

View file

@ -25,6 +25,10 @@ type OSBuildJobResult struct {
// JSON-serializable types for the HTTP API
//
type statusResponse struct {
Status string `json:"status"`
}
type errorResponse struct {
Message string `json:"message"`
}

View file

@ -49,6 +49,10 @@ func NewServer(logger *log.Logger, jobs jobqueue.JobQueue, artifactsDir string)
s.router.MethodNotAllowed = http.HandlerFunc(methodNotAllowedHandler)
s.router.NotFound = http.HandlerFunc(notFoundHandler)
// Add a basic status handler for checking if osbuild-composer is alive.
s.router.GET("/status", s.statusHandler)
// Add handlers for managing jobs.
s.router.POST("/job-queue/v1/jobs", s.addJobHandler)
s.router.PATCH("/job-queue/v1/jobs/:job_id", s.updateJobHandler)
s.router.POST("/job-queue/v1/jobs/:job_id/artifacts/:name", s.addJobImageHandler)
@ -134,6 +138,15 @@ func notFoundHandler(writer http.ResponseWriter, request *http.Request) {
jsonErrorf(writer, http.StatusNotFound, "not found")
}
func (s *Server) statusHandler(writer http.ResponseWriter, request *http.Request, _ httprouter.Params) {
writer.WriteHeader(http.StatusOK)
// Send back a status message.
_ = json.NewEncoder(writer).Encode(&statusResponse{
Status: "OK",
})
}
func (s *Server) addJobHandler(writer http.ResponseWriter, request *http.Request, _ httprouter.Params) {
contentType := request.Header["Content-Type"]
if len(contentType) != 1 || contentType[0] != "application/json" {

View file

@ -14,6 +14,12 @@ import (
"github.com/osbuild/osbuild-composer/internal/worker"
)
// Ensure that the status request returns OK.
func TestStatus(t *testing.T) {
server := worker.NewServer(nil, testjobqueue.New(), "")
test.TestRoute(t, server, false, "GET", "/status", ``, http.StatusOK, `{"status":"OK"}`, "message")
}
func TestErrors(t *testing.T) {
var cases = []struct {
Method string