* 'master' of https://github.com/usefathom/fathom:
  Adding /health endpoint and using it in container
This commit is contained in:
Danny van Kooten 2018-09-18 12:03:49 +02:00
commit ad80a83f9d
5 changed files with 28 additions and 0 deletions

View File

@ -13,6 +13,7 @@ RUN go get -u github.com/gobuffalo/packr/... && make docker
FROM alpine:latest FROM alpine:latest
EXPOSE 8080 EXPOSE 8080
HEALTHCHECK --retries=10 CMD ["wget", "-qO-", "http://localhost:8080/health"]
RUN apk add --update --no-cache bash ca-certificates RUN apk add --update --no-cache bash ca-certificates
WORKDIR /app WORKDIR /app
COPY --from=binarybuilder /go/src/github.com/usefathom/fathom/fathom . COPY --from=binarybuilder /go/src/github.com/usefathom/fathom/fathom .

14
pkg/api/health.go Normal file
View File

@ -0,0 +1,14 @@
package api
import "net/http"
// GET /health
func (api *API) Health(w http.ResponseWriter, _ *http.Request) error {
if err := api.database.Health(); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return err
}
w.WriteHeader(http.StatusOK)
return nil
}

View File

@ -30,6 +30,8 @@ func (api *API) Routes() *mux.Router {
r.Handle("/api/stats/referrers", api.Authorize(HandlerFunc(api.GetReferrerStatsHandler))).Methods(http.MethodGet) r.Handle("/api/stats/referrers", api.Authorize(HandlerFunc(api.GetReferrerStatsHandler))).Methods(http.MethodGet)
r.Handle("/api/stats/referrers/pageviews", api.Authorize(HandlerFunc(api.GetReferrerStatsPageviewsHandler))).Methods(http.MethodGet) r.Handle("/api/stats/referrers/pageviews", api.Authorize(HandlerFunc(api.GetReferrerStatsPageviewsHandler))).Methods(http.MethodGet)
r.Handle("/health", HandlerFunc(api.Health)).Methods(http.MethodGet)
// static assets & 404 handler // static assets & 404 handler
box := packr.NewBox("./../../assets/build") box := packr.NewBox("./../../assets/build")
r.Path("/tracker.js").Handler(serveTrackerFile(&box)) r.Path("/tracker.js").Handler(serveTrackerFile(&box))

View File

@ -53,6 +53,7 @@ type Datastore interface {
GetAggregatedReferrerStatsPageviews(time.Time, time.Time) (int, error) GetAggregatedReferrerStatsPageviews(time.Time, time.Time) (int, error)
// misc // misc
Health() error
Close() error Close() error
} }

View File

@ -1,7 +1,9 @@
package sqlstore package sqlstore
import ( import (
"context"
"errors" "errors"
"time"
_ "github.com/go-sql-driver/mysql" // mysql driver _ "github.com/go-sql-driver/mysql" // mysql driver
"github.com/gobuffalo/packr" "github.com/gobuffalo/packr"
@ -64,6 +66,14 @@ func (db *sqlstore) Migrate() {
} }
} }
// Health check health of database
func (db *sqlstore) Health() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
return db.PingContext(ctx)
}
// Closes the db pool // Closes the db pool
func (db *sqlstore) Close() error { func (db *sqlstore) Close() error {
return db.DB.Close() return db.DB.Close()