Skip to content

Instantly share code, notes, and snippets.

@nkreiger
Last active December 29, 2020 17:52
Show Gist options
  • Save nkreiger/fe43708b8f8ff50d9c816dc51b70a36f to your computer and use it in GitHub Desktop.
Save nkreiger/fe43708b8f8ff50d9c816dc51b70a36f to your computer and use it in GitHub Desktop.
mongo health endpoint
// 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