iris/_examples
kataras 9232b96188 Update iris.AutoTLS example, read description.
Iris devs should declare all the information now, there is no option to "leave something out" anymore, it's for your own good. Version is not changed yet, giving you time to see that changelog and do the necessary changes to your codebase, it will written to HISTORY.md too of course.

Before:
app.Run(iris.AutoTLS(":443"))

Now:
app.Run(iris.AutoTLS(":443", "example.com", "mail@example.com")

Commit Change: f7b655f145 [formerly f5314a8c1f8303d7216481d05129eb8a62766e14]

Happy weekend!


Former-commit-id: bf974dc486d05ff008c9ed13aaf96e716b1e55c6
2017-08-26 01:49:23 +03:00
..
authentication Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded 2017-07-10 18:32:42 +03:00
cache/simple move sessions and websocket examples, gofmt, fix misspells and experimental optimizations 🍐 2017-07-22 22:57:20 +03:00
configuration Update to 8.2.0 | BoltDB session database, fix file sessiondb, faster, simpler and improvement Session Database API 2017-08-07 06:04:35 +03:00
convert-handlers Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded 2017-07-10 18:32:42 +03:00
file-server Document https://github.com/kataras/iris/issues/720 and fix https://github.com/kataras/iris/issues/719 for good 2017-08-13 01:47:19 +03:00
hello-world fix some typos 2017-08-24 15:51:43 +03:00
http_request Update to 8.1.0 - a new logger implemented as a solution for https://github.com/kataras/iris/issues/680 2017-07-26 15:30:20 +03:00
http_responsewriter move sessions and websocket examples, gofmt, fix misspells and experimental optimizations 🍐 2017-07-22 22:57:20 +03:00
http-listening Update iris.AutoTLS example, read description. 2017-08-26 01:49:23 +03:00
miscellaneous Add example for Google reCAPTCAA middleware. Prev Commit: Update to 8.2.2. Read HISTORY.md 2017-08-10 15:41:59 +03:00
mvc fix some typos 2017-08-24 15:51:43 +03:00
orm/xorm Add xorm example 2017-08-11 04:11:20 +03:00
overview fix some typos 2017-08-24 15:51:43 +03:00
routing add a nested parties and wildcard subdomains test 2017-08-23 15:26:46 +03:00
sessions Update to 8.2.6 | More on Iris Controllers: optional EndRequest. Read HISTORY.md 2017-08-14 16:21:51 +03:00
subdomains Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded 2017-07-10 18:32:42 +03:00
testing/httptest Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded 2017-07-10 18:32:42 +03:00
tutorial Update to 8.3.0 | MVC Models and Bindings and fix of #723 , read HISTORY.md 2017-08-18 17:09:18 +03:00
view Update to 8.2.5 | Say Hello to Controllers. Read HISTORY.md 2017-08-13 21:58:34 +03:00
websocket Update to 8.3.0 | MVC Models and Bindings and fix of #723 , read HISTORY.md 2017-08-18 17:09:18 +03:00
README.md Update to 8.3.3 | Even better MVC. Read HISTORY.md 2017-08-23 01:11:52 +03:00

Examples

Please do learn how net/http std package works, first.

This folder provides easy to understand code snippets on how to get started with 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

HTTP Listening

Configuration

Routing, Grouping, Dynamic Path Parameters, "Macros" and Custom Context

  • 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:

// 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.

MVC

Iris has first-class support for the MVC (Model View Controller) pattern, you'll not find these stuff anywhere else in the Go world.

Iris web framework supports Request data, Models, Persistence Data and Binding with the fastest possible execution.

Characteristics

All HTTP Methods are supported, for example if want to serve GET then the controller should have a function named Get(), you can define more than one method function to serve in the same Controller struct.

Persistence data inside your Controller struct (share data between requests) via iris:"persistence" tag right to the field or Bind using app.Controller("/" , new(myController), theBindValue).

Models inside your Controller struct (set-ed at the Method function and rendered by the View) via iris:"model" tag right to the field, i.e User UserModel `iris:"model" name:"user"` view will recognise it as {{.user}}. If name tag is missing then it takes the field's name, in this case the "User".

Access to the request path and its parameters via the Path and Params fields.

Access to the template file that should be rendered via the Tmpl field.

Access to the template data that should be rendered inside the template file via Data field.

Access to the template layout via the Layout field.

Access to the low-level context.Context via the Ctx field.

Flow as you used to, Controllers can be registered to any Party, including Subdomains, the Party's begin and done handlers work as expected.

Optional BeginRequest(ctx) function to perform any initialization before the method execution, useful to call middlewares or when many methods use the same collection of data.

Optional EndRequest(ctx) function to perform any finalization after any method executed.

Inheritance, see for example our mvc.SessionController, it has the mvc.Controller as an embedded field and it adds its logic to its BeginRequest, here.

Using Iris MVC for code reuse

By creating components that are independent of one another, developers are able to reuse components quickly and easily in other applications. The same (or similar) view for one application can be refactored for another application with different data because the view is simply handling how the data is being displayed to the user.

If you're new to back-end web development read about the MVC architectural pattern first, a good start is that wikipedia article.

Follow the examples below,

Subdomains

Convert http.Handler/HandlerFunc

View

Engine Declaration
template/html iris.HTML(...)
django iris.Django(...)
handlebars iris.Handlebars(...)
amber iris.Amber(...)
pug(jade) iris.Pug(...)

You can serve quicktemplate files too, simply by using the context#ResponseWriter, take a look at the http_responsewriter/quicktemplate example.

Authentication

File Server

How to Read from context.Request() *http.Request

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

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.

ORM

Miscellaneous

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.

Example

Caching

iris cache library lives on its own package.

You're free to use your own favourite caching package if you'd like so.

Sessions

iris session manager lives on its own package.

You're free to use your own favourite sessions package if you'd like so.

Websockets

iris websocket library lives on its own package.

The package is designed to work with raw websockets although its API is similar to the famous socket.io. I have read an article recently and I felt very contented about my decision to design a fast websocket-only package for Iris and not a backwards socket.io-like package. You can read that article by following this link: https://medium.com/@ivanderbyl/why-you-don-t-need-socket-io-6848f1c871cd.

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 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 for a better understanding.

Psst, I almost forgot; do not forget to star or watch the project in order to stay updated with the latest tech trends, it never takes more than a second!