diff --git a/README.md b/README.md index c886f12..690879b 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,12 @@ NPM dependencies: npm install ``` +### As there is no versioning yet, you need to manually go get all the packages + +``` +go get ./... +``` + ### NOTE: for debian users If you use debian the node binary is named nodejs you have to symlink it to node: @@ -24,6 +30,9 @@ If you use debian the node binary is named nodejs you have to symlink it to node ## Dev +#### Check your config.yml file + +#### Run ``` make dev ``` diff --git a/src/internal/auth/auth.go b/src/internal/auth/auth.go index f621dd1..7144a68 100644 --- a/src/internal/auth/auth.go +++ b/src/internal/auth/auth.go @@ -24,6 +24,7 @@ type UserBackend interface { // User interface for user type User interface { + GetName() string GetHash() string HasRole(string) bool } diff --git a/src/internal/auth/middleware.go b/src/internal/auth/middleware.go index 268ad45..e5cf922 100644 --- a/src/internal/auth/middleware.go +++ b/src/internal/auth/middleware.go @@ -5,6 +5,8 @@ import ( "fmt" "net/http" + "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/data" + "github.com/Sirupsen/logrus" ) @@ -28,7 +30,14 @@ func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http panic(err) } - m.log.Debug("setting user in the context") + if user != nil { + name := user.GetName() + m.log.Debugf("setting user %s in the context", name) + data.SetData(r, "user", user) + } else { + m.log.Debugf("got a nil user in the context") + } + ctx := context.WithValue(r.Context(), "auth.user", user) r = r.WithContext(ctx) diff --git a/src/internal/data/data.go b/src/internal/data/data.go new file mode 100644 index 0000000..2a2a8cd --- /dev/null +++ b/src/internal/data/data.go @@ -0,0 +1,42 @@ +package data + +import ( + "fmt" + "net/http" + + "context" +) + +type key int + +// dKey is key for access to response data in context +const dKey key = 0 + +// GetAllData return response's data +func GetAllData(r *http.Request) map[string]interface{} { + data := GetData(r, "data") + if data == nil { + data = make(map[string]interface{}) + } + mapData, ok := data.(map[string]interface{}) + if !ok { + fmt.Printf("something wrong with data") + } + // log.Printf("got all data %+v", mapData) + return mapData +} + +// SetData sets some response's data for access in template +func SetData(r *http.Request, key string, val interface{}) { + allData := GetAllData(r) + allData[key] = val + + ctx := context.WithValue(r.Context(), "data", allData) + *r = *r.WithContext(ctx) + allData = GetAllData(r) +} + +// GetData gets some response's data for access in template +func GetData(r *http.Request, key string) interface{} { + return r.Context().Value(key) +} diff --git a/src/internal/movies/handlers.go b/src/internal/movies/handlers.go index 8f28f4d..e08adbb 100644 --- a/src/internal/movies/handlers.go +++ b/src/internal/movies/handlers.go @@ -15,6 +15,7 @@ import ( "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/auth" "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/config" + "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/data" "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/users" "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/web" ) @@ -114,14 +115,14 @@ func FromPolochon(env *web.Env, w http.ResponseWriter, r *http.Request) error { if !ok { return fmt.Errorf("invalid user type") } - web.SetData(r, "user", user) + data.SetData(r, "user", user) movies, err := getPolochonMovies(user) if err != nil { // Catch network error for accessing specified polochon address if err == ErrPolochonUnavailable { - web.SetData(r, "error", "Invalid address") + data.SetData(r, "error", "Invalid address") return env.Rends(w, r, "movies/library") } @@ -167,7 +168,7 @@ func FromPolochon(env *web.Env, w http.ResponseWriter, r *http.Request) error { sort.Sort(smovies) - web.SetData(r, "movies", movies[params.Start:params.Start+params.Limit]) + data.SetData(r, "movies", movies[params.Start:params.Start+params.Limit]) return env.Rends(w, r, "movies/library") } @@ -205,6 +206,7 @@ func ExplorePopular(env *web.Env, w http.ResponseWriter, r *http.Request) error movies = append(movies, movie) ids = append(ids, m.IDs.ImDB) } - web.SetData(r, "movies", movies) + data.SetData(r, "movies", movies) + return env.Rends(w, r, "movies/library") } diff --git a/src/internal/users/handlers.go b/src/internal/users/handlers.go index a8fcf03..e6a38ff 100644 --- a/src/internal/users/handlers.go +++ b/src/internal/users/handlers.go @@ -9,6 +9,7 @@ import ( "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/auth" "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/config" + "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/data" "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/web" ) @@ -37,20 +38,23 @@ func LoginPOSTHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error err = e.Auth.Login(w, r, form.Username, form.Password) if err != nil { if err == auth.ErrInvalidPassword || err == ErrUnknownUser { - web.SetData(r, "FormErrors", "Error invalid user or password") + data.SetData(r, "FormErrors", "Error invalid user or password") return e.Rends(w, r, "users/login") } return err } + e.Log.Debug("logged") path, err := auth.GetPostLoginRedirect(e.Auth, w, r) if err != nil { return err } + e.Log.Debugf("redirecting to %s", path) if path != "" { http.Redirect(w, r, path, http.StatusTemporaryRedirect) return nil } + e.Log.Debugf("got no path, redirecting to /") http.Redirect(w, r, "/", http.StatusTemporaryRedirect) return nil } @@ -58,8 +62,8 @@ func LoginPOSTHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error // LogoutHandler just logout func LogoutHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error { e.Auth.Logout(w, r) - route := e.GetLoginRouteGetter()() - http.Redirect(w, r, route, http.StatusTemporaryRedirect) + + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) return nil } @@ -77,8 +81,7 @@ func DetailsHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error { return err } - web.SetData(r, "user", user) - web.SetData(r, "polochon", polochonConfig) + data.SetData(r, "polochon", polochonConfig) return e.Rends(w, r, "users/details") } @@ -96,8 +99,7 @@ func EditHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error { } if r.Method == "GET" { - web.SetData(r, "user", user) - web.SetData(r, "polochon", polochonConfig) + data.SetData(r, "polochon", polochonConfig) return e.Rends(w, r, "users/edit") } diff --git a/src/internal/users/users.go b/src/internal/users/users.go index fa95664..5359dae 100644 --- a/src/internal/users/users.go +++ b/src/internal/users/users.go @@ -182,6 +182,11 @@ func (u *User) GetHash() string { return u.Hash } +// GetName implements auth.User interface +func (u *User) GetName() string { + return u.Name +} + func (u *User) HasRole(role string) bool { if role == AdminRole && !u.Admin { return false diff --git a/src/internal/web/data.go b/src/internal/web/data.go deleted file mode 100644 index 78c8416..0000000 --- a/src/internal/web/data.go +++ /dev/null @@ -1,36 +0,0 @@ -package web - -import ( - "net/http" - - "github.com/gorilla/context" -) - -type key int - -// dKey is key for access to response data in context -const dKey key = 0 - -// GetAllData return response's data -func GetAllData(r *http.Request) map[string]interface{} { - data, ok := context.GetOk(r, dKey) - if !ok { - return nil - } - d, ok := data.(map[string]interface{}) - if !ok { - return nil - } - return d -} - -// SetData sets some response's data for access in template -func SetData(r *http.Request, key string, val interface{}) { - data, ok := context.GetOk(r, dKey) - if !ok { - context.Set(r, dKey, make(map[string]interface{})) - data = make(map[string]interface{}) - } - data.(map[string]interface{})[key] = val - context.Set(r, dKey, data) -} diff --git a/src/internal/web/templates.go b/src/internal/web/templates.go index 6c8e41c..7750799 100644 --- a/src/internal/web/templates.go +++ b/src/internal/web/templates.go @@ -4,6 +4,8 @@ import ( "html/template" "net/http" + "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/data" + "github.com/gorilla/mux" ) @@ -35,12 +37,12 @@ func (e *Env) Rends(w http.ResponseWriter, r *http.Request, template string) err if r.Header.Get("Accept") == "application/json" { return e.Render.JSON(w, http.StatusOK, TemplateData{ Route: mux.CurrentRoute(r).GetName(), - Data: GetAllData(r), + Data: data.GetAllData(r), }) } return e.Render.HTML(w, http.StatusOK, template, TemplateData{ Route: mux.CurrentRoute(r).GetName(), - Data: GetAllData(r), + Data: data.GetAllData(r), }) } diff --git a/src/main.go b/src/main.go index 3da4738..90e474b 100644 --- a/src/main.go +++ b/src/main.go @@ -11,10 +11,10 @@ import ( "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/web" "github.com/Sirupsen/logrus" - "github.com/codegangsta/negroni" "github.com/gorilla/sessions" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" + "github.com/urfave/negroni" ) // UserBackend represents the data backend to get the user @@ -69,6 +69,7 @@ func main() { authMiddleware := auth.NewMiddleware(env.Auth, log) + env.Handle("/", movies.ExplorePopular).Name("movies.home") env.Handle("/users/login", users.LoginGETHandler).Name("users.login").Methods("GET") env.Handle("/users/login", users.LoginPOSTHandler).Name("users.login").Methods("POST") env.Handle("/users/logout", users.LogoutHandler).Name("users.logout") diff --git a/src/templates/layout.tmpl b/src/templates/layout.tmpl index 8d38326..c20cf23 100644 --- a/src/templates/layout.tmpl +++ b/src/templates/layout.tmpl @@ -12,7 +12,9 @@
{{ template "navbar" $ }}