#19 Health check (#24)

* Add health check endpoint (#19)

* Add healthy response (#19)
This commit is contained in:
Lev Zakharov 2019-12-09 09:59:30 +03:00 committed by Vikram Rangnekar
parent 0e16eee93b
commit 2d3e3cbae1
5 changed files with 49 additions and 11 deletions

View File

@ -97,6 +97,8 @@ database:
# Enable this if you need the user id in triggers, etc # Enable this if you need the user id in triggers, etc
set_user_id: false set_user_id: false
ping_timeout: 1m
# Define additional variables here to be used with filters # Define additional variables here to be used with filters
variables: variables:
admin_account_id: "5" admin_account_id: "5"

View File

@ -54,4 +54,6 @@ database:
# Set session variable "user.id" to the user id # Set session variable "user.id" to the user id
# Enable this if you need the user id in triggers, etc # Enable this if you need the user id in triggers, etc
set_user_id: false set_user_id: false
ping_timeout: 5m

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"regexp" "regexp"
"strings" "strings"
"time"
"unicode" "unicode"
"github.com/gobuffalo/flect" "github.com/gobuffalo/flect"
@ -57,16 +58,17 @@ type config struct {
} }
DB struct { DB struct {
Type string Type string
Host string Host string
Port uint16 Port uint16
DBName string DBName string
User string User string
Password string Password string
Schema string Schema string
PoolSize int32 `mapstructure:"pool_size"` PoolSize int32 `mapstructure:"pool_size"`
MaxRetries int `mapstructure:"max_retries"` MaxRetries int `mapstructure:"max_retries"`
SetUserID bool `mapstructure:"set_user_id"` SetUserID bool `mapstructure:"set_user_id"`
PingTimeout time.Duration `mapstructure:"ping_timeout"`
Vars map[string]string `mapstructure:"variables"` Vars map[string]string `mapstructure:"variables"`
Blocklist []string Blocklist []string

30
serv/health.go Normal file
View File

@ -0,0 +1,30 @@
package serv
import (
"context"
"net/http"
)
var healthyResponse = []byte("All's Well")
func health(w http.ResponseWriter, _ *http.Request) {
conn, err := db.Acquire(context.Background())
if err != nil {
errlog.Error().Err(err).Msg("error acquiring connection from pool")
w.WriteHeader(http.StatusInternalServerError)
return
}
ctx, _ := context.WithTimeout(context.Background(), conf.DB.PingTimeout)
if err := conn.Conn().Ping(ctx); err != nil {
errlog.Error().Err(err).Msg("error pinging database")
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write(healthyResponse); err != nil {
errlog.Error().Err(err).Msg("error writing healthy response")
w.WriteHeader(http.StatusInternalServerError)
return
}
}

View File

@ -123,6 +123,8 @@ func startHTTP() {
func routeHandler() http.Handler { func routeHandler() http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("/health", http.HandlerFunc(health))
mux.Handle("/api/v1/graphql", withAuth(apiv1Http)) mux.Handle("/api/v1/graphql", withAuth(apiv1Http))
if conf.WebUI { if conf.WebUI {