2019-05-16 10:39:25 -04:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2019-08-06 12:29:00 -08:00
|
|
|
"os"
|
2019-05-16 10:39:25 -04:00
|
|
|
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
2019-05-23 14:32:24 -05:00
|
|
|
"github.com/jmoiron/sqlx"
|
2019-06-24 17:36:42 -08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/contrib/go-redis/redis"
|
2019-05-16 10:39:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Check provides support for orchestration health checks.
|
|
|
|
type Check struct {
|
2019-05-23 14:32:24 -05:00
|
|
|
MasterDB *sqlx.DB
|
2019-06-24 17:36:42 -08:00
|
|
|
Redis *redis.Client
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
// ADD OTHER STATE LIKE THE LOGGER IF NEEDED.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Health validates the service is healthy and ready to accept requests.
|
|
|
|
func (c *Check) Health(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
2019-06-24 17:36:42 -08:00
|
|
|
|
|
|
|
// check postgres
|
2019-05-23 14:32:24 -05:00
|
|
|
_, err := c.MasterDB.Exec("SELECT 1")
|
|
|
|
if err != nil {
|
2019-06-24 17:36:42 -08:00
|
|
|
return errors.Wrap(err, "Postgres failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check redis
|
2019-07-10 16:24:10 -08:00
|
|
|
err = c.Redis.Ping().Err()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Redis failed")
|
|
|
|
}
|
2019-05-16 10:39:25 -04:00
|
|
|
|
2019-08-06 12:29:00 -08:00
|
|
|
data := struct {
|
2019-05-16 10:39:25 -04:00
|
|
|
Status string `json:"status"`
|
2019-08-06 12:29:00 -08:00
|
|
|
CiCommitRefName string `json:"ci-commit-ref-name,omitempty"`
|
|
|
|
CiCommitRefSlug string `json:"ci-commit-ref-slug,omitempty"`
|
|
|
|
CiCommitSha string `json:"ci-commit-sha,omitempty"`
|
|
|
|
CiCommitTag string `json:"ci-commit-tag,omitempty"`
|
|
|
|
CiCommitTitle string `json:"ci-commit-title,omitempty"`
|
|
|
|
CiJobId string `json:"ci-commit-job-id,omitempty"`
|
|
|
|
CiPipelineId string `json:"ci-commit-pipeline-id,omitempty"`
|
2019-05-16 10:39:25 -04:00
|
|
|
}{
|
|
|
|
Status: "ok",
|
2019-08-06 12:29:00 -08:00
|
|
|
CiCommitRefName: os.Getenv("CI_COMMIT_REF_NAME"),
|
|
|
|
CiCommitRefSlug: os.Getenv("CI_COMMIT_REF_SLUG"),
|
|
|
|
CiCommitSha: os.Getenv("CI_COMMIT_SHA"),
|
|
|
|
CiCommitTag: os.Getenv("CI_COMMIT_TAG"),
|
|
|
|
CiCommitTitle: os.Getenv("CI_COMMIT_TITLE"),
|
|
|
|
CiJobId : os.Getenv("CI_COMMIT_JOB_ID"),
|
|
|
|
CiPipelineId: os.Getenv("CI_COMMIT_PIPELINE_ID"),
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2019-08-06 12:29:00 -08:00
|
|
|
return web.RespondJson(ctx, w, data, http.StatusOK)
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
2019-07-10 16:24:10 -08:00
|
|
|
|
|
|
|
// Ping validates the service is ready to accept requests.
|
|
|
|
func (c *Check) Ping(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
|
|
|
status := "pong"
|
|
|
|
|
2019-07-13 16:32:29 -08:00
|
|
|
return web.RespondText(ctx, w, status, http.StatusOK)
|
2019-07-10 16:24:10 -08:00
|
|
|
}
|