-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTTP health check handler for server health monitoring (#952)
Added the handler to allow health checks to be performed with plain HTTP GET requests needed for traditional uptime checker or load balancer, along with existing gRPC health check.
- Loading branch information
1 parent
e1fbcf3
commit 1c88649
Showing
3 changed files
with
164 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2024 The Yorkie Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Package httphealth uses http GET to provide a health check for the server. | ||
package httphealth | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"connectrpc.com/grpchealth" | ||
) | ||
|
||
// serviceName is the path for the health check endpoint. | ||
const serviceName = "/healthz/" | ||
|
||
// CheckResponse represents the response structure for health checks. | ||
type CheckResponse struct { | ||
Status string `json:"status"` | ||
} | ||
|
||
// NewHandler creates a new HTTP handler for health checks. | ||
func NewHandler(checker grpchealth.Checker) (string, http.Handler) { | ||
check := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodGet { | ||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
var checkRequest grpchealth.CheckRequest | ||
if service := r.URL.Query().Get("service"); service != "" { | ||
checkRequest.Service = service | ||
} | ||
|
||
checkResponse, err := checker.Check(r.Context(), &checkRequest) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusNotFound) | ||
return | ||
} | ||
|
||
resp, err := json.Marshal(CheckResponse{checkResponse.Status.String()}) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
|
||
if _, err := w.Write(resp); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
}) | ||
|
||
return serviceName, check | ||
} |
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