Skip to content

Commit

Permalink
add health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
blesswinsamuel committed Sep 19, 2023
1 parent 58d6bb3 commit 7ea7849
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/ldap-sql-adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
defer ldapserver.Stop()

// Build handler
srv := server.NewServer(config, log)
srv := server.NewServer(config, log, provider)

// Start
go srv.Start()
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (

// Provider is used to authenticate users
type Provider interface {
Ping(ctx context.Context) error
FindUserPasswordByUsername(ctx context.Context, username string) ([]byte, error)
FindUserByUsernameOrEmail(ctx context.Context, username string, email string) (User, error)
FindUserGroups(ctx context.Context, username string) ([]Group, error)
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func NewSQLProvider(config SQLProviderConfig) (*SQLProvider, error) {
return &SQLProvider{db: db, config: config}, nil
}

func (p *SQLProvider) Ping(ctx context.Context) (err error) {
defer logMetric("Ping")(err)
return p.db.PingContext(ctx)
}

func (p *SQLProvider) FindUserPasswordByUsername(ctx context.Context, uid string) (passwordBytes []byte, err error) {
defer logMetric("FindUserPasswordByUsername")(err)
rows, err := p.db.NamedQueryContext(ctx, p.config.SQLGetUserPasswordByUsernameQuery, map[string]any{"uid": uid})
Expand Down
25 changes: 18 additions & 7 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ import (
"github.com/rs/zerolog/log"

"github.com/blesswinsamuel/ldap-sql-proxy/internal/config"
"github.com/blesswinsamuel/ldap-sql-proxy/internal/provider"
)

// Server contains router and handler methods
type Server struct {
router *mux.Router
logger zerolog.Logger
config *config.Config
router *mux.Router
provider provider.Provider
logger zerolog.Logger
config *config.Config
}

// NewServer creates a new server object and builds router
func NewServer(cfg *config.Config, logger zerolog.Logger) *Server {
func NewServer(cfg *config.Config, logger zerolog.Logger, provider provider.Provider) *Server {
s := &Server{
router: mux.NewRouter(),
logger: logger,
config: cfg,
router: mux.NewRouter(),
logger: logger,
provider: provider,
config: cfg,
}

s.buildRoutes()
Expand Down Expand Up @@ -69,6 +72,14 @@ func (s *Server) buildRoutes() {
s.router.Use(s.loggerMiddleware)

s.router.Handle("/metrics", promhttp.Handler())
s.router.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
err := s.provider.Ping(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
})
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 7ea7849

Please sign in to comment.