modify struc
Showing
5 changed files
with
69 additions
and
88 deletions
| 1 | package beego | 1 | package beego |
| 2 | 2 | ||
| 3 | import "./core" | 3 | import { |
| 4 | 4 | "net" | |
| 5 | "net/http" | ||
| 6 | "net/http/fcgi" | ||
| 7 | "log" | ||
| 8 | "strconv" | ||
| 9 | "./core" | ||
| 10 | } | ||
| 5 | type C struct { | 11 | type C struct { |
| 6 | core.Content | 12 | core.Content |
| 7 | } | 13 | } |
| ... | @@ -25,3 +31,28 @@ type A struct{ | ... | @@ -25,3 +31,28 @@ type A struct{ |
| 25 | type V struct{ | 31 | type V struct{ |
| 26 | core.View | 32 | core.View |
| 27 | } | 33 | } |
| 34 | |||
| 35 | type BeegoApp struct{ | ||
| 36 | Port int | ||
| 37 | } | ||
| 38 | |||
| 39 | func (app *BeegoApp) BeeListen(port int) { | ||
| 40 | app.Port = port | ||
| 41 | err := http.ListenAndServe(":"+strconv.Itoa(app.Port), nil) | ||
| 42 | if err != nil { | ||
| 43 | log.Fatal("ListenAndServe: ", err) | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | func (app *BeegoApp) BeeListenFcgi(port int) { | ||
| 48 | app.Port = port | ||
| 49 | l, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port)) | ||
| 50 | if err != nil { | ||
| 51 | log.Fatal("ListenAndServe: ", err) | ||
| 52 | } | ||
| 53 | fcgi.Serve(l, app.Handler) | ||
| 54 | } | ||
| 55 | |||
| 56 | func Run() { | ||
| 57 | rootPath, _ := os.Getwd() | ||
| 58 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -5,92 +5,14 @@ | ... | @@ -5,92 +5,14 @@ |
| 5 | package guestbook | 5 | package guestbook |
| 6 | 6 | ||
| 7 | import ( | 7 | import ( |
| 8 | "io" | 8 | "../../beego" |
| 9 | "net/http" | ||
| 10 | "text/template" | ||
| 11 | "time" | ||
| 12 | |||
| 13 | "appengine" | ||
| 14 | "appengine/datastore" | ||
| 15 | "appengine/user" | ||
| 16 | ) | 9 | ) |
| 17 | 10 | ||
| 18 | type Greeting struct { | 11 | func init() { |
| 19 | Author string | 12 | beego.C.loadconfig() |
| 20 | Content string | ||
| 21 | Date time.Time | ||
| 22 | } | ||
| 23 | |||
| 24 | func serve404(w http.ResponseWriter) { | ||
| 25 | w.WriteHeader(http.StatusNotFound) | ||
| 26 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
| 27 | io.WriteString(w, "Not Found") | ||
| 28 | } | ||
| 29 | |||
| 30 | func serveError(c appengine.Context, w http.ResponseWriter, err error) { | ||
| 31 | w.WriteHeader(http.StatusInternalServerError) | ||
| 32 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
| 33 | io.WriteString(w, "Internal Server Error") | ||
| 34 | c.Errorf("%v", err) | ||
| 35 | } | ||
| 36 | |||
| 37 | var mainPage = template.Must(template.New("guestbook").Parse( | ||
| 38 | `<html><body> | ||
| 39 | {{range .}} | ||
| 40 | {{with .Author}}<b>{{.|html}}</b>{{else}}An anonymous person{{end}} | ||
| 41 | on <em>{{.Date.Format "3:04pm, Mon 2 Jan"}}</em> | ||
| 42 | wrote <blockquote>{{.Content|html}}</blockquote> | ||
| 43 | {{end}} | ||
| 44 | <form action="/sign" method="post"> | ||
| 45 | <div><textarea name="content" rows="3" cols="60"></textarea></div> | ||
| 46 | <div><input type="submit" value="Sign Guestbook"></div> | ||
| 47 | </form></body></html> | ||
| 48 | `)) | ||
| 49 | |||
| 50 | func handleMainPage(w http.ResponseWriter, r *http.Request) { | ||
| 51 | if r.Method != "GET" || r.URL.Path != "/" { | ||
| 52 | serve404(w) | ||
| 53 | return | ||
| 54 | } | ||
| 55 | c := appengine.NewContext(r) | ||
| 56 | q := datastore.NewQuery("Greeting").Order("-Date").Limit(10) | ||
| 57 | var gg []*Greeting | ||
| 58 | _, err := q.GetAll(c, &gg) | ||
| 59 | if err != nil { | ||
| 60 | serveError(c, w, err) | ||
| 61 | return | ||
| 62 | } | ||
| 63 | w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
| 64 | if err := mainPage.Execute(w, gg); err != nil { | ||
| 65 | c.Errorf("%v", err) | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | func handleSign(w http.ResponseWriter, r *http.Request) { | ||
| 70 | if r.Method != "POST" { | ||
| 71 | serve404(w) | ||
| 72 | return | ||
| 73 | } | ||
| 74 | c := appengine.NewContext(r) | ||
| 75 | if err := r.ParseForm(); err != nil { | ||
| 76 | serveError(c, w, err) | ||
| 77 | return | ||
| 78 | } | ||
| 79 | g := &Greeting{ | ||
| 80 | Content: r.FormValue("content"), | ||
| 81 | Date: time.Now(), | ||
| 82 | } | ||
| 83 | if u := user.Current(c); u != nil { | ||
| 84 | g.Author = u.String() | ||
| 85 | } | ||
| 86 | if _, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Greeting", nil), g); err != nil { | ||
| 87 | serveError(c, w, err) | ||
| 88 | return | ||
| 89 | } | ||
| 90 | http.Redirect(w, r, "/", http.StatusFound) | ||
| 91 | } | 13 | } |
| 92 | 14 | ||
| 93 | func init() { | 15 | func main() { |
| 94 | http.HandleFunc("/", handleMainPage) | 16 | app:=beego.App |
| 95 | http.HandleFunc("/sign", handleSign) | 17 | app.Run() |
| 96 | } | 18 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
examples/blog/controller/user.go
0 → 100644
-
Please register or sign in to post a comment