update book code

This commit is contained in:
Unknwon
2015-03-03 12:25:25 -05:00
parent b8c82ba4e5
commit eab1d98ba8
465 changed files with 15392 additions and 1572 deletions

View File

@@ -0,0 +1,8 @@
application: helloworld
version: 1
runtime: go
api_version: 3
handlers:
- url: /.*
script: _go_app

View File

@@ -0,0 +1,14 @@
package hello
import (
"fmt"
"net/http"
)
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world!")
}

View File

@@ -0,0 +1,28 @@
package hello
import (
"appengine"
"appengine/user"
"fmt"
"net/http"
)
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
fmt.Fprintf(w, "Hello, %v!", u)
}

View File

@@ -0,0 +1,46 @@
package hello
import (
"fmt"
"net/http"
"template"
)
const guestbookForm = `
<html>
<body>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>
`
const signTemplateHTML = `
<html>
<body>
<p>You wrote:</p>
<pre>{{html .}}</pre>
</body>
</html>
`
var signTemplate = template.Must(template.New("sign").Parse(signTemplateHTML))
func init() {
http.HandleFunc("/", root)
http.HandleFunc("/sign", sign)
}
func root(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, guestbookForm)
}
func sign(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
err := signTemplate.Execute(w, r.FormValue("content"))
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
}

View File

@@ -0,0 +1,72 @@
package hello
import (
"appengine"
"appengine/datastore"
"appengine/user"
"net/http"
"template"
"time"
)
const guestbookTemplateHTML = `
<html>
<body>
{{range .}}
{{with .Author}}
<p><b>{{html .}}</b> wrote:</p>
{{else}}
<p>An anonymous person wrote:</p>
{{end}}
<pre>{{html .Content}}</pre>
<pre>{{html .Date}}</pre>
{{end}}
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>
`
var guestbookTemplate = template.Must(template.New("book").Parse(guestbookTemplateHTML))
type Greeting struct {
Author string
Content string
Date datastore.Time
}
func init() {
http.HandleFunc("/", root)
http.HandleFunc("/sign", sign)
}
func root(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
q := datastore.NewQuery("Greeting").Order("-Date").Limit(10)
greetings := make([]Greeting, 0, 10)
if _, err := q.GetAll(c, &greetings); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
if err := guestbookTemplate.Execute(w, greetings); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
}
func sign(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
g := Greeting{
Content: r.FormValue("content"),
Date: datastore.SecondsToTime(time.Seconds()),
}
if u := user.Current(c); u != nil {
g.Author = u.String()
}
_, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Greeting", nil), &g)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}

View File

@@ -0,0 +1,16 @@
// helloworld.go
package helloworld
import (
"fmt"
"net/http"
)
func init() {
http.HandleFunc("/", handle)
}
func handle(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<html><body>Hello, World! 세상아 안녕!! </body></html>")
}