mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
remove some examples learning content as they are exist on the wiki itself now
Former-commit-id: 8c69be5bf8ca21eb0e405ca5ad8431ceb1c2511e
This commit is contained in:
parent
722724ea96
commit
4caf8d7d9b
|
@ -15,26 +15,8 @@ It doesn't always contain the "best ways" but it does cover each important featu
|
|||
<details>
|
||||
<summary>External packages</summary>
|
||||
|
||||
```bash
|
||||
```sh
|
||||
cd _examples && go get ./...
|
||||
# or
|
||||
go get github.com/iris-contrib/middleware/...
|
||||
go get github.com/betacraft/yaag/irisyaag
|
||||
go get github.com/markbates/goth/...
|
||||
go get github.com/casbin/casbin
|
||||
go get github.com/aws/aws-sdk-go/...
|
||||
go get github.com/getsentry/raven-go/...
|
||||
go get github.com/prometheus/client_golang/...
|
||||
go get github.com/didip/tollbooth
|
||||
go get github.com/valyala/quicktemplate
|
||||
go get github.com/shiyanhui/hero
|
||||
go get github.com/go-xorm/xorm
|
||||
go get github.com/nfnt/resize
|
||||
go get github.com/dgrijalva/jwt-go
|
||||
go get github.com/newrelic/go-agent
|
||||
go get github.com/valyala/tcplisten
|
||||
go get github.com/kataras/bindata/cmd/bindata
|
||||
go get github.com/jmespath/go-jmespath
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -165,7 +147,7 @@ Navigate through examples for a better understanding.
|
|||
- [How it works](https://github.com/kataras/iris/blob/master/versioning/README.md)
|
||||
- [Example](versioning/main.go)
|
||||
|
||||
### hero
|
||||
### Dependency Injection
|
||||
|
||||
- [Basic](hero/basic/main.go)
|
||||
- [Overview](hero/overview)
|
||||
|
@ -174,140 +156,6 @@ Navigate through examples for a better understanding.
|
|||
|
||||
### MVC
|
||||
|
||||
![](mvc/web_mvc_diagram.png)
|
||||
|
||||
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.
|
||||
|
||||
Serve custom controller's struct's methods as handlers with custom paths(even with regex parametermized path) via the `BeforeActivation` custom event callback, per-controller. Example:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/mvc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
mvc.Configure(app.Party("/root"), myMVC)
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func myMVC(app *mvc.Application) {
|
||||
// app.Register(...)
|
||||
// app.Router.Use/UseGlobal/Done(...)
|
||||
app.Handle(new(MyController))
|
||||
}
|
||||
|
||||
type MyController struct {}
|
||||
|
||||
func (m *MyController) BeforeActivation(b mvc.BeforeActivation) {
|
||||
// b.Dependencies().Add/Remove
|
||||
// b.Router().Use/UseGlobal/Done // and any standard API call you already know
|
||||
|
||||
// 1-> Method
|
||||
// 2-> Path
|
||||
// 3-> The controller's function name to be parsed as handler
|
||||
// 4-> Any handlers that should run before the MyCustomHandler
|
||||
b.Handle("GET", "/something/{id:long}", "MyCustomHandler", anyMiddleware...)
|
||||
}
|
||||
|
||||
// GET: http://localhost:8080/root
|
||||
func (m *MyController) Get() string { return "Hey" }
|
||||
|
||||
// GET: http://localhost:8080/root/something/{id:long}
|
||||
func (m *MyController) MyCustomHandler(id int64) string { return "MyCustomHandler says Hey" }
|
||||
```
|
||||
|
||||
Persistence data inside your Controller struct (share data between requests)
|
||||
by defining services to the Dependencies or have a `Singleton` controller scope.
|
||||
|
||||
Share the dependencies between controllers or register them on a parent MVC Application, and ability
|
||||
to modify dependencies per-controller on the `BeforeActivation` optional event callback inside a Controller,
|
||||
i.e `func(c *MyController) BeforeActivation(b mvc.BeforeActivation) { b.Dependencies().Add/Remove(...) }`.
|
||||
|
||||
Access to the `Context` as a controller's field(no manual binding is neede) i.e `Ctx iris.Context` or via a method's input argument, i.e `func(ctx iris.Context, otherArguments...)`.
|
||||
|
||||
Models inside your Controller struct (set-ed at the Method function and rendered by the View).
|
||||
You can return models from a controller's method or set a field in the request lifecycle
|
||||
and return that field to another method, in the same request lifecycle.
|
||||
|
||||
Flow as you used to, mvc application has its own `Router` which is a type of `iris/router.Party`, the standard iris api.
|
||||
`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, recursively, see for example our `mvc.SessionController`, it has the `Session *sessions.Session` and `Manager *sessions.Sessions` as embedded fields
|
||||
which are filled by its `BeginRequest`, [here](https://github.com/kataras/iris/blob/master/mvc/session_controller.go).
|
||||
This is just an example, you could use the `sessions.Session` which returned from the manager's `Start` as a dynamic dependency to the MVC Application, i.e
|
||||
`mvcApp.Register(sessions.New(sessions.Config{Cookie: "iris_session_id"}).Start)`.
|
||||
|
||||
Access to the dynamic path parameters via the controller's methods' input arguments, no binding is needed.
|
||||
When you use the Iris' default syntax to parse handlers from a controller, you need to suffix the methods
|
||||
with the `By` word, uppercase is a new sub path. Example:
|
||||
|
||||
If `mvc.New(app.Party("/user")).Handle(new(user.Controller))`
|
||||
|
||||
- `func(*Controller) Get()` - `GET:/user`.
|
||||
- `func(*Controller) Post()` - `POST:/user`.
|
||||
- `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 `mvc.New(app.Party("/profile")).Handle(new(profile.Controller))`
|
||||
|
||||
- `func(*Controller) GetBy(username string)` - `GET:/profile/{param:string}`
|
||||
|
||||
If `mvc.New(app.Party("/assets")).Handle(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
|
||||
|
||||
```go
|
||||
func(c *ExampleController) Get() string |
|
||||
(string, string) |
|
||||
(string, int) |
|
||||
int |
|
||||
(int, string) |
|
||||
(string, error) |
|
||||
error |
|
||||
(int, error) |
|
||||
(any, bool) |
|
||||
(customStruct, error) |
|
||||
customStruct |
|
||||
(customStruct, int) |
|
||||
(customStruct, string) |
|
||||
mvc.Result or (mvc.Result, error)
|
||||
```
|
||||
|
||||
where [mvc.Result](https://github.com/kataras/iris/blob/master/mvc/go19.go#L10) is an [interface](https://github.com/kataras/iris/blob/master/hero/func_result.go#L18) 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](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller).
|
||||
|
||||
Follow the examples below,
|
||||
|
||||
- [Hello world](mvc/hello-world/main.go)
|
||||
- [Regexp](mvc/regexp/main.go) **NEW**
|
||||
- [Session Controller](mvc/session-controller/main.go)
|
||||
|
@ -334,15 +182,6 @@ Follow the examples below,
|
|||
|
||||
### View
|
||||
|
||||
| Engine | Declaration |
|
||||
| -----------|-------------|
|
||||
| template/html | `iris.HTML(...)` |
|
||||
| django | `iris.Django(...)` |
|
||||
| handlebars | `iris.Handlebars(...)` |
|
||||
| amber | `iris.Amber(...)` |
|
||||
| pug(jade) | `iris.Pug(...)` |
|
||||
| jet | `iris.Jet(...)` |
|
||||
|
||||
- [Overview](view/overview/main.go)
|
||||
- [Hi](view/template_html_0/main.go)
|
||||
- [A simple Layout](view/template_html_1/main.go)
|
||||
|
@ -480,12 +319,6 @@ iris session manager lives on its own [package](https://github.com/kataras/iris/
|
|||
|
||||
### Websockets
|
||||
|
||||
[WebSocket](https://wikipedia.org/wiki/WebSocket) is a protocol that enables two-way persistent communication channels over TCP connections. It is used for applications such as chat, stock tickers, games, anywhere you want real-time functionality in a web application.
|
||||
|
||||
Iris websocket library is now merged with the [neffos real-time framework](https://github.com/kataras/neffos) and Iris-specific helpers live on the [iris/websocket](https://github.com/kataras/iris/tree/master/websocket) subpackage. Learn neffos from its [wiki](https://github.com/kataras/neffos#learning-neffos) and have a look some of the [Iris websocket examples](websocket/).
|
||||
|
||||
The package is designed to work with raw websockets although its API is similar to the famous [socket.io](https://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.
|
||||
|
||||
- [Basic](websocket/basic) **NEW**
|
||||
* [Server](websocket/basic/server.go)
|
||||
* [Go Client](websocket/basic/go-client/client.go)
|
||||
|
|
|
@ -1,146 +0,0 @@
|
|||
# MVC
|
||||
|
||||
![](https://github.com/kataras/iris/raw/master/_examples/mvc/web_mvc_diagram.png)
|
||||
|
||||
Iris has **first-class support for the MVC pattern**, you'll not find
|
||||
these stuff anywhere else in the Go world.
|
||||
|
||||
Iris 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.
|
||||
|
||||
Serve custom controller's struct's methods as handlers with custom paths(even with regex parametermized path)
|
||||
via the `BeforeActivation` custom event callback, per-controller. Example:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/mvc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
mvc.Configure(app.Party("/root"), myMVC)
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func myMVC(app *mvc.Application) {
|
||||
// app.Register(...)
|
||||
// app.Router.Use/UseGlobal/Done(...)
|
||||
app.Handle(new(MyController))
|
||||
}
|
||||
|
||||
type MyController struct {}
|
||||
|
||||
func (m *MyController) BeforeActivation(b mvc.BeforeActivation) {
|
||||
// b.Dependencies().Add/Remove
|
||||
// b.Router().Use/UseGlobal/Done // and any standard API call you already know
|
||||
|
||||
// 1-> Method
|
||||
// 2-> Path
|
||||
// 3-> The controller's function name to be parsed as handler
|
||||
// 4-> Any handlers that should run before the MyCustomHandler
|
||||
b.Handle("GET", "/something/{id:long}", "MyCustomHandler", anyMiddleware...)
|
||||
}
|
||||
|
||||
// GET: http://localhost:8080/root
|
||||
func (m *MyController) Get() string { return "Hey" }
|
||||
|
||||
// GET: http://localhost:8080/root/something/{id:long}
|
||||
func (m *MyController) MyCustomHandler(id int64) string { return "MyCustomHandler says Hey" }
|
||||
```
|
||||
|
||||
Persistence data inside your Controller struct (share data between requests)
|
||||
by defining services to the Dependencies or have a `Singleton` controller scope.
|
||||
|
||||
Share the dependencies between controllers or register them on a parent MVC Application, and ability
|
||||
to modify dependencies per-controller on the `BeforeActivation` optional event callback inside a Controller,
|
||||
i.e `func(c *MyController) BeforeActivation(b mvc.BeforeActivation) { b.Dependencies().Add/Remove(...) }`.
|
||||
|
||||
Access to the `Context` as a controller's field(no manual binding is neede) i.e `Ctx iris.Context` or via a method's input argument, i.e `func(ctx iris.Context, otherArguments...)`.
|
||||
|
||||
Models inside your Controller struct (set-ed at the Method function and rendered by the View).
|
||||
You can return models from a controller's method or set a field in the request lifecycle
|
||||
and return that field to another method, in the same request lifecycle.
|
||||
|
||||
Flow as you used to, mvc application has its own `Router` which is a type of `iris/router.Party`, the standard iris api.
|
||||
`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, recursively, see for example our `mvc.SessionController`, it has the `Session *sessions.Session` and `Manager *sessions.Sessions` as embedded fields
|
||||
which are filled by its `BeginRequest`, [here](https://github.com/kataras/iris/blob/master/mvc/session_controller.go).
|
||||
This is just an example, you could use the `sessions.Session` as a dependency to the MVC Application, i.e
|
||||
`mvcApp.Register(sessions.New(sessions.Config{Cookie: "iris_session_id"}).Start)`.
|
||||
|
||||
Access to the dynamic path parameters via the controller's methods' input arguments, no binding is needed.
|
||||
When you use the Iris' default syntax to parse handlers from a controller, you need to suffix the methods
|
||||
with the `By` word, uppercase is a new sub path. Example:
|
||||
|
||||
If `mvc.New(app.Party("/user")).Handle(new(user.Controller))`
|
||||
|
||||
- `func(*Controller) Get()` - `GET:/user`.
|
||||
- `func(*Controller) Post()` - `POST:/user`.
|
||||
- `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 `mvc.New(app.Party("/profile")).Handle(new(profile.Controller))`
|
||||
|
||||
- `func(*Controller) GetBy(username string)` - `GET:/profile/{param:string}`
|
||||
|
||||
If `mvc.New(app.Party("/assets")).Handle(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
|
||||
|
||||
```go
|
||||
func(c *ExampleController) Get() string |
|
||||
(string, string) |
|
||||
(string, int) |
|
||||
int |
|
||||
(int, string) |
|
||||
(string, error) |
|
||||
error |
|
||||
(int, error) |
|
||||
(any, bool) |
|
||||
(customStruct, error) |
|
||||
customStruct |
|
||||
(customStruct, int) |
|
||||
(customStruct, string) |
|
||||
mvc.Result or (mvc.Result, error)
|
||||
```
|
||||
|
||||
where [mvc.Result](https://github.com/kataras/iris/blob/master/mvc/func_result.go) 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](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller).
|
||||
|
||||
## Examples
|
||||
|
||||
- [Hello world](hello-world/main.go) **UPDATED**
|
||||
- [Session Controller](session-controller/main.go) **UPDATED**
|
||||
- [Overview - Plus Repository and Service layers](overview) **UPDATED**
|
||||
- [Login showcase - Plus Repository and Service layers](login) **UPDATED**
|
||||
- [Singleton](singleton) **NEW**
|
||||
- [Websocket Controller](websocket) **NEW**
|
||||
- [Register Middleware](middleware) **NEW**
|
||||
|
||||
Folder structure guidelines can be found at the [_examples/#structuring](https://github.com/kataras/iris/tree/master/_examples/#structuring) section.
|
Binary file not shown.
Before Width: | Height: | Size: 25 KiB |
|
@ -1,119 +0,0 @@
|
|||
# Sessions
|
||||
|
||||
Iris provides a fast, fully featured and easy to use sessions manager.
|
||||
|
||||
Iris sessions manager lives on its own [kataras/iris/sessions](https://github.com/kataras/iris/tree/master/sessions) package.
|
||||
|
||||
Some trivial examples,
|
||||
|
||||
- [Overview](https://github.com/kataras/iris/blob/master/_examples/sessions/overview/main.go)
|
||||
- [Standalone](https://github.com/kataras/iris/blob/master/_examples/sessions/standalone/main.go)
|
||||
- [Secure Cookie](https://github.com/kataras/iris/blob/master/_examples/sessions/securecookie/main.go)
|
||||
- [Flash Messages](https://github.com/kataras/iris/blob/master/_examples/sessions/flash-messages/main.go)
|
||||
- [Databases](https://github.com/kataras/iris/tree/master/_examples/sessions/database)
|
||||
* [Badger](https://github.com/kataras/iris/blob/master/_examples/sessions/database/badger/main.go) **fastest**
|
||||
* [BoltDB](https://github.com/kataras/iris/blob/master/_examples/sessions/database/boltdb/main.go)
|
||||
* [Redis](https://github.com/kataras/iris/blob/master/_examples/sessions/database/redis/main.go)
|
||||
|
||||
## Overview
|
||||
|
||||
```go
|
||||
import "github.com/kataras/iris/sessions"
|
||||
|
||||
manager := sessions.Start(iris.Context)
|
||||
manager.
|
||||
ID() string
|
||||
Get(string) interface{}
|
||||
HasFlash() bool
|
||||
GetFlash(string) interface{}
|
||||
GetFlashString(string) string
|
||||
GetString(key string) string
|
||||
GetInt(key string) (int, error)
|
||||
GetInt64(key string) (int64, error)
|
||||
GetFloat32(key string) (float32, error)
|
||||
GetFloat64(key string) (float64, error)
|
||||
GetBoolean(key string) (bool, error)
|
||||
GetAll() map[string]interface{}
|
||||
GetFlashes() map[string]interface{}
|
||||
VisitAll(cb func(k string, v interface{}))
|
||||
Set(string, interface{})
|
||||
SetImmutable(key string, value interface{})
|
||||
SetFlash(string, interface{})
|
||||
Delete(string)
|
||||
Clear()
|
||||
ClearFlashes()
|
||||
```
|
||||
|
||||
This example will show how to store data from a session.
|
||||
|
||||
You don't need any third-party library except Iris, but if you want you can use anything, remember Iris is fully compatible with the standard library. You can find a more detailed examples by pressing [here](https://github.com/kataras/iris/tree/master/_examples/sessions).
|
||||
|
||||
In this example we will only allow authenticated users to view our secret message on the `/secret` age. To get access to it, the will first have to visit `/login` to get a valid session cookie, hich logs him in. Additionally he can visit `/logout` to revoke his access to our secret message.
|
||||
|
||||
```go
|
||||
// sessions.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
|
||||
var (
|
||||
cookieNameForSessionID = "mycookiesessionnameid"
|
||||
sess = sessions.New(sessions.Config{Cookie: cookieNameForSessionID, AllowReclaim: true})
|
||||
)
|
||||
|
||||
func secret(ctx iris.Context) {
|
||||
// Check if user is authenticated
|
||||
if auth, _ := sess.Start(ctx).GetBoolean("authenticated"); !auth {
|
||||
ctx.StatusCode(iris.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
// Print secret message
|
||||
ctx.WriteString("The cake is a lie!")
|
||||
}
|
||||
|
||||
func login(ctx iris.Context) {
|
||||
session := sess.Start(ctx)
|
||||
|
||||
// Authentication goes here
|
||||
// ...
|
||||
|
||||
// Set user as authenticated
|
||||
session.Set("authenticated", true)
|
||||
}
|
||||
|
||||
func logout(ctx iris.Context) {
|
||||
session := sess.Start(ctx)
|
||||
|
||||
// Revoke users authentication
|
||||
session.Set("authenticated", false)
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/secret", secret)
|
||||
app.Get("/login", login)
|
||||
app.Get("/logout", logout)
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
```bash
|
||||
$ go run sessions.go
|
||||
|
||||
$ curl -s http://localhost:8080/secret
|
||||
Forbidden
|
||||
|
||||
$ curl -s -I http://localhost:8080/login
|
||||
Set-Cookie: mysessionid=MTQ4NzE5Mz...
|
||||
|
||||
$ curl -s --cookie "mysessionid=MTQ4NzE5Mz..." http://localhost:8080/secret
|
||||
The cake is a lie!
|
||||
```
|
|
@ -1 +0,0 @@
|
|||
Head over to the [kataras/iris/versioning/README.md](https://github.com/kataras/iris/blob/master/versioning/README.md) instead.
|
|
@ -3,7 +3,9 @@ package sessions
|
|||
import (
|
||||
"time"
|
||||
|
||||
"github.com/iris-contrib/go.uuid"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
uuid "github.com/iris-contrib/go.uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -103,10 +105,11 @@ type (
|
|||
// Defaults to infinitive/unlimited life duration(0).
|
||||
Expires time.Duration
|
||||
|
||||
// SessionIDGenerator should returns a random session id.
|
||||
// SessionIDGenerator can be set to a function which
|
||||
// return a unique session id.
|
||||
// By default we will use a uuid impl package to generate
|
||||
// that, but developers can change that with simple assignment.
|
||||
SessionIDGenerator func() string
|
||||
SessionIDGenerator func(ctx context.Context) string
|
||||
|
||||
// DisableSubdomainPersistence set it to true in order dissallow your subdomains to have access to the session cookie
|
||||
//
|
||||
|
@ -123,7 +126,7 @@ func (c Config) Validate() Config {
|
|||
}
|
||||
|
||||
if c.SessionIDGenerator == nil {
|
||||
c.SessionIDGenerator = func() string {
|
||||
c.SessionIDGenerator = func(context.Context) string {
|
||||
id, _ := uuid.NewV4()
|
||||
return id.String()
|
||||
}
|
||||
|
|
|
@ -78,7 +78,7 @@ func (s *Sessions) Start(ctx context.Context, cookieOptions ...context.CookieOpt
|
|||
cookieValue := s.decodeCookieValue(GetCookie(ctx, s.config.Cookie))
|
||||
|
||||
if cookieValue == "" { // cookie doesn't exists, let's generate a session and add set a cookie
|
||||
sid := s.config.SessionIDGenerator()
|
||||
sid := s.config.SessionIDGenerator(ctx)
|
||||
|
||||
sess := s.provider.Init(sid, s.config.Expires)
|
||||
sess.isNew = s.provider.db.Len(sid) == 0
|
||||
|
|
Loading…
Reference in New Issue
Block a user