Beego的幾個小Tips

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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
  • Like
  • Tweet
  • +1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.