This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/trantor.go

159 lines
3.9 KiB
Go
Raw Normal View History

2012-07-30 23:23:38 +02:00
package main
import (
2012-08-18 02:06:43 +02:00
"crypto/md5"
2012-07-30 23:23:38 +02:00
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"net/http"
"os"
2012-07-30 23:23:38 +02:00
)
2012-08-18 02:06:43 +02:00
type aboutData struct {
S Status
}
2012-08-15 13:17:27 +02:00
func aboutHandler(w http.ResponseWriter, r *http.Request) {
2012-08-18 02:06:43 +02:00
var data aboutData
2012-08-19 02:29:34 +02:00
data.S = GetStatus(w, r)
2012-08-21 10:57:54 +02:00
data.S.About = true
2012-08-18 02:06:43 +02:00
loadTemplate(w, "about", data)
}
func logoutHandler(w http.ResponseWriter, r *http.Request) {
2012-08-19 02:29:34 +02:00
sess := GetSession(r)
sess.LogOut()
sess.Notify("Log out!", "Bye bye "+sess.User, "success")
sess.Save(w, r)
2012-08-18 02:06:43 +02:00
http.Redirect(w, r, "/", 307)
}
func loginHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
user := r.FormValue("user")
pass := r.FormValue("pass")
h := md5.New()
hash := h.Sum(([]byte)(PASS_SALT + pass))
2012-08-19 02:29:34 +02:00
n, _ := coll.Find(bson.M{"user": user, "pass": hash}).Count()
sess := GetSession(r)
2012-08-18 02:06:43 +02:00
if n != 0 {
2012-08-19 02:29:34 +02:00
sess.LogIn(user)
sess.Notify("Successful login!", "Welcome "+user, "success")
2012-08-18 02:06:43 +02:00
} else {
2012-08-19 02:29:34 +02:00
sess.Notify("Invalid login!", "user or password invalid", "error")
2012-08-18 02:06:43 +02:00
}
2012-08-19 02:29:34 +02:00
sess.Save(w, r)
2012-08-18 02:06:43 +02:00
}
2012-08-19 02:29:34 +02:00
http.Redirect(w, r, r.Referer(), 307)
2012-08-18 02:06:43 +02:00
}
}
type bookData struct {
2012-08-19 02:29:34 +02:00
S Status
Book Book
2012-08-15 13:17:27 +02:00
}
2012-08-15 13:58:16 +02:00
func bookHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
2012-08-18 02:06:43 +02:00
var data bookData
2012-08-19 02:29:34 +02:00
data.S = GetStatus(w, r)
2012-08-21 21:22:56 +02:00
id := bson.ObjectIdHex(r.URL.Path[len("/book/"):])
books, _, err := GetBook(coll, bson.M{"_id": id})
2012-08-19 02:29:34 +02:00
if err != nil || len(books) == 0 {
2012-08-18 02:06:43 +02:00
http.NotFound(w, r)
return
}
2012-08-19 02:29:34 +02:00
data.Book = books[0]
2012-08-18 02:06:43 +02:00
loadTemplate(w, "book", data)
2012-08-15 13:58:16 +02:00
}
2012-07-30 23:23:38 +02:00
}
2012-08-15 11:31:05 +02:00
func fileHandler(path string) {
h := http.FileServer(http.Dir(path[1:]))
http.Handle(path, http.StripPrefix(path, h))
2012-07-30 23:23:38 +02:00
}
2012-08-15 14:26:10 +02:00
type indexData struct {
2012-08-18 02:06:43 +02:00
S Status
2012-08-15 14:26:10 +02:00
Books []Book
Count int
2012-08-20 17:19:27 +02:00
Tags []string
}
2012-08-15 14:26:10 +02:00
func indexHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
2012-08-15 20:16:13 +02:00
var data indexData
2012-08-20 17:19:27 +02:00
/* get the tags */
2012-08-21 11:35:06 +02:00
tags, err := GetTags(coll)
2012-08-20 17:19:27 +02:00
if err == nil {
2012-08-21 11:35:06 +02:00
length := len(tags)
if length > TAGS_DISPLAY {
length = TAGS_DISPLAY
2012-08-20 17:28:53 +02:00
}
data.Tags = make([]string, length)
2012-08-21 11:35:06 +02:00
for i, tag := range tags {
if i == TAGS_DISPLAY {
2012-08-20 17:25:07 +02:00
break /* display only 50 */
}
2012-08-20 17:19:27 +02:00
if tag.Subject != "" {
data.Tags[i] = tag.Subject
}
}
}
2012-08-19 02:29:34 +02:00
data.S = GetStatus(w, r)
2012-08-18 02:06:43 +02:00
data.S.Home = true
2012-08-24 20:18:16 +02:00
data.Books, data.Count, _ = GetBook(coll, bson.M{}, 6)
2012-08-15 14:26:10 +02:00
loadTemplate(w, "index", data)
}
}
2012-07-30 23:23:38 +02:00
func main() {
2012-08-22 19:48:02 +02:00
session, err := mgo.Dial(DB_IP)
2012-07-30 23:23:38 +02:00
if err != nil {
panic(err)
}
defer session.Close()
coll := session.DB(DB_NAME).C(BOOKS_COLL)
2012-08-18 02:06:43 +02:00
userColl := session.DB(DB_NAME).C(USERS_COLL)
2012-08-19 15:58:37 +02:00
newColl := session.DB(DB_NAME).C(NEW_BOOKS_COLL)
2012-07-30 23:23:38 +02:00
_, err = os.Stat(BOOKS_PATH)
if err != nil {
os.Mkdir(BOOKS_PATH, os.ModePerm)
}
_, err = os.Stat(COVER_PATH)
if err != nil {
os.Mkdir(COVER_PATH, os.ModePerm)
}
_, err = os.Stat(NEW_PATH)
if err != nil {
os.Mkdir(NEW_PATH, os.ModePerm)
}
2012-08-15 13:58:16 +02:00
http.HandleFunc("/book/", bookHandler(coll))
http.HandleFunc("/search/", searchHandler(coll))
2012-08-20 14:25:18 +02:00
http.HandleFunc("/upload/", uploadHandler(newColl))
2012-08-18 02:06:43 +02:00
http.HandleFunc("/login/", loginHandler(userColl))
http.HandleFunc("/logout/", logoutHandler)
2012-08-19 15:58:37 +02:00
http.HandleFunc("/new/", newHandler(newColl))
2012-08-20 14:25:18 +02:00
http.HandleFunc("/delnew/", deleteHandler(newColl, "/new/"))
http.HandleFunc("/store/", storeHandler(newColl, coll))
2012-08-21 18:15:21 +02:00
http.HandleFunc("/read/", readHandler(coll))
2012-08-30 20:49:58 +02:00
http.HandleFunc("/content/", contentHandler(coll))
2012-08-21 20:54:57 +02:00
http.HandleFunc("/readnew/", readHandler(newColl))
2012-08-30 20:49:58 +02:00
http.HandleFunc("/contentnew/", contentHandler(newColl))
2012-08-19 15:58:37 +02:00
http.HandleFunc("/edit/", editHandler(coll))
http.HandleFunc("/save/", saveHandler(coll))
2012-08-20 14:25:18 +02:00
http.HandleFunc("/delete/", deleteHandler(coll, "/"))
2012-08-15 13:17:27 +02:00
http.HandleFunc("/about/", aboutHandler)
2012-08-15 11:31:05 +02:00
fileHandler("/img/")
fileHandler("/cover/")
fileHandler("/books/")
2012-08-17 08:25:44 +02:00
fileHandler("/css/")
fileHandler("/js/")
2012-08-15 14:26:10 +02:00
http.HandleFunc("/", indexHandler(coll))
2012-08-31 00:35:39 +02:00
http.ListenAndServe(PORT, nil)
2012-07-30 23:23:38 +02:00
}