parent
ac5e21d8ac
commit
ecefc619e4
3
main.go
3
main.go
|
@ -33,9 +33,8 @@ var maxProcs = flag.Int("max_procs", 0, "max number of CPUs that can be used sim
|
|||
func main() {
|
||||
flag.Parse()
|
||||
setMaxProcs()
|
||||
util.InitLogger()
|
||||
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
//gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
// middleware settings
|
||||
r.Use(util.Ginrus(time.RFC3339, true))
|
||||
|
|
12
route/api.go
12
route/api.go
|
@ -1,8 +1,18 @@
|
|||
package route
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"amuz.es/go/mnemonics/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Api(c *gin.Context) {
|
||||
content := gin.H{"Hello": "World"}
|
||||
|
||||
conn := util.NewAccoauntSource()
|
||||
conn.Connect()
|
||||
defer conn.Close()
|
||||
conn.Search()
|
||||
util.Log().Error("hello")
|
||||
|
||||
c.JSON(200, content)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
// ldap
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/nmcclain/ldap"
|
||||
)
|
||||
|
||||
type ldapUnit struct {
|
||||
dn string
|
||||
filter string
|
||||
uniqueAttributeName string
|
||||
}
|
||||
|
||||
type ldapSource struct {
|
||||
host string
|
||||
port int16
|
||||
user ldapUnit
|
||||
group ldapUnit
|
||||
connection *ldap.Conn
|
||||
}
|
||||
|
||||
type AccountSource interface {
|
||||
Connect()
|
||||
Search()
|
||||
Close()
|
||||
}
|
||||
|
||||
//ldapSourceBool
|
||||
func (l *ldapSource) Connect() {
|
||||
addr := fmt.Sprintf("%s:%d", l.host, l.port)
|
||||
conn, err := ldap.DialTLS("tcp", addr, nil)
|
||||
if err == nil {
|
||||
// (*conn).Debug = true
|
||||
l.connection = conn
|
||||
} else {
|
||||
Log().Error(err.Error())
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ldapSource) Close() {
|
||||
if l.connection != nil {
|
||||
l.connection.Close()
|
||||
l.connection = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (l ldapSource) Search() {
|
||||
|
||||
search := ldap.NewSearchRequest(
|
||||
l.user.dn,
|
||||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||
l.user.filter,
|
||||
nil,
|
||||
nil)
|
||||
searchResults, err := l.connection.Search(search)
|
||||
// be sure to add error checking!
|
||||
if err == nil {
|
||||
for _, v := range searchResults.Entries {
|
||||
|
||||
m := make(map[string][]string)
|
||||
for _, attr := range v.Attributes {
|
||||
m[attr.Name] = append(m[attr.Name], attr.Values...)
|
||||
}
|
||||
Log().Error(m)
|
||||
}
|
||||
} else {
|
||||
Log().Error(err.Error())
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func NewAccoauntSource() AccountSource {
|
||||
user := ldapUnit{
|
||||
dn: "ou=User,dc=amuz,dc=es",
|
||||
filter: "(objectClass=person)",
|
||||
uniqueAttributeName: "uid"}
|
||||
group := ldapUnit{
|
||||
dn: "ou=Group,dc=amuz,dc=es",
|
||||
filter: "|((objectClass=groupOfNames)(objectClass=groupOfUniqueNames))",
|
||||
uniqueAttributeName: "cn"}
|
||||
return &ldapSource{
|
||||
host: "keeper.amuz.es",
|
||||
port: 636,
|
||||
user: user,
|
||||
group: group}
|
||||
|
||||
}
|
|
@ -14,13 +14,12 @@ import (
|
|||
|
||||
var logger = logrus.New()
|
||||
|
||||
func InitLogger() {
|
||||
// logging framework
|
||||
func Log() *logrus.Logger {
|
||||
if logger == nil {
|
||||
logger = logrus.New()
|
||||
logger.Level = logrus.DebugLevel
|
||||
logger.Out = os.Stderr
|
||||
}
|
||||
|
||||
func Log() *logrus.Logger {
|
||||
}
|
||||
// logging framework
|
||||
return logger
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue