Enabled Routes() to accept a prefix

This commit is contained in:
Arnaud (Arhuman) ASSAD 2024-05-10 08:25:34 +02:00
parent 19d9ab1391
commit 92b101dec4
1 changed files with 13 additions and 3 deletions

View File

@ -7,9 +7,19 @@ import (
) )
// Routes sets up the routes for the server. // Routes sets up the routes for the server.
func (s *Server) Routes() { func (s *Server) Routes(prefix string) {
s.Log.Info("Health check handler installed") if prefix != "" {
s.AddRoute("GET /health", func(w http.ResponseWriter, r *http.Request) { // Ensure the prefix starts with a slash
if prefix[0] != '/' {
prefix = "/" + prefix
}
// Ensure the prefix starts does not end with a slash
if prefix[len(prefix)-1] == '/' {
prefix = prefix[:len(prefix)-1]
}
}
s.Log.Info(fmt.Sprintf("Health check handler installed: %s/health", prefix))
s.AddRoute("GET $prefix/health", func(w http.ResponseWriter, r *http.Request) {
s.Log.Debug("Health check handler called") s.Log.Debug("Health check handler called")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Hello, World!") fmt.Fprintf(w, "Hello, World!")