Last active
December 29, 2020 17:52
-
-
Save nkreiger/fe43708b8f8ff50d9c816dc51b70a36f to your computer and use it in GitHub Desktop.
mongo health endpoint
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
// Connection tests the mongodb connection health | |
var Health = func(w http.ResponseWriter, r *http.Request) { | |
(w).Header().Set("Content-Type", "application/json") | |
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | |
defer cancel() | |
client, err := mongo.Connect(ctx) | |
if err != nil { | |
writeStatus(&w, http.StatusBadRequest, err.Error()) | |
return | |
} | |
defer func() { | |
if err := client.Disconnect(ctx); err != nil { | |
log.Printf("error disconnecting client: %v", err) | |
} | |
cancel() | |
}() | |
// write back okay for good connection | |
writeStatus(&w, http.StatusOK, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment