set go mod

This commit is contained in:
Matthieu Lamalle 2019-12-13 16:05:19 +01:00
parent f92aaf9a1e
commit ecdde6debe
6 changed files with 71 additions and 34 deletions

View File

@ -1,17 +1,18 @@
package auth package auth
import ( import (
"cadoles/foodoles/config" "forge.cadoles.com/foodoles/config"
"log" "log"
"github.com/jtblin/go-ldap-client" "github.com/jtblin/go-ldap-client"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
const configFile string = "server.conf"
// 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 conf *config.Config
var conferr error var conferr error
conf, conferr = config.NewFromFile(configFile) conf, conferr = config.NewFromFile(configFile)

View File

@ -39,13 +39,18 @@ func CloseDB(db *bolt.DB) error {
} }
// InitDB init the db // InitDB init the db
func InitDB() { func InitDB() error {
db, err := OpenDB() db, err := OpenDB()
if err != nil { if err != nil {
log.Printf("\nOpenDB error: %v", err) log.Printf("\nOpenDB error: %v", err)
return return err
} }
CloseDB(db) defer func() {
if err1 := CloseDB(db); err1 != nil {
err = err1
}
}()
return nil
} }
// AddVote ajoute un vote à la bdd // AddVote ajoute un vote à la bdd

13
go.mod Normal file
View File

@ -0,0 +1,13 @@
module forge.cadoles.com/foodoles
go 1.13
require (
github.com/boltdb/bolt v1.3.1
github.com/go-chi/chi v4.0.2+incompatible
github.com/jtblin/go-ldap-client v0.0.0-20170223121919-b73f66626b33
github.com/pkg/errors v0.8.1
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
gopkg.in/ini.v1 v1.51.0
gopkg.in/ldap.v2 v2.5.1 // indirect
)

18
go.sum Normal file
View File

@ -0,0 +1,18 @@
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/jtblin/go-ldap-client v0.0.0-20170223121919-b73f66626b33 h1:XDpFOMOZq0u0Ar4F0p/wklqQXp/AMV1pTF5T5bDoUfQ=
github.com/jtblin/go-ldap-client v0.0.0-20170223121919-b73f66626b33/go.mod h1:+0BcLY5d54TVv6irFzHoiFvwAHR6T0g9B+by/UaS9T0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ldap.v2 v2.5.1 h1:wiu0okdNfjlBzg6UWvd1Hn8Y+Ux17/u/4nlk4CQr6tU=
gopkg.in/ldap.v2 v2.5.1/go.mod h1:oI0cpe/D7HRtBQl8aTg+ZmzFUAvu4lsv3eLXMLGFxWk=

View File

@ -1,11 +1,6 @@
package main package main
import ( import (
"cadoles/foodoles/auth"
"cadoles/foodoles/bdd"
"cadoles/foodoles/config"
"cadoles/foodoles/foodlist"
"cadoles/foodoles/vote"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
@ -14,6 +9,11 @@ import (
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
"github.com/pkg/errors" "github.com/pkg/errors"
"forge.cadoles.com/foodoles/bdd"
"forge.cadoles.com/foodoles/config"
"forge.cadoles.com/foodoles/foodlist"
"forge.cadoles.com/foodoles/vote"
) )
// User is a user // User is a user
@ -22,7 +22,8 @@ type User struct {
Password string Password string
} }
var configFile = "server.conf" const configFile string = "server.conf"
var secret string var secret string
func main() { func main() {
@ -56,10 +57,11 @@ func main() {
func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
log.Printf("ParseForm() err: %v", err) log.Printf("ParseForm() err: %v", err)
} else { return
u.Auth(r.FormValue("user"), r.FormValue("password"))
} }
u.Auth(r.FormValue("user"), r.FormValue("password"))
if r.Method == http.MethodPost && r.Form.Get("option") != "" && r.Form.Get("key") == secret { if r.Method == http.MethodPost && r.Form.Get("option") != "" && r.Form.Get("key") == secret {
VoteEndPoint(w, r) VoteEndPoint(w, r)
return return
@ -81,17 +83,17 @@ func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Auth is the login page for user // Auth is the login page for user
func (u *User) Auth(user string, pass string) bool { func (u *User) Auth(user string, pass string) bool {
auth, _ := auth.LogIn(user, pass) // auth, _ := auth.LogIn(user, pass)
if auth { // if auth {
u.Name = user // u.Name = user
u.Password = pass // u.Password = pass
} else { // } else {
u.Name = "" // u.Name = ""
u.Password = "" // u.Password = ""
} //}
// auth := true auth := true
// u.Name = "mlamalle" u.Name = "mlamalle"
// u.Password = "pass" u.Password = "pass"
return auth return auth
} }
@ -101,8 +103,7 @@ func LogInPage(w http.ResponseWriter, r *http.Request) {
"./templates/login.tmpl", "./templates/login.tmpl",
} }
t := template.Must(template.New("login.tmpl").ParseFiles(paths...)) t := template.Must(template.New("login.tmpl").ParseFiles(paths...))
err := t.Execute(w, "") if err := t.Execute(w, ""); err != nil {
if err != nil {
log.Printf("\nExecute error: %v", err) log.Printf("\nExecute error: %v", err)
return return
} }
@ -122,8 +123,8 @@ func HomePage(w http.ResponseWriter, r *http.Request) {
"./templates/index.tmpl", "./templates/index.tmpl",
} }
t := template.Must(template.New("index.tmpl").ParseFiles(paths...)) t := template.Must(template.New("index.tmpl").ParseFiles(paths...))
err := t.Execute(w, datas)
if err != nil { if err := t.Execute(w, datas); err != nil {
log.Printf("\nExecute error: %v", err) log.Printf("\nExecute error: %v", err)
return return
} }
@ -135,9 +136,8 @@ func ResultsPage(w http.ResponseWriter, r *http.Request) {
"./templates/results.tmpl", "./templates/results.tmpl",
} }
t := template.Must(template.New("results.tmpl").ParseFiles(paths...)) t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
err := t.Execute(w, vote.GetVotesOfTheDay())
if err != nil { if err := t.Execute(w, vote.GetVotesOfTheDay()); err != nil {
log.Printf("\nExecute error: %v", err) log.Printf("\nExecute error: %v", err)
return return
} }

View File

@ -1,7 +1,7 @@
package vote package vote
import ( import (
"cadoles/foodoles/bdd" "forge.cadoles.com/foodoles/bdd"
"log" "log"
"time" "time"
) )
@ -21,12 +21,13 @@ type Vote struct {
// ForFood vote pour un choix du jour // ForFood vote pour un choix du jour
func ForFood(Food string) { func ForFood(Food string) {
db, err := bdd.OpenDB() db, err := bdd.OpenDB()
defer bdd.CloseDB(db)
if err != nil { if err != nil {
log.Printf("\nOpenDB error: %v", err) log.Printf("\nOpenDB error: %v", err)
return return
} }
bdd.AddVote(db, Food, time.Now().AddDate(0, 0, 0)) bdd.AddVote(db, Food, time.Now().AddDate(0, 0, 0))
bdd.CloseDB(db)
return return
} }
@ -55,14 +56,13 @@ func FoodVotesList() []Vote {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer bdd.CloseDB(db)
lvotes, _ := bdd.GetVotesOfTheDay(db) lvotes, _ := bdd.GetVotesOfTheDay(db)
for _, fo := range lvotes { for _, fo := range lvotes {
vf := Vote{fo, 1} vf := Vote{fo, 1}
f = append(f, vf) f = append(f, vf)
} }
bdd.CloseDB(db)
return f return f
} }