diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..11969ca --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +foods.db \ No newline at end of file diff --git a/auth/auth.go b/auth/auth.go new file mode 100644 index 0000000..e7c4afc --- /dev/null +++ b/auth/auth.go @@ -0,0 +1,38 @@ +package auth + +import ( + "log" + + "github.com/jtblin/go-ldap-client" +) + +// LogIn auth the client +func LogIn(username string, password string) (ok bool, user map[string]string) { + + ldapclient := &ldap.LDAPClient{ + Base: "o=gouv,c=fr", + Host: "ldap.cadoles.com", + Port: 389, + UseSSL: false, + BindDN: "cn=reader,o=gouv,c=fr", + BindPassword: "ohc7kei8lil8Zoesai5chisaiGhu5Yaisai6kaegh9aingai0pae8ohb", + UserFilter: "(uid=%s)", + GroupFilter: "(memberUid=%s)", + Attributes: []string{"uid", "mail"}, + } + defer ldapclient.Close() + + ok, user, err := ldapclient.Authenticate(username, password) + + if err != nil { + log.Printf("Error authenticating user %s: %+v", "username", err) + return + } + if !ok { + log.Printf("Authenticating failed for user %s", "username") + return + } + log.Printf("User %s authentificated", username) + return ok, user + +} diff --git a/bdd/bdd.go b/bdd/bdd.go index 6b86638..94d3c3e 100644 --- a/bdd/bdd.go +++ b/bdd/bdd.go @@ -3,6 +3,7 @@ package bdd import ( "bytes" "fmt" + "log" "time" "github.com/boltdb/bolt" @@ -37,6 +38,16 @@ func CloseDB(db *bolt.DB) error { return db.Close() } +// InitDB init the db +func InitDB() { + db, err := OpenDB() + if err != nil { + log.Printf("\nOpenDB error: %v", err) + return + } + CloseDB(db) +} + // AddVote ajoute un vote à la bdd func AddVote(db *bolt.DB, vote string, date time.Time) error { @@ -72,9 +83,28 @@ func GetVotesOfTheDay(db *bolt.DB, date time.Time) ([]string, error) { err := db.View(func(tx *bolt.Tx) error { c := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Cursor() min := []byte(time.Now().AddDate(0, 0, -1).Format(time.RFC3339)) - max := []byte(time.Now().AddDate(0, 0, 0).Format(time.RFC3339)) + max := []byte(date.Format(time.RFC3339)) for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() { res = append(res, string(v)) + fmt.Println(string(k)) + fmt.Println(string(v)) + } + return nil + }) + return res, err +} + +// GetVotesOfTheRange liste tous les votes du jour +func GetVotesOfTheRange(db *bolt.DB, min time.Time, max time.Time) ([]string, error) { + res := []string{} + err := db.View(func(tx *bolt.Tx) error { + c := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Cursor() + mino := []byte(min.Format(time.RFC3339)) + maxo := []byte(max.Format(time.RFC3339)) + for k, v := c.Seek(mino); k != nil && bytes.Compare(k, maxo) <= 0; k, v = c.Next() { + res = append(res, string(v)) + fmt.Println(string(k)) + fmt.Println(string(v)) } return nil }) diff --git a/server b/server index 43052d1..b12ea66 100755 Binary files a/server and b/server differ diff --git a/server.go b/server.go index cbcfbda..b139a7b 100644 --- a/server.go +++ b/server.go @@ -9,36 +9,78 @@ import ( "net/http" ) +// User is a user +type User struct { + Name string + Password string +} + func main() { - http.HandleFunc("/", Base) - http.HandleFunc("/results", ResultsPage) - db, err := bdd.OpenDB() - if err != nil { - log.Printf("\nOpenDB error: %v", err) - return - } - bdd.CloseDB(db) + bdd.InitDB() vote.GetVotesOfTheDay() + mux := http.NewServeMux() + mux.Handle("/", &User{}) + s := &http.Server{Addr: "localhost:8080", Handler: mux} + log.Print("\nready: listening on localhost:8080\n") - if err := http.ListenAndServe(":8080", nil); err != nil { + if err := s.ListenAndServe(); err != nil { panic(err) } } -// Base is the entry point to all requests -func Base(w http.ResponseWriter, r *http.Request) { - - if r.Method == http.MethodPost { - VotePage(w, r) - } else if r.Method == http.MethodGet { - HomePage(w, r) +// ServerHTTP is the entry point to all requests +func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + log.Printf("ParseForm() err: %v", err) } else { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("400 - Unsupported Request Method!")) + u.Auth(r.FormValue("user"), r.FormValue("password")) + } + if u.Name == "" { + LogInPage(w, r) + return + } + if r.Method == http.MethodPost && r.Form.Get("option") != "" { + VoteEndPoint(w, r) + return + } + if r.URL.Path == "/" { + HomePage(w, r) + return + } + if r.URL.Path == "/results" { + ResultsPage(w, r) + return + } + +} + +// Auth is the login page for user +func (u *User) Auth(user string, pass string) bool { + // auth, _ := auth.LogIn(user, pass) + // if auth { + // u.Name = user + // u.Password = pass + // } + u.Name = "mlamalle" + u.Password = "password" + var auth = true + return auth +} + +// LogInPage is the log in page of the app +func LogInPage(w http.ResponseWriter, r *http.Request) { + paths := []string{ + "./templates/login.tmpl", + } + t := template.Must(template.New("login.tmpl").ParseFiles(paths...)) + err := t.Execute(w, "") + if err != nil { + log.Printf("\nExecute error: %v", err) + return } } @@ -58,26 +100,20 @@ func HomePage(w http.ResponseWriter, r *http.Request) { // ResultsPage is the page displaiyng the votes results func ResultsPage(w http.ResponseWriter, r *http.Request) { - db, err := bdd.OpenDB() - if err != nil { - log.Printf("\nOpenDB error: %v", err) - return - } - //vote, _ := bdd.GetVotesOfTheDay(db, time.Now().AddDate(0, 0, -1)) - bdd.CloseDB(db) paths := []string{ "./templates/results.tmpl", } t := template.Must(template.New("results.tmpl").ParseFiles(paths...)) - err = t.Execute(w, "") + err := t.Execute(w, vote.GetVotesOfTheDay()) + if err != nil { log.Printf("\nExecute error: %v", err) return } } -// VotePage is the endpoint to add votes -func VotePage(w http.ResponseWriter, r *http.Request) { +// VoteEndPoint is the endpoint to add votes +func VoteEndPoint(w http.ResponseWriter, r *http.Request) { r.ParseForm() option := r.Form.Get("option") diff --git a/templates/login.tmpl b/templates/login.tmpl new file mode 100644 index 0000000..2674bc4 --- /dev/null +++ b/templates/login.tmpl @@ -0,0 +1,80 @@ + + + + + Foodoles written in Go + + + + + + + + + + +
+
+

