Esempio n. 1
0
func post(w http.ResponseWriter, r *http.Request) {
	user := store.CurrentUser(r)
	title := r.FormValue("title")
	content := r.FormValue("content")
	if user != nil && title != "" && content != "" {
		store.AddPost(user.Username, title, content)
	}
	http.Redirect(w, r, "/", http.StatusFound)
}
Esempio n. 2
0
func Renderhtmlindexhtml(writer http.ResponseWriter, request *http.Request) {
	request.ParseForm()
	writer.Header().Set("Content-Type", "text/html")
	print := func(toPrint ...interface{}) {
		fmt.Fprint(writer, toPrint...)
	}
	formValue := func(keyToGet string) string {
		return request.FormValue(keyToGet)
	}
	_ = print
	_ = formValue // prevent initialization runtime error

	posts := store.GetPosts()
	user := store.CurrentUser(request)
	login := false
	if user != nil {
		login = true
	}

	fmt.Fprint(writer, `
<html>
	<head> <title>gopages Sample Blog</title>	</head>
	<body>
		`)
	if login {
		fmt.Fprint(writer, `
			<p>
				Logged in [`)
		print(user.Username)
		fmt.Fprint(writer, `] |
				<a href="/logout">Logout</a>
			</p>
			`)

		fmt.Fprint(writer, `<h3> Create Post </h3>
<form action="/post" method="post">
	Title
	<br />
	<input type="text" name="title" />
	<br />
	Contents
	<br />
	<textarea name="content"></textarea>
	<br />
	<input type="submit" value="Post" />
</form>`)

	} else {
		fmt.Fprint(writer, `
			<a href="/login">Login</a> or <a href="/register">Register</a> to post
			<hr />
		`)
	}
	for _, post := range posts {

		fmt.Fprint(writer, `<div>
	<h3> `)
		print(post.Title)
		fmt.Fprint(writer, ` </h3>
	<p>
		`)
		print(post.Content)
		fmt.Fprint(writer, `
	</p>
	<p>
		<sub> Posted by `)
		print(post.Name)
		fmt.Fprint(writer, ` on `)
		print(post.Time)
		fmt.Fprint(writer, ` </sub>
	</p>
	<hr/>
</div>`)

	}
	if len(posts) == 0 {
		print("No posts yet")
	}

	fmt.Fprint(writer, `
	</body>
</html>`)

}