diff --git a/ldap.go b/ldap.go index 61c07cf..44cac5e 100644 --- a/ldap.go +++ b/ldap.go @@ -1,11 +1,13 @@ package main import ( - "crypto/tls" "fmt" + "strconv" + "time" + + "crypto/tls" log "github.com/sirupsen/logrus" "gopkg.in/ldap.v2" - "strconv" ) type LDAPConnection struct { @@ -33,15 +35,40 @@ func (c *LDAPConnection) Init( func (c *LDAPConnection) reconnectToLDAP() { lcon, err := ldap.DialTLS("tcp", c.host, &tls.Config{ServerName: "ldap.csh.rit.edu"}) - if err != nil { - c.app.db.AddLog(0, "ldap connection error: "+err.Error()) - log.Fatal(err) + + maxReconnectionAttempts := 3 // TODO (wilnil): Use an environment variable? + attempts := 0 + + for err != nil { + if attempts < maxReconnectionAttempts { + c.app.db.AddLog(0, "ldap connection error: "+err.Error()) + log.Info(err) + time.Sleep(2 * time.Second) + lcon, err = ldap.DialTLS("tcp", c.host, + &tls.Config{ServerName: "ldap.csh.rit.edu"}) + } else { + c.app.db.AddLog(0, "ldap connection error: "+err.Error()) + log.Fatal(err) + } + maxReconnectionAttempts++ } + err = lcon.Bind(c.bind_dn, c.bind_pw) - if err != nil { - c.app.db.AddLog(0, "ldap bind error: "+err.Error()) - log.Fatal(err) + + attempts = 0 + for err != nil { + if attempts < maxReconnectionAttempts { + c.app.db.AddLog(0, "ldap bind error: "+err.Error()) + log.Info(err) + time.Sleep(2 * time.Second) + err = lcon.Bind(c.bind_dn, c.bind_pw) + } else { + c.app.db.AddLog(0, "ldap bind error: "+err.Error()) + log.Fatal(err) + } + maxReconnectionAttempts++ } + c.con = lcon } diff --git a/routes.go b/routes.go index f83bf84..c3ad67b 100644 --- a/routes.go +++ b/routes.go @@ -5,9 +5,9 @@ import ( csh_auth "github.com/liam-middlebrook/csh-auth" log "github.com/sirupsen/logrus" "image" + _ "image/gif" _ "image/jpeg" _ "image/png" - _ "image/gif" "io" "net/http" "strconv"