_examples/

directory
v0.0.0-...-c8e73f4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2019 License: BSD-3-Clause

README

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

Running the examples

  1. Install the Go Programming Language, version 1.12+ from https://golang.org/dl.
  2. Install Iris
  3. Install any external packages that required by the examples
External packages
cd _examples && go get ./...

And run each example you wanna see, e.g.

$ cd $GOPATH/src/github.com/kataras/iris/_examples/overview
$ go run main.go

Test the examples by opening a terminal window and execute: go test -v ./...

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.

Versioning
Dependency Injection
MVC
Subdomains
Convert http.Handler/HandlerFunc
View

You can serve quicktemplate and hero templates files too, simply by using the context#ResponseWriter, take a look at the http_responsewriter/quicktemplate and http_responsewriter/herotemplate examples.

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.

Cookies
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
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!

Directories

Path Synopsis
apidoc
yaag command
authentication
basicauth command
oauth2 command
cache
client-side command
Package main shows how you can use the `WriteWithExpiration` based on the "modtime", if it's newer than the request header then it will refresh the contents, otherwise will let the client (99.9% the browser) to handle the cache mechanism, it's faster than iris.Cache because server-side has nothing to do and no need to store the responses in the memory.
Package main shows how you can use the `WriteWithExpiration` based on the "modtime", if it's newer than the request header then it will refresh the contents, otherwise will let the client (99.9% the browser) to handle the cache mechanism, it's faster than iris.Cache because server-side has nothing to do and no need to store the responses in the memory.
simple command
configuration
from-toml-file command
from-yaml-file command
functional command
convert-handlers
negroni-like command
nethttp command
cookies
basic command
securecookie command
dependency-injection
jwt/contrib module
desktop
webview module
experimental-handlers
casbin/wrapper command
cors/simple command
csrf command
This middleware provides Cross-Site Request Forgery protection.
This middleware provides Cross-Site Request Forgery protection.
jwt command
iris provides some basic middleware, most for your learning curve.
iris provides some basic middleware, most for your learning curve.
newrelic/simple command
secure/simple command
file-server
basic command
favicon command
send-files command
hero
basic command
overview command
sessions command
smart-contract command
http-listening
custom-listener command
listen-addr command
listen-letsencrypt command
Package main provide one-line integration with letsencrypt.org
Package main provide one-line integration with letsencrypt.org
listen-tls command
listen-unix command
http-server
h2c module
http3-quic module
http_request
extract-referer command
read-form command
package main contains an example on how to use the ReadForm, but with the same way you can do the ReadJSON & ReadJSON
package main contains an example on how to use the ReadForm, but with the same way you can do the ReadJSON & ReadJSON
read-json command
read-json-struct-validation command
Package main shows the validator(latest, version 9) integration with Iris.
Package main shows the validator(latest, version 9) integration with Iris.
read-many command
read-query command
package main contains an example on how to use the ReadForm, but with the same way you can do the ReadJSON & ReadJSON
package main contains an example on how to use the ReadForm, but with the same way you can do the ReadJSON & ReadJSON
read-xml command
read-yaml command
request-logger command
upload-file command
upload-files command
http_responsewriter
content-negotiation command
Package main contains three different ways to render content based on the client's accepted.
Package main contains three different ways to render content based on the client's accepted.
herotemplate command
herotemplate/template
Code generated by hero.
Code generated by hero.
quicktemplate command
sse command
Package main shows how to send continuous event messages to the clients through SSE via a broker.
Package main shows how to send continuous event messages to the clients through SSE via a broker.
sse-third-party command
stream-writer command
transactions command
write-gzip command
write-rest command
miscellaneous
file-logger command
i18n command
pprof command
recaptcha command
recover command
mvc
basic command
error-handler command
hello-world command
login command
middleware command
Package main shows how you can add middleware to an mvc Application, simply by using its `Router` which is a sub router(an iris.Party) of the main iris app.
Package main shows how you can add middleware to an mvc Application, simply by using its `Router` which is a sub router(an iris.Party) of the main iris app.
middleware/per-method command
If you want to use it as middleware for the entire controller you can use its router which is just a sub router to add it as you normally do with standard API: I'll show you 4 different methods for adding a middleware into an mvc application, all of those 4 do exactly the same thing, select what you prefer, I prefer the last code-snippet when I need the middleware to be registered somewhere else as well, otherwise I am going with the first one: “`go // 1 mvc.Configure(app.Party("/user"), func(m *mvc.Application) { m.Router.Use(cache.Handler(10*time.Second)) }) “` “`go // 2 // same: userRouter := app.Party("/user") userRouter.Use(cache.Handler(10*time.Second)) mvc.Configure(userRouter, ...) “` “`go // 3 // same: userRouter := app.Party("/user", cache.Handler(10*time.Second)) mvc.Configure(userRouter, ...) “` “`go // 4 // same: app.PartyFunc("/user", func(r iris.Party){ r.Use(cache.Handler(10*time.Second)) mvc.Configure(r, ...) }) “` If you want to use a middleware for a single route, for a single controller's method that is already registered by the engine and not by custom `Handle` (which you can add the middleware there on the last parameter) and it's not depend on the `Next Handler` to do its job then you just call it on the method: “`go var myMiddleware := myMiddleware.New(...) // this should return an iris/context.Handler type UserController struct{} func (c *UserController) GetSomething(ctx iris.Context) { // ctx.Proceed checks if myMiddleware called `ctx.Next()` // inside it and returns true if so, otherwise false.
If you want to use it as middleware for the entire controller you can use its router which is just a sub router to add it as you normally do with standard API: I'll show you 4 different methods for adding a middleware into an mvc application, all of those 4 do exactly the same thing, select what you prefer, I prefer the last code-snippet when I need the middleware to be registered somewhere else as well, otherwise I am going with the first one: “`go // 1 mvc.Configure(app.Party("/user"), func(m *mvc.Application) { m.Router.Use(cache.Handler(10*time.Second)) }) “` “`go // 2 // same: userRouter := app.Party("/user") userRouter.Use(cache.Handler(10*time.Second)) mvc.Configure(userRouter, ...) “` “`go // 3 // same: userRouter := app.Party("/user", cache.Handler(10*time.Second)) mvc.Configure(userRouter, ...) “` “`go // 4 // same: app.PartyFunc("/user", func(r iris.Party){ r.Use(cache.Handler(10*time.Second)) mvc.Configure(r, ...) }) “` If you want to use a middleware for a single route, for a single controller's method that is already registered by the engine and not by custom `Handle` (which you can add the middleware there on the last parameter) and it's not depend on the `Next Handler` to do its job then you just call it on the method: “`go var myMiddleware := myMiddleware.New(...) // this should return an iris/context.Handler type UserController struct{} func (c *UserController) GetSomething(ctx iris.Context) { // ctx.Proceed checks if myMiddleware called `ctx.Next()` // inside it and returns true if so, otherwise false.
middleware/without-ctx-next command
Package main is a simple example of the behavior change of the execution flow of the handlers, normally we need the `ctx.Next()` to call the next handler in a route's handler chain, but with the new `ExecutionRules` we can change this default behavior.
Package main is a simple example of the behavior change of the execution flow of the handlers, normally we need the `ctx.Next()` to call the next handler in a route's handler chain, but with the new `ExecutionRules` we can change this default behavior.
overview command
regexp command
Package main shows how to match "/xxx.json" in MVC handler.
Package main shows how to match "/xxx.json" in MVC handler.
singleton command
websocket command
orm
gorm command
xorm command
Package main shows how an orm can be used within your web app it just inserts a column and select the first.
Package main shows how an orm can be used within your web app it just inserts a column and select the first.
response-writer
basic command
custom-wrapper command
dynamic-path command
http-errors command
macros command
Package main shows how you can register a custom parameter type and macro functions that belongs to it.
Package main shows how you can register a custom parameter type and macro functions that belongs to it.
overview command
reverse command
route-state command
sessions
database/badger command
database/boltdb command
database/redis command
flash-messages command
middleware command
overview command
securecookie command
structuring
bootstrap command
subdomains
multi command
redirect command
Package main shows how to register a simple 'www' subdomain, using the `app.WWW` method, which will register a router wrapper which will redirect all 'mydomain.com' requests to 'www.mydomain.com'.
Package main shows how to register a simple 'www' subdomain, using the `app.WWW` method, which will register a router wrapper which will redirect all 'mydomain.com' requests to 'www.mydomain.com'.
single command
Package main register static subdomains, simple as parties, check ./hosts if you use windows
Package main register static subdomains, simple as parties, check ./hosts if you use windows
wildcard command
Package main an example on how to catch dynamic subdomains - wildcard.
Package main an example on how to catch dynamic subdomains - wildcard.
www command
testing
httptest command
tutorial
caddy/server1 command
caddy/server2 command
dropzonejs/src command
mongodb command
online-visitors command
url-shortener command
Package main shows how you can create a simple URL Shortener.
Package main shows how you can create a simple URL Shortener.
view
overview command
template_html_0 command
template_html_1 command
template_html_2 command
template_html_3 command
Package main an example on how to naming your routes & use the custom 'url path' HTML Template Engine, same for other template engines.
Package main an example on how to naming your routes & use the custom 'url path' HTML Template Engine, same for other template engines.
template_html_4 command
Package main an example on how to naming your routes & use the custom 'url' HTML Template Engine, same for other template engines.
Package main an example on how to naming your routes & use the custom 'url' HTML Template Engine, same for other template engines.
template_html_5 command
template_jet_0 command
Package main shows how to use jet template parser with ease using the Iris built-in Jet view engine.
Package main shows how to use jet template parser with ease using the Iris built-in Jet view engine.
template_jet_1_embedded command
Package main shows how to use jet templates embedded in your application with ease using the Iris built-in Jet view engine.
Package main shows how to use jet templates embedded in your application with ease using the Iris built-in Jet view engine.
template_pug_0 command
template_pug_1 command
Package main shows an example of pug actions based on https://github.com/Joker/jade/tree/master/example/actions
Package main shows an example of pug actions based on https://github.com/Joker/jade/tree/master/example/actions
template_pug_2 command
template_pug_3 command
write-to command
webassembly
basic command
basic/client command
websocket
basic command
basic/go-client command
native-messages command
socketio module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL