1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 10:10:54 +00:00

modify struc

This commit is contained in:
xiemengjun 2012-03-30 23:48:36 +08:00
parent 446daaea37
commit 2121c4dd88
5 changed files with 70 additions and 89 deletions

View File

@ -1,7 +1,13 @@
package beego
import "./core"
import {
"net"
"net/http"
"net/http/fcgi"
"log"
"strconv"
"./core"
}
type C struct {
core.Content
}
@ -25,3 +31,28 @@ 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()
}

View File

@ -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(
`<html><body>
{{range .}}
{{with .Author}}<b>{{.|html}}</b>{{else}}An anonymous person{{end}}
on <em>{{.Date.Format "3:04pm, Mon 2 Jan"}}</em>
wrote <blockquote>{{.Content|html}}</blockquote>
{{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>
`))
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()
}

View File

@ -1,3 +1,6 @@
application: guestbook-go
application: blog
handlers:
- url: /.*
- url: "/user"
handlefun:"user.Index"
- url: "/blog"
handlefun:"blog.Index"

View File

@ -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")
}

View File

@ -0,0 +1,14 @@
package model
import (
"./beego"
)
type Users struct {
username string
password string
beego.M
}
func NewUsers() (a *Users) {
return &Users{}
}