+
+ Log-in +
+

+
+
+
+
+ + +
+
+
+
+ + +
+
+
Login
+
+ +
+ +
+ + +
+
+ + + + diff --git a/templates/results.tmpl b/templates/results.tmpl index 84afca0..451163a 100644 --- a/templates/results.tmpl +++ b/templates/results.tmpl @@ -5,25 +5,20 @@ - - + -{{ range .Foods }} -
-
{{ .Icon }}
-
- {{ end }} +
-

TV Show - Poll Results


+

Foodoles - Results


- -
{{.Key}}
-
{{.Cluster}}
+ \ No newline at end of file diff --git a/vote/vote.go b/vote/vote.go index 4d17635..77bcf48 100644 --- a/vote/vote.go +++ b/vote/vote.go @@ -33,9 +33,8 @@ func ForFood(Food string) { // GetVotesOfTheDay récupère les votes du jour func GetVotesOfTheDay() VotesOfTheDay { - time.Now() vo := VotesOfTheDay{time.Now().Format("02/01/2006"), nil} - duplicate := FoodList() + duplicate := FoodVotesList() dupmap := dupcount(duplicate) @@ -49,8 +48,8 @@ func GetVotesOfTheDay() VotesOfTheDay { return vo } -// FoodList return a list of food -func FoodList() []Vote { +// FoodVotesList return a list of votes for food +func FoodVotesList() []Vote { f := []Vote{} db, err := bdd.OpenDB() @@ -58,7 +57,7 @@ func FoodList() []Vote { log.Fatal(err) } - lvotes, _ := bdd.GetVotesOfTheDay(db, time.Now()) + lvotes, _ := bdd.GetVotesOfTheRange(db, time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 0)) for _, fo := range lvotes { vf := Vote{fo, 1}