mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 02:16:48 +08:00
update book code
This commit is contained in:
8
eBook/examples/chapter_20/helloapp/app.yaml
Normal file
8
eBook/examples/chapter_20/helloapp/app.yaml
Normal file
@@ -0,0 +1,8 @@
|
||||
application: helloworld
|
||||
version: 1
|
||||
runtime: go
|
||||
api_version: 3
|
||||
|
||||
handlers:
|
||||
- url: /.*
|
||||
script: _go_app
|
@@ -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!")
|
||||
}
|
@@ -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)
|
||||
}
|
@@ -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)
|
||||
}
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
16
eBook/examples/chapter_20/helloworld.go
Normal file
16
eBook/examples/chapter_20/helloworld.go
Normal 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>")
|
||||
}
|
||||
|
Reference in New Issue
Block a user