良好的框架

使用 Gramework Stats Dashboard 和指標中間件製作的 Gramework 長期測試台指標螢幕截圖
Gramework 是一個快速、有效率、可靠、SPA 優先、go-way 的 Web 框架,由 fasthttp 維護者製作。您可以獲得簡單但功能強大的 API,我們在內部處理優化。我們總是很高興看到您的功能請求和 PR。
使用 Gramework 的理由
Go >= 1.10.8 是最古老的持續測試和支援的版本。
如果您遇到任何漏洞,請隨時透過 [email protected] 提交。
| 姓名 | 連結/徽章 |
|---|---|
| 文件 | 戈多克 |
| 我們的吉拉 | 吉拉 |
| 許可證報告 | 報告 |
| 變更日誌 | 變更日誌 |
| 透過捐款支持我們或成為贊助商 | 開放集體 |
| 我們的電報聊天 | @gramework |
| 我們在 Gophers Slack 中的 #gramework 頻道 | https://gophers.slack.com |
| 我們的不和諧伺服器 | https://discord.gg/HkW8DsD |
| 主分支覆蓋範圍 | |
| 主分支狀態 | |
| 開發分支覆蓋範圍 | |
| 開發分支狀態 | |
| CII 最佳實踐 | |
| Grafana 的 Gramework 統計儀表板 | https://grafana.com/dashboards/3422 |
| 支持聯繫人 | 透過電子郵件:[email protected] |
| 透過 Telegram 社群:@gramework |
這個專案的存在要歸功於我們出色的貢獻者! [貢獻]。
感謝我們所有的支持者! [成為支持者]
成為贊助商來支持該計畫。您的徽標將顯示在此處,並帶有指向您網站的連結。 [成為贊助商]
/third_party_licenses/fasthttp和/third_party_licenses/fasthttprouter中找到相應的授權。nettls_*.go中,是 caddytls 的整合版本,因為透過簡單的導入使用它不是一個選項,gramework 基於fasthttp ,與net/http不相容。在我所依據的提交中,caddy 是Apache-2.0許可的。其授權位於/third_party_licenses/caddy 。 @mholt 允許我們複製此儲存庫中的程式碼。下面的範例將提供「hello, Grameworld」。 Gramework 將為您註冊bind標誌,讓您選擇 Gramework 應該偵聽的另一個 ip/連接埠:
package main
import (
"github.com/gramework/gramework"
)
func main () {
app := gramework . New ()
app . GET ( "/" , "hello, grameworld" )
app . ListenAndServe ()
}如果您不想支援bind標誌,請將可選的地址參數傳遞給ListenAndServe 。
注意:下面的所有範例都將註冊bind標誌。
從版本:1.1.0-rc1
下面的範例將從地圖中提供{"hello":"grameworld"} 。 Gramework 將為您註冊bind標誌,讓您選擇 Gramework 應該偵聽的另一個 ip/連接埠:
package main
import (
"github.com/gramework/gramework"
)
func main () {
app := gramework . New ()
app . GET ( "/" , func () map [ string ] interface {} {
return map [ string ] interface {}{
"hello" : "gramework" ,
}
})
app . ListenAndServe ()
}從版本:1.1.0-rc1
下面的範例將從結構中提供{"hello":"grameworld"}服務。 Gramework 將為您註冊bind標誌,讓您選擇 Gramework 應該偵聽的另一個 ip/連接埠:
package main
import (
"github.com/gramework/gramework"
)
type SomeResponse struct {
hello string
}
func main () {
app := gramework . New ()
app . GET ( "/" , func () interface {} {
return SomeResponse {
hello : "gramework" ,
}
})
app . ListenAndServe ()
}下面的範例將從./files提供靜態檔案:
package main
import (
"github.com/gramework/gramework"
)
func main () {
app := gramework . New ()
app . GET ( "/*any" , app . ServeDir ( "./files" ))
app . ListenAndServe ()
}下面的範例將提供一個位元組切片:
package main
import (
"fmt"
"os"
"time"
"github.com/gramework/gramework"
)
type SomeData struct {
Name string
Age uint8
}
func main () {
app := gramework . New ()
d := SomeData {
Name : "Grame" ,
Age : 20 ,
}
// service-wide CORS. you can also instead of using middleware
// call ctx.CORS() manually
app . Use ( app . CORSMiddleware ())
app . GET ( "/someJSON" , func ( ctx * gramework. Context ) {
// send json, no metter if user asked for json, xml or anything else.
if err := ctx . JSON ( d ); err != nil {
// you can return err instead of manual checks and Err500() call.
// See next handler for example.
ctx . Err500 ()
}
})
app . GET ( "/simpleJSON" , func ( ctx * gramework. Context ) error {
return ctx . JSON ( d )
})
app . GET ( "/someData" , func ( ctx * gramework. Context ) error {
// send data in one of supported encodings user asked for.
// Now we support json, xml and csv. More coming soon.
sentType , err := ctx . Encode ( d )
if err != nil {
ctx . Logger . WithError ( err ). Error ( "could not process request" )
return err
}
ctx . Logger . WithField ( "sentType" , sentType ). Debug ( "some request-related message" )
return nil
})
// you can omit context if you want, return `interface{}`, `error` or both.
app . GET ( "/simplestJSON" , func () interface {} {
return d
})
// you can also use one of built-in types as a handler, we got you covered too
app . GET ( "/hostnameJSON" , fmt . Sprintf ( `{"hostname": %q}` , os . Hostname ()))
wait := make ( chan struct {})
go func () {
time . Sleep ( 10 * time . Minute )
app . Shutdown ()
wait <- struct {}{}
}()
app . ListenAndServe ()
// allow Shutdown() to stop the app properly.
// ListenAndServe will return before Shutdown(), so we should wait.
<- wait
}此範例示範如何從 fasthttp 遷移到 Gramework,而無需重寫處理程序。
package main
import (
"github.com/gramework/gramework"
"github.com/valyala/fasthttp"
)
func main () {
app := gramework . New ()
app . GET ( "/someJSON" , func ( ctx * fasthttp. RequestCtx ) {
ctx . WriteString ( "another data" )
})
app . ListenAndServe ()
}