* Add health check endpoint (#19) * Add healthy response (#19)
This commit is contained in:
parent
0e16eee93b
commit
2d3e3cbae1
|
@ -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"
|
||||||
|
|
|
@ -55,3 +55,5 @@ 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
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/gobuffalo/flect"
|
"github.com/gobuffalo/flect"
|
||||||
|
@ -67,6 +68,7 @@ type config struct {
|
||||||
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
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue