iris/_examples
Gerasimos (Makis) Maropoulos 0b2dcc76f5 start of the vue + mvc example and a simple session binding
Former-commit-id: 994f00952352c93d270ad197cb843f3222fb37c0
2017-12-14 05:56:23 +02:00
..
apidoc/yaag Update to version 8.5.6 | Read HISTORY.md 2017-11-05 04:12:18 +02:00
authentication Update to version 8.5.6 | Read HISTORY.md 2017-11-05 04:12:18 +02:00
cache/simple Update to version 8.5.5 2017-11-02 05:50:56 +02:00
configuration Add a useful(?) introduction README.md section to some examples 2017-11-07 19:12:30 +02:00
convert-handlers Add an example on how to use third-party handlers that are not compatible to the "next pattern" 2017-10-13 06:59:31 +03:00
experimental-handlers move csrf middleware example 2017-11-21 11:38:55 +02:00
file-server give some more control over request params and their entries for the end-developers 2017-11-23 12:30:13 +02:00
hello-world add content type and response text to the Controller 💯 2017-09-02 14:32:14 +03:00
http_request Update to version 8.5.5 2017-11-02 05:50:56 +02:00
http_responsewriter give some more control over request params and their entries for the end-developers 2017-11-23 12:30:13 +02:00
http-listening made it work but looking for another approach 2017-12-04 05:06:03 +02:00
miscellaneous update test to cover the multi files per language new feature - requested at: https://github.com/kataras/iris/issues/815 2017-11-22 01:08:26 +02:00
mvc made it work but looking for another approach 2017-12-04 05:06:03 +02:00
orm/xorm 8.4.0 | New MVC Features | Refactor examples and godoc for go 1.9 use. Read HISTORY.md. 2017-08-27 20:35:23 +03:00
overview fix https://github.com/kataras/iris/issues/802 2017-11-07 12:16:52 +02:00
routing Add a useful(?) introduction README.md section to some examples 2017-11-07 19:12:30 +02:00
sessions Add a useful(?) introduction README.md section to some examples 2017-11-07 19:12:30 +02:00
structuring give some more control over request params and their entries for the end-developers 2017-11-23 12:30:13 +02:00
subdomains Update to version 8.5.5 2017-11-02 05:50:56 +02:00
testing/httptest 8.4.0 | New MVC Features | Refactor examples and godoc for go 1.9 use. Read HISTORY.md. 2017-08-27 20:35:23 +03:00
tutorial start of the vue + mvc example and a simple session binding 2017-12-14 05:56:23 +02:00
view Update to version 8.5.6 | Read HISTORY.md 2017-11-05 04:12:18 +02:00
websocket give some more control over request params and their entries for the end-developers 2017-11-23 12:30:13 +02:00
README.md ok the new mvc system works great, all tests done and almost x3 smaller LOC used plus new three awesome features:) - next commit will be commenting out and replace the mvc package with the new mvc2 2017-12-10 07:00:51 +02: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

Structuring

Nothing stops you from using your favorite folder structure. Iris is a low level web framework, it has got MVC first-class support but it doesn't limit your folder structure, this is your choice.

Structuring depends on your own needs. We can't tell you how to design your own application for sure but you're free to take a closer look to the examples below; you may find something useful that you can borrow for your app;

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

Register one or more relative paths and able to get path parameters, i.e

If app.Controller("/user", new(user.Controller))

  • func(*Controller) Get() - GET:/user , as usual.
  • func(*Controller) Post() - POST:/user, as usual.
  • func(*Controller) GetLogin() - GET:/user/login
  • func(*Controller) PostLogin() - POST:/user/login
  • func(*Controller) GetProfileFollowers() - GET:/user/profile/followers
  • func(*Controller) PostProfileFollowers() - POST:/user/profile/followers
  • func(*Controller) GetBy(id int64) - GET:/user/{param:long}
  • func(*Controller) PostBy(id int64) - POST:/user/{param:long}

If app.Controller("/profile", new(profile.Controller))

  • func(*Controller) GetBy(username string) - GET:/profile/{param:string}

If app.Controller("/assets", new(file.Controller))

  • func(*Controller) GetByWildard(path string) - GET:/assets/{param:path}

    Supported types for method functions receivers: int, int64, bool and string.

Response via output arguments, optionally, i.e

func(c *ExampleController) Get() string |
                                (string, string) |
                                (string, int) |
                                int |
                                (int, string) |
                                (string, error) |
                                bool |
                                (any, bool) |
                                (bool, any) |
                                error |
                                (int, error) |
                                (customStruct, error) |
                                customStruct |
                                (customStruct, int) |
                                (customStruct, string) |
                                mvc.Result or (mvc.Result, error) and so on...

where mvc.Result is an interface which contains only that function: Dispatch(ctx iris.Context).

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

Experimental Handlers

More

https://github.com/kataras/iris/tree/master/middleware#third-party-handlers

Automated API Documentation

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 and https://docs.iris-go.com 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!