diff --git a/.gitignore b/.gitignore index 11969ca..5c0581f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -foods.db \ No newline at end of file +foods.db +server.conf \ No newline at end of file diff --git a/README.md b/README.md index ba4de07..6386948 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,5 @@ Projet Go de vote pour choisir ce qu'on mange au déjeuner TODO : Refactor en utilisant "github.com/go-chi/chi" et "github.com/go-chi/chi/middleware" - Gérer la configuration avec "gopkg.in/ini.v1" Afficher une liste de restaurants locaux répondant au vote du jour Afficher le résultat du vote \ No newline at end of file diff --git a/auth/auth.go b/auth/auth.go index e7c4afc..de17ff9 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -1,24 +1,34 @@ package auth import ( + "cadoles/foodoles/config" "log" "github.com/jtblin/go-ldap-client" + "github.com/pkg/errors" ) // LogIn auth the client func LogIn(username string, password string) (ok bool, user map[string]string) { + var configFile = "../server.conf" + var conf *config.Config + var conferr error + conf, conferr = config.NewFromFile(configFile) + if conferr != nil { + panic(errors.Wrapf(conferr, "error while loading config file '%s'", configFile)) + } + ldapclient := &ldap.LDAPClient{ - Base: "o=gouv,c=fr", - Host: "ldap.cadoles.com", - Port: 389, + Base: conf.LDAP.Base, + Host: conf.LDAP.Host, + Port: conf.LDAP.Port, UseSSL: false, - BindDN: "cn=reader,o=gouv,c=fr", - BindPassword: "ohc7kei8lil8Zoesai5chisaiGhu5Yaisai6kaegh9aingai0pae8ohb", - UserFilter: "(uid=%s)", + BindDN: conf.LDAP.BindDN, + BindPassword: conf.LDAP.BindPassword, + UserFilter: conf.LDAP.UserFilter, GroupFilter: "(memberUid=%s)", - Attributes: []string{"uid", "mail"}, + Attributes: conf.LDAP.Attributes, } defer ldapclient.Close() diff --git a/bdd/bdd.go b/bdd/bdd.go index 7da612d..c8ebfcc 100644 --- a/bdd/bdd.go +++ b/bdd/bdd.go @@ -93,7 +93,7 @@ func GetVotesOfTheDay(db *bolt.DB) ([]string, error) { }) return nil }) - fmt.Print(res) + //fmt.Print(res) return res, err } diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..1b55d54 --- /dev/null +++ b/config/config.go @@ -0,0 +1,69 @@ +package config + +import ( + "io" + + ini "gopkg.in/ini.v1" +) +// Config is the config +type Config struct { + HTTP HTTPConfig + LDAP LDAPConfig +} +// HTTPConfig is the http config +type HTTPConfig struct { + Address string + +} +// LDAPConfig is the ldap config +type LDAPConfig struct { + Base string + Host string + Port int + BindDN string + BindPassword string + UserFilter string + Attributes []string +} + +// NewFromFile retrieves the configuration from the given file +func NewFromFile(filepath string) (*Config, error) { + config := NewDefault() + cfg, err := ini.Load(filepath) + if err != nil { + return nil, err + } + if err := cfg.MapTo(config); err != nil { + return nil, err + } + return config, nil +} +// NewDefault set a default config +func NewDefault() *Config { + return &Config{ + HTTP: HTTPConfig{ + Address: ":3001", + }, + LDAP: LDAPConfig{ + Base: "dc=example,dc=com", + Host: "ldap.example.com", + Port: 389, + BindDN: "uid=readonlysuer,ou=People,dc=example,dc=com", + BindPassword: "readonlypassword", + UserFilter: "(uid=%s)", + Attributes: []string{"givenName", "sn", "mail", "uid"}, + }, + + } +} +// Dump return the config dump +func Dump(config *Config, w io.Writer) error { + cfg := ini.Empty() + if err := cfg.ReflectFrom(config); err != nil { + return err + } + if _, err := cfg.WriteTo(w); err != nil { + return err + } + return nil +} diff --git a/foods.db b/foods.db index 6fed49e..4424d75 100644 Binary files a/foods.db and b/foods.db differ diff --git a/server b/server index 1e8f239..f8a6942 100755 Binary files a/server and b/server differ diff --git a/server.go b/server.go index b139a7b..e5b34ad 100644 --- a/server.go +++ b/server.go @@ -2,6 +2,7 @@ package main import ( "cadoles/foodoles/bdd" + "cadoles/foodoles/config" "cadoles/foodoles/foodlist" "cadoles/foodoles/vote" "html/template" @@ -15,17 +16,26 @@ type User struct { Password string } +var configFile = "server.conf" + func main() { + var conf *config.Config + var conferr error + conf, conferr = config.NewFromFile(configFile) + if conferr != nil { + conf = config.NewDefault() + } + bdd.InitDB() vote.GetVotesOfTheDay() mux := http.NewServeMux() mux.Handle("/", &User{}) - s := &http.Server{Addr: "localhost:8080", Handler: mux} + s := &http.Server{Addr: "localhost" + conf.HTTP.Address, Handler: mux} - log.Print("\nready: listening on localhost:8080\n") + log.Print("\nready: listening on localhost" + conf.HTTP.Address + "\n") if err := s.ListenAndServe(); err != nil { panic(err) diff --git a/vote/vote.go b/vote/vote.go index 8bc7d57..10d3b94 100644 --- a/vote/vote.go +++ b/vote/vote.go @@ -2,7 +2,6 @@ package vote import ( "cadoles/foodoles/bdd" - "fmt" "log" "time" ) @@ -38,13 +37,13 @@ func GetVotesOfTheDay() VotesOfTheDay { dupmap := dupcount(duplicate) - fmt.Println(dupmap) + //fmt.Println(dupmap) for k, v := range dupmap { vv := Vote{k, v} vo.Votes = append(vo.Votes, vv) } - fmt.Println(vo) + //fmt.Println(vo) return vo }