guichet/http-utils.go

39 lines
797 B
Go
Raw Normal View History

2023-07-21 04:37:18 +00:00
/*
http-utils provide utility functions that interact with http
*/
package main
import (
"crypto/tls"
2023-07-21 07:15:54 +00:00
"log"
2023-07-21 04:37:18 +00:00
"net/http"
"github.com/go-ldap/ldap/v3"
)
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2023-07-21 07:15:54 +00:00
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
2023-07-21 04:37:18 +00:00
handler.ServeHTTP(w, r)
})
}
func ldapOpen(w http.ResponseWriter) *ldap.Conn {
l, err := ldap.DialURL(config.LdapServerAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
if config.LdapTLS {
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
}
return l
}