良好的框架

使用 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 ()
}