2017-03-23 23:22:05 +01:00
# Examples
2017-03-13 14:16:12 +01:00
2017-07-10 17:32:42 +02:00
Please do learn how [net/http ](https://golang.org/pkg/net/http/ ) std package works, first.
This folder provides easy to understand code snippets on how to get started with [iris ](https://github.com/kataras/iris ) micro web framework.
It doesn't always contain the "best ways" but it does cover each important feature that will make you so excited to GO with iris!
### Overview
- [Hello world! ](hello-world/main.go )
- [Glimpse ](overview/main.go )
- [Tutorial: Online Visitors ](tutorial/online-visitors/main.go )
2017-07-16 12:58:10 +02:00
- [Tutorial: URL Shortener using BoltDB ](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7 )
2017-08-13 06:57:47 +02:00
- [Tutorial: How to turn your Android Device into a fully featured Web Server (**MUST**) ](https://twitter.com/ThePracticalDev/status/892022594031017988 )
- [Tutorial: Controllers from scratch (**Coming soon as built'n feature, probably at v8.3**) ](tutorial/mvc )
2017-07-10 17:32:42 +02:00
### HTTP Listening
- [Common, with address ](http-listening/listen-addr/main.go )
2017-07-14 05:19:58 +02:00
* [omit server errors ](http-listening/listen-addr/omit-server-errors/main.go )
2017-07-10 17:32:42 +02:00
- [UNIX socket file ](http-listening/listen-unix/main.go )
- [TLS ](http-listening/listen-tls/main.go )
- [Letsencrypt (Automatic Certifications) ](http-listening/listen-letsencrypt/main.go )
2017-07-29 03:39:30 +02:00
- [Notify on shutdown ](http-listening/notify-on-shutdown/main.go )
2017-07-10 17:32:42 +02:00
- Custom TCP Listener
* [common net.Listener ](http-listening/custom-listener/main.go )
* [SO_REUSEPORT for unix systems ](http-listening/custom-listener/unix-reuseport/main.go )
- Custom HTTP Server
2017-07-16 12:58:10 +02:00
* [easy way ](http-listening/custom-httpserver/easy-way/main.go )
2017-07-10 17:32:42 +02:00
* [std way ](http-listening/custom-httpserver/std-way/main.go )
* [multi server instances ](http-listening/custom-httpserver/multi/main.go )
- Graceful Shutdown
* [using the `RegisterOnInterrupt` ](http-listening/graceful-shutdown/default-notifier/main.go )
* [using a custom notifier ](http-listening/graceful-shutdown/custom-notifier/main.go )
2017-07-29 03:39:30 +02:00
2017-07-29 03:27:58 +02:00
2017-07-10 17:32:42 +02:00
### Configuration
- [Functional ](configuration/functional/main.go )
2017-07-15 16:40:29 +02:00
- [From Configuration Struct ](configuration/from-configuration-structure/main.go )
2017-07-10 17:32:42 +02:00
- [Import from YAML file ](configuration/from-yaml-file/main.go )
- [Import from TOML file ](configuration/from-toml-file/main.go )
### Routing, Grouping, Dynamic Path Parameters, "Macros" and Custom Context
2017-07-15 16:40:29 +02:00
* `app.Get("{userid:int min(1)}", myHandler)`
* `app.Post("{asset:path}", myHandler)`
* `app.Put("{custom:string regexp([a-z]+)}", myHandler)`
Note: unlike other routers you'd seen, iris' router can handle things like these:
```go
// Matches all GET requests prefixed with "/assets/"
app.Get("/assets/{asset:path}", assetsWildcardHandler)
// Matches only GET "/"
app.Get("/", indexHandler)
// Matches only GET "/about"
app.Get("/about", aboutHandler)
// Matches all GET requests prefixed with "/profile/"
// and followed by a single path part
app.Get("/profile/{username:string}", userHandler)
// Matches only GET "/profile/me" because
// it does not conflict with /profile/{username:string}
// or the root wildcard {root:path}
app.Get("/profile/me", userHandler)
// Matches all GET requests prefixed with /users/
// and followed by a number which should be equal or bigger than 1
app.Get("/user/{userid:int min(1)}", getUserHandler)
// Matches all requests DELETE prefixed with /users/
// and following by a number which should be equal or bigger than 1
app.Delete("/user/{userid:int min(1)}", deleteUserHandler)
// Matches all GET requests except "/", "/about", anything starts with "/assets/" etc...
// because it does not conflict with the rest of the routes.
app.Get("{root:path}", rootWildcardHandler)
```
Navigate through examples for a better understanding.
2017-07-10 17:32:42 +02:00
- [Overview ](routing/overview/main.go )
- [Basic ](routing/basic/main.go )
- [Custom HTTP Errors ](routing/http-errors/main.go )
- [Dynamic Path ](routing/dynamic-path/main.go )
2017-07-16 12:58:10 +02:00
* [root level wildcard path ](routing/dynamic-path/root-wildcard/main.go )
2017-07-10 17:32:42 +02:00
- [Reverse routing ](routing/reverse/main.go )
- [Custom wrapper ](routing/custom-wrapper/main.go )
- Custom Context
2017-07-16 12:58:10 +02:00
* [method overriding ](routing/custom-context/method-overriding/main.go )
* [new implementation ](routing/custom-context/new-implementation/main.go )
2017-07-10 17:32:42 +02:00
- [Route State ](routing/route-state/main.go )
### Subdomains
- [Single ](subdomains/single/main.go )
- [Multi ](subdomains/multi/main.go )
- [Wildcard ](subdomains/wildcard/main.go )
- [WWW ](subdomains/www/main.go )
### Convert `http.Handler/HandlerFunc`
- [From func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) ](convert-handlers/negroni-like/main.go )
- [From http.Handler or http.HandlerFunc ](convert-handlers/nethttp/main.go )
### View
| Engine | Declaration |
| -----------|-------------|
| template/html | `iris.HTML(...)` |
| django | `iris.Django(...)` |
| handlebars | `iris.Handlebars(...)` |
| amber | `iris.Amber(...)` |
| pug(jade) | `iris.Pug(...)` |
- [Overview ](view/overview/main.go )
- [Hi ](view/template_html_0/main.go )
- [A simple Layout ](view/template_html_1/main.go )
- [Layouts: `yield` and `render` tmpl funcs ](view/template_html_2/main.go )
- [The `urlpath` tmpl func ](view/template_html_3/main.go )
- [The `url` tmpl func ](view/template_html_4/main.go )
- [Inject Data Between Handlers ](view/context-view-data/main.go )
- [Embedding Templates Into App Executable File ](view/embedding-templates-into-app/main.go )
2017-07-18 20:17:23 +02:00
You can serve [quicktemplate ](https://github.com/valyala/quicktemplate ) files too, simply by using the `context#ResponseWriter` , take a look at the [http_responsewriter/quicktemplate ](http_responsewriter/quicktemplate ) example.
2017-07-10 17:32:42 +02:00
### Authentication
- [Basic Authentication ](authentication/basicauth/main.go )
- [OAUth2 ](authentication/oauth2/main.go )
- [JWT ](https://github.com/iris-contrib/middleware/blob/master/jwt/_example/main.go )
- [Sessions ](#sessions )
### File Server
- [Favicon ](file-server/favicon/main.go )
- [Basic ](file-server/basic/main.go )
- [Embedding Files Into App Executable File ](file-server/embedding-files-into-app/main.go )
- [Send/Force-Download Files ](file-server/send-files/main.go )
- Single Page Applications
2017-07-16 12:58:10 +02:00
* [single Page Application ](file-server/single-page-application/basic/main.go )
* [embedded Single Page Application ](file-server/single-page-application/embedded-single-page-application/main.go )
2017-07-10 17:32:42 +02:00
### How to Read from `context.Request() *http.Request`
- [Bind JSON ](http_request/read-json/main.go )
- [Bind Form ](http_request/read-form/main.go )
- [Upload/Read Files ](http_request/upload-files/main.go )
> The `context.Request()` returns the same *http.Request you already know, these examples show some places where the Context uses this object. Besides that you can use it as you did before iris.
### How to Write to `context.ResponseWriter() http.ResponseWriter`
2017-07-18 20:17:23 +02:00
- [Write `valyala/quicktemplate` templates ](http_responsewriter/quicktemplate )
2017-07-10 17:32:42 +02:00
- [Text, Markdown, HTML, JSON, JSONP, XML, Binary ](http_responsewriter/write-rest/main.go )
- [Stream Writer ](http_responsewriter/stream-writer/main.go )
- [Transactions ](http_responsewriter/transactions/main.go )
> The `context.ResponseWriter()` returns an enchament version of a http.ResponseWriter, these examples show some places where the Context uses this object. Besides that you can use it as you did before iris.
2017-08-11 03:11:20 +02:00
### ORM
- [Using xorm(Mysql, MyMysql, Postgres, Tidb, **SQLite**, MsSql, MsSql, Oracle) ](orm/xorm/main.go )
2017-07-10 17:32:42 +02:00
### Miscellaneous
- [Request Logger ](http_request/request-logger/main.go )
2017-07-16 12:58:10 +02:00
* [log requests to a file ](http_request/request-logger/request-logger-file/main.go )
2017-07-10 17:32:42 +02:00
- [Localization and Internationalization ](miscellaneous/i18n/main.go )
- [Recovery ](miscellaneous/recover/main.go )
- [Profiling (pprof) ](miscellaneous/pprof/main.go )
- [Internal Application File Logger ](miscellaneous/file-logger/main.go )
2017-08-10 14:41:59 +02:00
- [Google reCAPTCHA ](miscellaneous/recaptcha/main.go )
2017-07-10 17:32:42 +02:00
#### More
https://github.com/kataras/iris/tree/master/middleware#third-party-handlers
### Testing
The `httptest` package is your way for end-to-end HTTP testing, it uses the httpexpect library created by our friend, [gavv ](https://github.com/gavv ).
[Example ](testing/httptest/main_test.go )
### Caching
2017-07-22 21:57:20 +02:00
iris cache library lives on its own [package ](https://github.com/kataras/iris/tree/master/cache ).
- [Simple ](cache/simple/main.go )
> You're free to use your own favourite caching package if you'd like so.
2017-07-10 17:32:42 +02:00
### Sessions
2017-08-13 06:57:47 +02:00
2017-07-22 21:57:20 +02:00
iris session manager lives on its own [package ](https://github.com/kataras/iris/tree/master/sessions ).
2017-07-10 17:32:42 +02:00
2017-07-22 21:57:20 +02:00
- [Overview ](sessions/overview/main.go )
- [Standalone ](sessions/standalone/main.go )
- [Secure Cookie ](sessions/securecookie/main.go )
- [Flash Messages ](sessions/flash-messages/main.go )
2017-08-07 05:04:35 +02:00
- [Databases ](sessions/database )
2017-08-02 20:52:53 +02:00
* [File ](sessions/database/file/main.go )
2017-08-07 05:04:35 +02:00
* [BoltDB ](sessions/database/boltdb/main.go )
* [Redis ](sessions/database/redis/main.go )
2017-07-10 17:32:42 +02:00
> You're free to use your own favourite sessions package if you'd like so.
### Websockets
2017-07-22 21:57:20 +02:00
iris websocket library lives on its own [package ](https://github.com/kataras/iris/tree/master/websocket ).
- [Chat ](websocket/chat/main.go )
- [Native Messages ](websocket/native-messages/main.go )
- [Connection List ](websocket/connectionlist/main.go )
- [TLS Enabled ](websocket/secure/main.go )
- [Custom Raw Go Client ](websocket/custom-go-client/main.go )
2017-07-10 17:32:42 +02:00
> You're free to use your own favourite websockets package if you'd like so.
### Typescript Automation Tools
typescript automation tools have their own repository: [https://github.com/kataras/iris/tree/master/typescript ](https://github.com/kataras/iris/tree/master/typescript ) **it contains examples**
> I'd like to tell you that you can use your favourite but I don't think you will find such a thing anywhere else.
### Hey, You!
Developers should read the [godocs ](https://godoc.org/github.com/kataras/iris ) for a better understanding.
Psst, I almost forgot; do not forget to [star or watch ](https://github.com/kataras/iris/stargazers ) the project in order to stay updated with the latest tech trends, it never takes more than a second!