mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 03:20:27 +00:00
Adding /health endpoint and using it in container
This commit is contained in:
parent
fc2e9d2fd1
commit
e712f0fcdd
@ -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
14
pkg/api/health.go
Normal 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
|
||||||
|
}
|
@ -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))
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user