github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/docs/README.md (about) 1 # Gramework 2 3 [![codecov](https://codecov.io/gh/gramework/gramework/branch/master/graph/badge.svg)](https://codecov.io/gh/gramework/gramework) [![Build Status](https://travis-ci.org/gramework/gramework.svg?branch=master)](https://travis-ci.org/gramework/gramework) [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/1203/badge)](https://bestpractices.coreinfrastructure.org/projects/1203) [![Backers on Open Collective](https://opencollective.com/gramework/backers/badge.svg)](#backers) [![Sponsors on Open Collective](https://opencollective.com/gramework/sponsors/badge.svg)](#sponsors) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fgramework%2Fgramework.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fgramework%2Fgramework?ref=badge_shield) [![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com) 4 5 The Good Framework 6 7 [![Gramework Stats Screenshot](https://github.com/gramework/gramework/raw/dev/docs/gramework_dashboard.png)](https://grafana.com/dashboards/3422) 8 9 _Gramework long-term testing stand metrics screenshot made with [Gramework Stats Dashboard](https://grafana.com/dashboards/3422) and [metrics middleware](https://github.com/gramework/gramework/tree/dev/metrics)_ 10 11 ### What is it? 12 Gramework is a fast, highly effective, reliable, SPA-first, go-way web framework made by a [fasthttp](https://github.com/valyala/fasthttp) [maintainer](https://github.com/kirillDanshin). You get the simple yet powerful API, we handle optimizations internally. 13 We're always glad to see your feature requests and PRs. 14 15 ----- 16 17 **Reasons to use Gramework** 18 19 - Gramework has a stable API. 20 - Gramework is battle-tested. 21 - Gramework is made by a [maintainer](https://github.com/kirillDanshin) of [fasthttp](https://github.com/valyala/fasthttp). 22 - Gramework is one of the rare frameworks that can help you use your server's resources more efficiently. 23 - Gramework lowers your infrastructure costs by using as little memory as possible. 24 - Gramework helps you serve requests faster, and so it helps you increase conversions ([source 1](https://blog.kissmetrics.com/speed-is-a-killer/), [source 2](https://blog.hubspot.com/marketing/page-load-time-conversion-rates)). 25 - With Gramework you can build software faster using a simple yet powerful and highly optimized API. 26 - With Gramework you get enterprise-grade support and answers to all your questions. 27 - At the Gramework team, we respect our users. 28 - You can directly contact the [maintainer](https://github.com/kirillDanshin) and [donate](https://opencollective.com/gramework) for high priority feature. 29 - You can be sure that all license questions are OK with gramework. 30 31 **Go >= 1.10.8 is the oldest continously tested and supported version.** 32 33 34 ### Useful links and info 35 If you encounter any vulnerabilities then please feel free to submit them via k@gramework.win. 36 37 | Name | Link/Badge | 38 |--- |--- | 39 | Docs | [GoDoc](https://godoc.org/github.com/gramework/gramework) | 40 | Our Jira | [Jira](https://gramework.atlassian.net) | 41 | License Report | [Report](https://github.com/gramework/gramework/tree/dev/third_party_licenses/REPORT.md) | 42 | Changelog | [Changelog](https://github.com/gramework/gramework/tree/dev/docs/CHANGELOG.md) | 43 | Support us with a donation or become a sponsor | [OpenCollective](https://opencollective.com/gramework) | 44 | Our Telegram chat | [@gramework](https://t.me/gramework) | 45 | Our #gramework channel in the Gophers Slack | https://gophers.slack.com | 46 | Our Discord Server | https://discord.gg/HkW8DsD | 47 | Master branch coverage | [![codecov](https://codecov.io/gh/gramework/gramework/branch/master/graph/badge.svg)](https://codecov.io/gh/gramework/gramework) | 48 | Master branch status | [![Build Status](https://travis-ci.org/gramework/gramework.svg?branch=master)](https://travis-ci.org/gramework/gramework) | 49 | Dev branch coverage | [![codecov](https://codecov.io/gh/gramework/gramework/branch/dev/graph/badge.svg)](https://codecov.io/gh/gramework/gramework) | 50 | Dev branch status | [![Build Status](https://travis-ci.org/gramework/gramework.svg?branch=dev)](https://travis-ci.org/gramework/gramework) | 51 | CII Best Practices | [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/1203/badge)](https://bestpractices.coreinfrastructure.org/projects/1203) | 52 | Gramework Stats Dashboard for Grafana | https://grafana.com/dashboards/3422 | 53 | **Support contacts** | Via email: k@gramework.win | 54 | | Via Telegram community: [@gramework](https://t.me/gramework) | 55 56 # Table of Contents 57 - [Benchmarks](#benchmarks) 58 - [3rd-party license info](#3rd-party-license-info) 59 - [Basic usage](#basic-usage) 60 - [Hello world](#hello-world) 61 - [Serving a dir](#serving-a-dir) 62 - [Serving prepared bytes](#serving-prepared-bytes) 63 - [Using dynamic handlers, example 1](#using-dynamic-handlers-example-1) 64 - [Using dynamic handlers, example 2](#using-dynamic-handlers-example-2) 65 66 # Benchmarks 67 [![benchmark](https://raw.githubusercontent.com/smallnest/go-web-framework-benchmark/master/benchmark.png)](https://github.com/smallnest/go-web-framework-benchmark) 68 69 ## Contributors 70 This project exists thanks to our awesome contributors! [[Contribute](CONTRIBUTING.md)]. 71 <a href="graphs/contributors"><img src="https://opencollective.com/gramework/contributors.svg?width=890&button=false" /></a> 72 73 ## Backers 74 Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/gramework#backer)] 75 76 <a href="https://opencollective.com/gramework#backers" target="_blank"><img src="https://opencollective.com/gramework/backers.svg?width=890"></a> 77 78 ## Sponsors 79 Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/gramework)] 80 81 <a href="https://opencollective.com/gramework/sponsor/0/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/0/avatar.svg"></a> 82 <a href="https://opencollective.com/gramework/sponsor/1/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/1/avatar.svg"></a> 83 <a href="https://opencollective.com/gramework/sponsor/2/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/2/avatar.svg"></a> 84 <a href="https://opencollective.com/gramework/sponsor/3/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/3/avatar.svg"></a> 85 <a href="https://opencollective.com/gramework/sponsor/4/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/4/avatar.svg"></a> 86 <a href="https://opencollective.com/gramework/sponsor/5/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/5/avatar.svg"></a> 87 <a href="https://opencollective.com/gramework/sponsor/6/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/6/avatar.svg"></a> 88 <a href="https://opencollective.com/gramework/sponsor/7/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/7/avatar.svg"></a> 89 <a href="https://opencollective.com/gramework/sponsor/8/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/8/avatar.svg"></a> 90 <a href="https://opencollective.com/gramework/sponsor/9/website" target="_blank"><img src="https://opencollective.com/gramework/sponsor/9/avatar.svg"></a> 91 92 # 3rd-party license info 93 - Gramework is now powered by [fasthttp](https://github.com/valyala/fasthttp) and an embedded custom fasthttprouter. 94 You will find the according licenses in `/third_party_licenses/fasthttp` and `/third_party_licenses/fasthttprouter`. 95 - The 3rd autoTLS implementation, placed in `nettls_*.go`, is an integrated version of 96 [caddytls](https://github.com/mholt/caddy/tree/d85e90a7b4c06d1698d0b96b695b05d41833fcd3/caddytls), because using it through a simple import isn't an option, gramework is based on `fasthttp`, which is incompatible with `net/http`. 97 In [the commit I based on](https://github.com/mholt/caddy/tree/d85e90a7b4c06d1698d0b96b695b05d41833fcd3), caddy is `Apache-2.0` licensed. 98 Its license placed in `/third_party_licenses/caddy`. @mholt [allow us](https://github.com/mholt/caddy/issues/1520#issuecomment-286907851) to copy the code in this repo. 99 100 101 [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fgramework%2Fgramework.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fgramework%2Fgramework?ref=badge_large) 102 103 # Basic usage 104 ### Hello world 105 The example below will serve "hello, grameworld". Gramework will register the `bind` flag for you, that allows you to choose another ip/port that gramework should listen on: 106 107 ```go 108 package main 109 110 import ( 111 "github.com/gramework/gramework" 112 ) 113 114 func main() { 115 app := gramework.New() 116 117 app.GET("/", "hello, grameworld") 118 119 app.ListenAndServe() 120 } 121 ``` 122 123 If you don't want to support the `bind` flag then pass the optional address argument to `ListenAndServe`. 124 125 **NOTE**: all examples below will register the `bind` flag. 126 127 ### JSON world ;) Part 1 128 From version: 1.1.0-rc1 129 130 The example below will serve `{"hello":"grameworld"}` from the map. Gramework will register the `bind` flag for you, that allows you to choose another ip/port that gramework should listen on: 131 132 ```go 133 package main 134 135 import ( 136 "github.com/gramework/gramework" 137 ) 138 139 func main() { 140 app := gramework.New() 141 142 app.GET("/", func() map[string]interface{} { 143 return map[string]interface{}{ 144 "hello": "gramework", 145 } 146 }) 147 148 app.ListenAndServe() 149 } 150 ``` 151 152 ### JSON world. Part 2 153 From version: 1.1.0-rc1 154 155 The example below will serve `{"hello":"grameworld"}` from the struct. Gramework will register the `bind` flag for you, that allows you to choose another ip/port that gramework should listen on: 156 157 ```go 158 package main 159 160 import ( 161 "github.com/gramework/gramework" 162 ) 163 164 type SomeResponse struct { 165 hello string 166 } 167 168 func main() { 169 app := gramework.New() 170 171 app.GET("/", func() interface{} { 172 return SomeResponse{ 173 hello: "gramework", 174 } 175 }) 176 177 app.ListenAndServe() 178 } 179 ``` 180 181 ### Serving a dir 182 The example below will serve static files from `./files`: 183 184 ```go 185 package main 186 187 import ( 188 "github.com/gramework/gramework" 189 ) 190 191 func main() { 192 app := gramework.New() 193 194 app.GET("/*any", app.ServeDir("./files")) 195 196 app.ListenAndServe() 197 } 198 ``` 199 200 ### Serving prepared bytes 201 The example below will serve a byte slice: 202 203 ```go 204 package main 205 206 import ( 207 "fmt" 208 "os" 209 "time" 210 211 "github.com/gramework/gramework" 212 ) 213 214 type SomeData struct { 215 Name string 216 Age uint8 217 } 218 219 func main() { 220 app := gramework.New() 221 222 d := SomeData{ 223 Name: "Grame", 224 Age: 20, 225 } 226 227 // service-wide CORS. you can also instead of using middleware 228 // call ctx.CORS() manually 229 app.Use(app.CORSMiddleware()) 230 231 app.GET("/someJSON", func(ctx *gramework.Context) { 232 // send json, no metter if user asked for json, xml or anything else. 233 if err := ctx.JSON(d); err != nil { 234 // you can return err instead of manual checks and Err500() call. 235 // See next handler for example. 236 ctx.Err500() 237 } 238 }) 239 240 app.GET("/simpleJSON", func(ctx *gramework.Context) error { 241 return ctx.JSON(d) 242 }) 243 244 app.GET("/someData", func(ctx *gramework.Context) error { 245 // send data in one of supported encodings user asked for. 246 // Now we support json, xml and csv. More coming soon. 247 sentType, err := ctx.Encode(d) 248 if err != nil { 249 ctx.Logger.WithError(err).Error("could not process request") 250 return err 251 } 252 ctx.Logger.WithField("sentType", sentType).Debug("some request-related message") 253 return nil 254 }) 255 256 // you can omit context if you want, return `interface{}`, `error` or both. 257 app.GET("/simplestJSON", func() interface{} { 258 return d 259 }) 260 261 // you can also use one of built-in types as a handler, we got you covered too 262 app.GET("/hostnameJSON", fmt.Sprintf(`{"hostname": %q}`, os.Hostname())) 263 264 wait := make(chan struct{}) 265 go func() { 266 time.Sleep(10 * time.Minute) 267 app.Shutdown() 268 wait <- struct{}{} 269 }() 270 271 app.ListenAndServe() 272 273 // allow Shutdown() to stop the app properly. 274 // ListenAndServe will return before Shutdown(), so we should wait. 275 <-wait 276 } 277 ``` 278 279 ### Using dynamic handlers, example 2. Simple FastHTTP-compatible handlers. 280 This example demonstrates how to migrate from fasthttp to gramework 281 without rewriting your handlers. 282 283 ```go 284 package main 285 286 import ( 287 "github.com/gramework/gramework" 288 "github.com/valyala/fasthttp" 289 ) 290 291 func main() { 292 app := gramework.New() 293 294 app.GET("/someJSON", func(ctx *fasthttp.RequestCtx) { 295 ctx.WriteString("another data") 296 }) 297 298 app.ListenAndServe() 299 } 300 ```