這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
Beego架構不錯,個人比較喜歡,關鍵是還有一些開發工具很實用,封裝了很多實現,不需要自己做很多工作,在不考慮效率的前提下,都是可以接受的範疇。
自訂錯誤頁面
Beego預設自己帶了一些錯誤頁面,在error.go檔案中進行了定義。以404為例:
// show 404 notfound error.func NotFound(rw http.ResponseWriter, r *http.Request) { t, _ := template.New("beegoerrortemp").Parse(errtpl) data := make(map[string]interface{}) data["Title"] = "Page Not Found" data["Content"] = template.HTML("<br>The page you have requested has flown the coop." + "<br>Perhaps you are here because:" + "<br><br><ul>" + "<br>The page has moved" + "<br>The page no longer exists" + "<br>You were looking for your puppy and got lost" + "<br>You like 404 pages" + "</ul>") data["BeegoVersion"] = VERSION //rw.WriteHeader(http.StatusNotFound) t.Execute(rw, data)}
其實自己做一個修改也很容易:
func page_not_found(rw http.ResponseWriter, r *http.Request){ t,_:= template.New("404.html").ParseFiles(beego.ViewsPath+"/404.html") data :=make(map[string]interface{}) data["content"] = "page not found" t.Execute(rw, data)}func main() { beego.Errorhandler("404",page_not_found) beego.Router("/", &controllers.MainController{}) beego.Run()}
Server欄位的修改
HTTP返回頭中的Server總是顯示的是BeegoServer,太不專業了,定義在config.go檔案中:
BeegoServerName = "beegoServer"
我最初是fork了一個repo改,後來發現,只需要在app.conf檔案中定義一下就可以了:
BeegoServerName = ACGSOSERVER
這樣Server這個欄位就更改掉了,其他的屬性也可以用這種方式定義掉。
GolangBeego