From ecdde6debe3c78f6a80ea5da4eaa07a58fb40e0e Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Fri, 13 Dec 2019 16:05:19 +0100 Subject: [PATCH] set go mod --- auth/auth.go | 5 +++-- bdd/bdd.go | 11 ++++++++--- go.mod | 13 +++++++++++++ go.sum | 18 ++++++++++++++++++ server.go | 50 +++++++++++++++++++++++++------------------------- vote/vote.go | 8 ++++---- 6 files changed, 71 insertions(+), 34 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/auth/auth.go b/auth/auth.go index c16903e..532d816 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -1,17 +1,18 @@ package auth import ( - "cadoles/foodoles/config" + "forge.cadoles.com/foodoles/config" "log" "github.com/jtblin/go-ldap-client" "github.com/pkg/errors" ) +const configFile string = "server.conf" + // 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) diff --git a/bdd/bdd.go b/bdd/bdd.go index c8ebfcc..0e47229 100644 --- a/bdd/bdd.go +++ b/bdd/bdd.go @@ -39,13 +39,18 @@ func CloseDB(db *bolt.DB) error { } // InitDB init the db -func InitDB() { +func InitDB() error { db, err := OpenDB() if err != nil { 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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8fb4b70 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6c39437 --- /dev/null +++ b/go.sum @@ -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= diff --git a/server.go b/server.go index 7ab4b2e..bc415b9 100644 --- a/server.go +++ b/server.go @@ -1,11 +1,6 @@ package main import ( - "cadoles/foodoles/auth" - "cadoles/foodoles/bdd" - "cadoles/foodoles/config" - "cadoles/foodoles/foodlist" - "cadoles/foodoles/vote" "html/template" "log" "net/http" @@ -14,6 +9,11 @@ import ( "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "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 @@ -22,7 +22,8 @@ type User struct { Password string } -var configFile = "server.conf" +const configFile string = "server.conf" + var secret string func main() { @@ -56,10 +57,11 @@ func main() { func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { log.Printf("ParseForm() err: %v", err) - } else { - u.Auth(r.FormValue("user"), r.FormValue("password")) + return } + u.Auth(r.FormValue("user"), r.FormValue("password")) + if r.Method == http.MethodPost && r.Form.Get("option") != "" && r.Form.Get("key") == secret { VoteEndPoint(w, r) return @@ -81,17 +83,17 @@ func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) { // 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 - } else { - u.Name = "" - u.Password = "" - } - // auth := true - // u.Name = "mlamalle" - // u.Password = "pass" + // auth, _ := auth.LogIn(user, pass) + // if auth { + // u.Name = user + // u.Password = pass + // } else { + // u.Name = "" + // u.Password = "" + //} + auth := true + u.Name = "mlamalle" + u.Password = "pass" return auth } @@ -101,8 +103,7 @@ func LogInPage(w http.ResponseWriter, r *http.Request) { "./templates/login.tmpl", } t := template.Must(template.New("login.tmpl").ParseFiles(paths...)) - err := t.Execute(w, "") - if err != nil { + if err := t.Execute(w, ""); err != nil { log.Printf("\nExecute error: %v", err) return } @@ -122,8 +123,8 @@ func HomePage(w http.ResponseWriter, r *http.Request) { "./templates/index.tmpl", } 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) return } @@ -135,9 +136,8 @@ func ResultsPage(w http.ResponseWriter, r *http.Request) { "./templates/results.tmpl", } 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) return } diff --git a/vote/vote.go b/vote/vote.go index 10d3b94..48392e9 100644 --- a/vote/vote.go +++ b/vote/vote.go @@ -1,7 +1,7 @@ package vote import ( - "cadoles/foodoles/bdd" + "forge.cadoles.com/foodoles/bdd" "log" "time" ) @@ -21,12 +21,13 @@ type Vote struct { // ForFood vote pour un choix du jour func ForFood(Food string) { db, err := bdd.OpenDB() + defer bdd.CloseDB(db) if err != nil { log.Printf("\nOpenDB error: %v", err) return } bdd.AddVote(db, Food, time.Now().AddDate(0, 0, 0)) - bdd.CloseDB(db) + return } @@ -55,14 +56,13 @@ func FoodVotesList() []Vote { if err != nil { log.Fatal(err) } - + defer bdd.CloseDB(db) lvotes, _ := bdd.GetVotesOfTheDay(db) for _, fo := range lvotes { vf := Vote{fo, 1} f = append(f, vf) } - bdd.CloseDB(db) return f }