Ejemplo n.º 1
0
// Loads users info - or supplys links to login or register
func LoadUserInfo(title string, r *http.Request) (filename string, option []byte, usr []byte, bar []byte) {

	if cookies.IsLoggedIn(r) {

		cookie, _ := r.Cookie("SessionID")
		z := strings.Split(cookie.Value, ":")
		filename = "accounts/" + z[0]
		usr = []byte("<a href='" + filename + "'>" + z[0] + "</a>: ")
		option = []byte("<a href='/logout'>logout</a>")

		userProfile := FindUser(z[0])

		// Will need to change/add checkin function
		str := "Book Collection: <br><br>"
		// TODO instead of downloading it may make more sense to bring
		// them to a page where they can view the books "info"
		for i := 0; i < 5; i++ {
			if i < len(userProfile.BookList) {
				book := bkz.FindBook(userProfile.BookList[i])
				link := "<a href='books/" + book.ISBN + ".info'>"
				str += link + book.Title + "</a><br><br>"
			} else {
				str += "<a href='/add-book.txt'>Add Book!</a><br><br>"
			}
		}

		bar = []byte(str)

	} else {
		option = []byte("<a href='/login'>login</a> or <a href='/register'>register</a>")
	}

	filename = "web/" + title + ".txt"

	// Adds link to profile if clicked.
	file := strings.Split(title, "/")
	if len(file) > 1 {
		filename = "accounts/" + file[1] + ".profile"
	}

	return filename, option, usr, bar

}
Ejemplo n.º 2
0
// Shows a particular page
func viewHandler(w http.ResponseWriter, r *http.Request) {

	title := r.URL.Path[len("/"):]
	p, err := loadPage(title, r)

	// wonky TODO make better
	z := strings.Split(title, "/")
	if z[0] == "books" {
		http.ServeFile(w, r, title)
		return
	}

	if err != nil && !cookies.IsLoggedIn(r) {
		http.Redirect(w, r, "/home", http.StatusFound)
		return
	} else if err != nil {
		http.Redirect(w, r, "/login-succeeded", http.StatusFound)
		return
	}

	t, _ := template.ParseFiles("view.html")
	t.Execute(w, p)
}