2121c4dd by xiemengjun

modify struc

1 parent 446daaea
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 }
...@@ -24,4 +30,29 @@ type A struct{ ...@@ -24,4 +30,29 @@ type A struct{
24 30
25 type V struct{ 31 type V struct{
26 core.View 32 core.View
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()
27 } 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 {
19 Author string
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 }
92
93 func init() { 11 func init() {
94 http.HandleFunc("/", handleMainPage) 12 beego.C.loadconfig()
95 http.HandleFunc("/sign", handleSign)
96 } 13 }
14
15 func main() {
16 app:=beego.App
17 app.Run()
18 }
...\ No newline at end of file ...\ No newline at end of file
......
1 application: guestbook-go 1 application: blog
2 handlers: 2 handlers:
3 - url: /.* 3 - url: "/user"
4 handlefun:"user.Index"
5 - url: "/blog"
6 handlefun:"blog.Index"
...\ No newline at end of file ...\ No newline at end of file
......
1 package controller
2
3 import (
4 "github.com/astaxie/beego/beego"
5 "../model"
6 )
7
8 func UserIndex(w beego.A) {
9 userinfo :=model.User.getAll()
10 beego.V.Render(w,"users/index")
11 }
...\ No newline at end of file ...\ No newline at end of file
1 package model
2 import (
3 "./beego"
4 )
5
6 type Users struct {
7 username string
8 password string
9 beego.M
10 }
11
12 func NewUsers() (a *Users) {
13 return &Users{}
14 }
...\ No newline at end of file ...\ No newline at end of file
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!