1
0
Fork 0

removed logger init func

added initial ldap support
This commit is contained in:
Sangbum Kim 2016-03-14 02:36:32 +09:00
parent ac5e21d8ac
commit ecefc619e4
4 changed files with 107 additions and 9 deletions

View File

@ -33,9 +33,8 @@ var maxProcs = flag.Int("max_procs", 0, "max number of CPUs that can be used sim
func main() { func main() {
flag.Parse() flag.Parse()
setMaxProcs() setMaxProcs()
util.InitLogger()
gin.SetMode(gin.ReleaseMode) //gin.SetMode(gin.ReleaseMode)
r := gin.New() r := gin.New()
// middleware settings // middleware settings
r.Use(util.Ginrus(time.RFC3339, true)) r.Use(util.Ginrus(time.RFC3339, true))

View File

@ -1,8 +1,18 @@
package route package route
import "github.com/gin-gonic/gin" import (
"amuz.es/go/mnemonics/util"
"github.com/gin-gonic/gin"
)
func Api(c *gin.Context) { func Api(c *gin.Context) {
content := gin.H{"Hello": "World"} content := gin.H{"Hello": "World"}
conn := util.NewAccoauntSource()
conn.Connect()
defer conn.Close()
conn.Search()
util.Log().Error("hello")
c.JSON(200, content) c.JSON(200, content)
} }

90
util/ldap.go Normal file
View File

@ -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}
}

View File

@ -14,13 +14,12 @@ import (
var logger = logrus.New() var logger = logrus.New()
func InitLogger() { func Log() *logrus.Logger {
// logging framework if logger == nil {
logger = logrus.New()
logger.Level = logrus.DebugLevel logger.Level = logrus.DebugLevel
logger.Out = os.Stderr logger.Out = os.Stderr
} }
func Log() *logrus.Logger {
// logging framework // logging framework
return logger return logger
} }