set config file

This commit is contained in:
Matthieu Lamalle 2019-11-27 12:09:15 +01:00
parent 68eb55d8a7
commit 86c2cf49f0
9 changed files with 103 additions and 15 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
foods.db foods.db
server.conf

View File

@ -5,6 +5,5 @@ Projet Go de vote pour choisir ce qu'on mange au déjeuner
TODO : TODO :
Refactor en utilisant "github.com/go-chi/chi" et "github.com/go-chi/chi/middleware" 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 une liste de restaurants locaux répondant au vote du jour
Afficher le résultat du vote Afficher le résultat du vote

View File

@ -1,24 +1,34 @@
package auth package auth
import ( import (
"cadoles/foodoles/config"
"log" "log"
"github.com/jtblin/go-ldap-client" "github.com/jtblin/go-ldap-client"
"github.com/pkg/errors"
) )
// LogIn auth the client // LogIn auth the client
func LogIn(username string, password string) (ok bool, user map[string]string) { 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{ ldapclient := &ldap.LDAPClient{
Base: "o=gouv,c=fr", Base: conf.LDAP.Base,
Host: "ldap.cadoles.com", Host: conf.LDAP.Host,
Port: 389, Port: conf.LDAP.Port,
UseSSL: false, UseSSL: false,
BindDN: "cn=reader,o=gouv,c=fr", BindDN: conf.LDAP.BindDN,
BindPassword: "ohc7kei8lil8Zoesai5chisaiGhu5Yaisai6kaegh9aingai0pae8ohb", BindPassword: conf.LDAP.BindPassword,
UserFilter: "(uid=%s)", UserFilter: conf.LDAP.UserFilter,
GroupFilter: "(memberUid=%s)", GroupFilter: "(memberUid=%s)",
Attributes: []string{"uid", "mail"}, Attributes: conf.LDAP.Attributes,
} }
defer ldapclient.Close() defer ldapclient.Close()

View File

@ -93,7 +93,7 @@ func GetVotesOfTheDay(db *bolt.DB) ([]string, error) {
}) })
return nil return nil
}) })
fmt.Print(res) //fmt.Print(res)
return res, err return res, err
} }

69
config/config.go Normal file
View File

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

BIN
foods.db

Binary file not shown.

BIN
server

Binary file not shown.

View File

@ -2,6 +2,7 @@ package main
import ( import (
"cadoles/foodoles/bdd" "cadoles/foodoles/bdd"
"cadoles/foodoles/config"
"cadoles/foodoles/foodlist" "cadoles/foodoles/foodlist"
"cadoles/foodoles/vote" "cadoles/foodoles/vote"
"html/template" "html/template"
@ -15,17 +16,26 @@ type User struct {
Password string Password string
} }
var configFile = "server.conf"
func main() { func main() {
var conf *config.Config
var conferr error
conf, conferr = config.NewFromFile(configFile)
if conferr != nil {
conf = config.NewDefault()
}
bdd.InitDB() bdd.InitDB()
vote.GetVotesOfTheDay() vote.GetVotesOfTheDay()
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("/", &User{}) 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 { if err := s.ListenAndServe(); err != nil {
panic(err) panic(err)

View File

@ -2,7 +2,6 @@ package vote
import ( import (
"cadoles/foodoles/bdd" "cadoles/foodoles/bdd"
"fmt"
"log" "log"
"time" "time"
) )
@ -38,13 +37,13 @@ func GetVotesOfTheDay() VotesOfTheDay {
dupmap := dupcount(duplicate) dupmap := dupcount(duplicate)
fmt.Println(dupmap) //fmt.Println(dupmap)
for k, v := range dupmap { for k, v := range dupmap {
vv := Vote{k, v} vv := Vote{k, v}
vo.Votes = append(vo.Votes, vv) vo.Votes = append(vo.Votes, vv)
} }
fmt.Println(vo) //fmt.Println(vo)
return vo return vo
} }