From c513344169167b2b1a4a8426c6ebb2824aeeeb36 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Sat, 18 Aug 2012 02:06:43 +0200 Subject: [PATCH] Sessions with gorilla --- .gitignore | 1 + admin.go | 30 ++++++++++++++++++++ database.go | 1 + search.go | 5 ++-- session.go | 43 ++++++++++++++++++++++++++++ template.go | 8 ++++++ templates/about.html | 2 +- templates/book.html | 40 ++++++++++++++++++++++---- templates/footer.html | 11 +------- templates/header.html | 66 +++++++++++++++++++++++++++++++++++++------ templates/index.html | 8 +----- templates/search.html | 6 ++-- templates/upload.html | 4 +-- trantor.go | 59 +++++++++++++++++++++++++++++++++++--- upload.go | 14 +++++++-- 15 files changed, 251 insertions(+), 47 deletions(-) create mode 100644 admin.go create mode 100644 session.go diff --git a/.gitignore b/.gitignore index b35a33d..c6c1e7f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ upload/upload upload/books upload/cover upload/new +adduser/adduser diff --git a/admin.go b/admin.go new file mode 100644 index 0000000..b4631b2 --- /dev/null +++ b/admin.go @@ -0,0 +1,30 @@ +package main + +import ( + "labix.org/v2/mgo" + "labix.org/v2/mgo/bson" + "net/http" + "os" +) + +func deleteHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + if SessionUser(r) == "" { + http.NotFound(w, r) + return + } + + var id bson.ObjectId = bson.ObjectIdHex(r.URL.Path[len("/delete/"):]) + var book Book + if coll.Find(bson.M{"_id": id}).One(&book) != nil { + http.NotFound(w, r) + return + } + os.RemoveAll(book.Path) + os.RemoveAll(book.Cover[1:]) + os.RemoveAll(book.CoverSmall[1:]) + coll.Remove(bson.M{"_id": id}) + http.Redirect(w, r, "/", 307) + //TODO: notify deleted + } +} diff --git a/database.go b/database.go index 1688b37..cec0b25 100644 --- a/database.go +++ b/database.go @@ -1,6 +1,7 @@ package main type Book struct { + Id string `bson:"_id"` Title string Author []string Contributor string diff --git a/search.go b/search.go index 80831ad..a48a834 100644 --- a/search.go +++ b/search.go @@ -31,7 +31,7 @@ func buildQuery(q string) bson.M { } type searchData struct { - Search string + S Status Found int Books []Book Page int @@ -59,7 +59,8 @@ func searchHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request } var data searchData - data.Search = req + data.S.User = SessionUser(r) + data.S.Search = req data.Found = len(res) if len(res) > ITEMS_PAGE*(page+1) { data.Books = res[ITEMS_PAGE*page : ITEMS_PAGE*(page+1)] diff --git a/session.go b/session.go new file mode 100644 index 0000000..0616615 --- /dev/null +++ b/session.go @@ -0,0 +1,43 @@ +package main + +import ( + "net/http" + "code.google.com/p/gorilla/sessions" + "code.google.com/p/gorilla/securecookie" +) + +var sesStore = sessions.NewCookieStore(securecookie.GenerateRandomKey(64)) + +func CreateSession(user string, w http.ResponseWriter, r *http.Request) { + session, _ := sesStore.Get(r, "admin") + session.Values["user"] = user + session.Save(r, w) +} + +func SessionUser(r *http.Request) string { + session, err := sesStore.New(r, "admin") + if err != nil { + return "" + } + if session.IsNew { + return "" + } + user, ok := session.Values["user"].(string) + if !ok { + return "" + } + return user +} + +func LogOut(w http.ResponseWriter, r *http.Request) { + session, err := sesStore.Get(r, "admin") + if err != nil { + return + } + if session.IsNew { + return + } + session.Values["user"] = "" + session.Options.MaxAge = -1 + session.Save(r, w) +} diff --git a/template.go b/template.go index c6562c2..54d55e3 100644 --- a/template.go +++ b/template.go @@ -9,6 +9,14 @@ const ( TEMPLATE_DIR = "templates/" ) +type Status struct { + Search string + User string + Home bool + About bool + Upload bool +} + func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) { // TODO: when finish devel conver to global: var templates = template.Must(template.ParseFiles(TEMPLATE_DIR+"header.html", diff --git a/templates/about.html b/templates/about.html index 26519ec..ca2f7b2 100644 --- a/templates/about.html +++ b/templates/about.html @@ -1,4 +1,4 @@ -{{template "header.html"}} +{{template "header.html" .S}}

The Imperial Library of Trantor (also known as Galactic Library) is a repository of ebooks on ePub format.

diff --git a/templates/book.html b/templates/book.html index 18dafb1..f57fd0b 100644 --- a/templates/book.html +++ b/templates/book.html @@ -1,10 +1,27 @@ -{{template "header.html"}} +{{template "header.html" .S}} -
+{{$user := .S.User}} +{{with .Book}} + +
+ +

{{.Title}}

-
+
{{if .Cover}}
@@ -15,7 +32,7 @@

-
+
    {{if .Author}}
  • Author: {{range .Author}}{{.}}, {{end}}
  • {{end}} {{if .Publisher}}
  • Publisher: {{.Publisher}}
  • {{end}} @@ -24,8 +41,19 @@ {{if .Lang}}
  • Lang: {{range .Lang}}{{.}} {{end}}
  • {{end}}
-
+
+ {{if $user}} +
+ +
+

+ {{end}} +
@@ -35,7 +63,7 @@
-
+{{end}} {{template "footer.html"}} diff --git a/templates/footer.html b/templates/footer.html index 134357e..8fe5dca 100644 --- a/templates/footer.html +++ b/templates/footer.html @@ -7,17 +7,8 @@ - - - - - - - - - - + diff --git a/templates/header.html b/templates/header.html index fdc1e85..d98ccc4 100644 --- a/templates/header.html +++ b/templates/header.html @@ -9,24 +9,72 @@ Imperial Library of Trantor + + +