diff --git a/beego/beego.go b/beego/beego.go index 7d63f48d..7bcda8a1 100644 --- a/beego/beego.go +++ b/beego/beego.go @@ -1,7 +1,13 @@ package beego -import "./core" - +import { + "net" + "net/http" + "net/http/fcgi" + "log" + "strconv" + "./core" +} type C struct { core.Content } @@ -24,4 +30,29 @@ type A struct{ type V struct{ core.View +} + +type BeegoApp struct{ + Port int +} + +func (app *BeegoApp) BeeListen(port int) { + app.Port = port + err := http.ListenAndServe(":"+strconv.Itoa(app.Port), nil) + if err != nil { + log.Fatal("ListenAndServe: ", err) + } +} + +func (app *BeegoApp) BeeListenFcgi(port int) { + app.Port = port + l, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port)) + if err != nil { + log.Fatal("ListenAndServe: ", err) + } + fcgi.Serve(l, app.Handler) +} + +func Run() { + rootPath, _ := os.Getwd() } \ No newline at end of file diff --git a/examples/blog/blog.go b/examples/blog/blog.go index c9c2f515..6c030ce4 100644 --- a/examples/blog/blog.go +++ b/examples/blog/blog.go @@ -5,92 +5,14 @@ package guestbook import ( - "io" - "net/http" - "text/template" - "time" - - "appengine" - "appengine/datastore" - "appengine/user" + "../../beego" ) -type Greeting struct { - Author string - Content string - Date time.Time -} - -func serve404(w http.ResponseWriter) { - w.WriteHeader(http.StatusNotFound) - w.Header().Set("Content-Type", "text/plain; charset=utf-8") - io.WriteString(w, "Not Found") -} - -func serveError(c appengine.Context, w http.ResponseWriter, err error) { - w.WriteHeader(http.StatusInternalServerError) - w.Header().Set("Content-Type", "text/plain; charset=utf-8") - io.WriteString(w, "Internal Server Error") - c.Errorf("%v", err) -} - -var mainPage = template.Must(template.New("guestbook").Parse( - `
-{{range .}} -{{with .Author}}{{.|html}}{{else}}An anonymous person{{end}} -on {{.Date.Format "3:04pm, Mon 2 Jan"}} -wrote{{.Content|html}}-{{end}} - -`)) - -func handleMainPage(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" || r.URL.Path != "/" { - serve404(w) - return - } - c := appengine.NewContext(r) - q := datastore.NewQuery("Greeting").Order("-Date").Limit(10) - var gg []*Greeting - _, err := q.GetAll(c, &gg) - if err != nil { - serveError(c, w, err) - return - } - w.Header().Set("Content-Type", "text/html; charset=utf-8") - if err := mainPage.Execute(w, gg); err != nil { - c.Errorf("%v", err) - } -} - -func handleSign(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - serve404(w) - return - } - c := appengine.NewContext(r) - if err := r.ParseForm(); err != nil { - serveError(c, w, err) - return - } - g := &Greeting{ - Content: r.FormValue("content"), - Date: time.Now(), - } - if u := user.Current(c); u != nil { - g.Author = u.String() - } - if _, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Greeting", nil), g); err != nil { - serveError(c, w, err) - return - } - http.Redirect(w, r, "/", http.StatusFound) -} - func init() { - http.HandleFunc("/", handleMainPage) - http.HandleFunc("/sign", handleSign) + beego.C.loadconfig() } + +func main() { + app:=beego.App + app.Run() +} \ No newline at end of file diff --git a/examples/blog/configs/app.yaml b/examples/blog/configs/app.yaml index c2f8c6c0..0107267a 100644 --- a/examples/blog/configs/app.yaml +++ b/examples/blog/configs/app.yaml @@ -1,3 +1,6 @@ -application: guestbook-go +application: blog handlers: -- url: /.* +- url: "/user" + handlefun:"user.Index" +- url: "/blog" + handlefun:"blog.Index" \ No newline at end of file diff --git a/examples/blog/controller/user.go b/examples/blog/controller/user.go new file mode 100644 index 00000000..61ad5756 --- /dev/null +++ b/examples/blog/controller/user.go @@ -0,0 +1,11 @@ +package controller + +import ( + "github.com/astaxie/beego/beego" + "../model" +) + +func UserIndex(w beego.A) { + userinfo :=model.User.getAll() + beego.V.Render(w,"users/index") +} \ No newline at end of file diff --git a/examples/blog/model/user.go b/examples/blog/model/user.go new file mode 100644 index 00000000..ad37141c --- /dev/null +++ b/examples/blog/model/user.go @@ -0,0 +1,14 @@ +package model +import ( + "./beego" +) + +type Users struct { + username string + password string + beego.M +} + +func NewUsers() (a *Users) { + return &Users{} +} \ No newline at end of file