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

157 lines
3.8 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"
)
const (
2012-08-19 15:58:37 +02:00
IP = "127.0.0.1"
DB_NAME = "trantor"
BOOKS_COLL = "books"
NEW_BOOKS_COLL = "new"
USERS_COLL = "users"
PASS_SALT = "ImperialLibSalt"
2012-08-21 11:35:06 +02:00
TAGS_DISPLAY = 50
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-15 20:16:13 +02:00
data.Count, _ = coll.Count()
2012-08-17 08:25:44 +02:00
coll.Find(bson.M{}).Sort("-_id").Limit(6).All(&data.Books)
2012-08-21 21:22:56 +02:00
for i, b := range data.Books {
data.Books[i].Id = bson.ObjectId(b.Id).Hex()
}
2012-08-15 14:26:10 +02:00
loadTemplate(w, "index", data)
}
}
2012-07-30 23:23:38 +02:00
func main() {
session, err := mgo.Dial(IP)
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
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-21 20:54:57 +02:00
http.HandleFunc("/readnew/", readHandler(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-07-30 23:23:38 +02:00
http.ListenAndServe(":8080", nil)
}