mirror of
https://github.com/kataras/iris.git
synced 2026-01-15 16:05:57 +00:00
README: add 'run in the browser' button
after the quick start details, so it is visible always Former-commit-id: 0c13135a01c2b883aa4a9629a507aaf622d22ade
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Use(RoleMiddleware)
|
||||
|
||||
app.Get("/", commonHandler)
|
||||
c := app.ConfigureContainer()
|
||||
/*
|
||||
When you do NOT have access to the middleware code itself
|
||||
then you can register a request dependency
|
||||
which retrieves the value from the Context
|
||||
and returns it, so handler/function's input arguments
|
||||
with that `Role` type can be binded.
|
||||
|
||||
c.RegisterDependency(func(ctx iris.Context) Role {
|
||||
role, ok := GetRole(ctx)
|
||||
if !ok {
|
||||
// This codeblock will never be executed here
|
||||
// but you can stop executing a handler which depends on
|
||||
// that dependency with `ctx.StopExecution/ctx.StopWithXXX` methods
|
||||
// or by returning a second output argument of `error` type.
|
||||
ctx.StopExecution()
|
||||
return Role{}
|
||||
}
|
||||
|
||||
return role
|
||||
})
|
||||
*/
|
||||
c.Get("/dep", handlerWithDependencies)
|
||||
|
||||
// http://localhost:8080?name=kataras
|
||||
// http://localhost:8080/dep?name=kataras
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func commonHandler(ctx iris.Context) {
|
||||
role, _ := GetRole(ctx)
|
||||
ctx.WriteString(role.Name)
|
||||
}
|
||||
|
||||
func handlerWithDependencies(role Role) string {
|
||||
return role.Name
|
||||
}
|
||||
|
||||
// Code for an example middleware.
|
||||
|
||||
// Role struct value example.
|
||||
type Role struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
const roleContextKey = "myapp.role"
|
||||
|
||||
// RoleMiddleware example of a custom middleware.
|
||||
func RoleMiddleware(ctx iris.Context) {
|
||||
// [do it yourself: extract the role from the request...]
|
||||
if ctx.URLParam("name") != "kataras" {
|
||||
ctx.StopWithStatus(iris.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
//
|
||||
|
||||
role := Role{Name: "admin"}
|
||||
|
||||
ctx.Values().Set(roleContextKey, role)
|
||||
|
||||
// When you have access to the middleware itself:
|
||||
// Use the `RegisterDependency` to register
|
||||
// struct type values as dependencies at request-time for
|
||||
// any potential dependency injection-ed user handler.
|
||||
// This way the user of your middleware can get rid of
|
||||
// manually register a dependency for that `Role` type with calls of
|
||||
// `APIContainer.RegisterDependency` (and `mvc.Application.Register`).
|
||||
ctx.RegisterDependency(role)
|
||||
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
// GetRole returns the role inside the context values,
|
||||
// the `roleMiddleware` should be executed first.
|
||||
func GetRole(ctx iris.Context) (Role, bool) {
|
||||
v := ctx.Values().Get(roleContextKey)
|
||||
if v != nil {
|
||||
if role, ok := v.(Role); ok {
|
||||
return role, true
|
||||
}
|
||||
}
|
||||
|
||||
return Role{}, false
|
||||
}
|
||||
|
||||
// End Code of our example middleware.
|
||||
@@ -92,9 +92,7 @@ Create a `database/database.go` file and copy-paste the following:
|
||||
```go
|
||||
package database
|
||||
|
||||
import (
|
||||
"app/environment"
|
||||
)
|
||||
import "app/environment"
|
||||
|
||||
type DB interface {
|
||||
Exec(q string) error
|
||||
@@ -260,7 +258,7 @@ func (c *GreetController) Get(req model.Request) (model.Response, error) {
|
||||
}
|
||||
```
|
||||
|
||||
The `GreetController` depends on the `GreetService`. It serves the `GET: /greet` index path through its `Get` method. The `Get` method expecting a `model.Request` which contains a single field name of `Name` which will be extracted from the `URL Query Parameter 'name'` (because it's a `GET` requst and its `url:"name"` struct field).
|
||||
The `GreetController` depends on the `GreetService`. It serves the `GET: /greet` index path through its `Get` method. The `Get` method accepts a `model.Request` which contains a single field name of `Name` which will be extracted from the `URL Query Parameter 'name'` (because it's a `GET` requst and its `url:"name"` struct field).
|
||||
|
||||
## Wrap up
|
||||
|
||||
@@ -364,14 +362,17 @@ Install [Go](https://golang.org/dl) and run the application with:
|
||||
go run main.go
|
||||
```
|
||||
|
||||
### Docker
|
||||
<details><summary>Docker</summary>
|
||||
|
||||
Download the [Dockerfile](https://raw.githubusercontent.com/kataras/iris/9b93c0dbb491dcedf49c91e89ca13bab884d116f/_examples/mvc/overview/Dockerfile) and [docker-compose.yml](https://raw.githubusercontent.com/kataras/iris/9b93c0dbb491dcedf49c91e89ca13bab884d116f/_examples/mvc/overview/docker-compose.yml) files to the `app` folder.
|
||||
|
||||
Install [Docker](https://www.docker.com/) and execute the following command:
|
||||
|
||||
```sh
|
||||
$ docker-compose up
|
||||
```
|
||||
</details>
|
||||
|
||||
Visit http://localhost:8080?name=kataras.
|
||||
|
||||
Now, replace the `main.go`'s `app.Register(environment.DEV` with `environment.PROD`, restart the application and refresh. You will see that a new database (`sqlite`) and another service of (`greeterWithLogging`) will be binded to the `GreetController`. With **a single change** you achieve to completety change the result.
|
||||
Optionally, replace the `main.go`'s `app.Register(environment.DEV` with `environment.PROD`, restart the application and refresh. You will see that a new database (`sqlite`) and another service of (`greeterWithLogging`) will be binded to the `GreetController`. With **a single change** you achieve to completety change the result.
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"app/environment"
|
||||
)
|
||||
import "app/environment"
|
||||
|
||||
// DB example database interface.
|
||||
type DB interface {
|
||||
|
||||
@@ -33,25 +33,14 @@ func (c *VisitController) Get() string {
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
// Configure sessions manager as we used to.
|
||||
sess := sessions.New(sessions.Config{Cookie: "mysession_cookie_name"})
|
||||
app.Use(sess.Handler())
|
||||
|
||||
visitApp := mvc.New(app)
|
||||
// bind the current *session.Session, which is required, to the `VisitController.Session`
|
||||
// and the time.Now() to the `VisitController.StartTime`.
|
||||
visitApp.Register(
|
||||
// if dependency is a function which accepts
|
||||
// a Context and returns a single value
|
||||
// then the result type of this function is resolved by the controller
|
||||
// and on each request it will call the function with its Context
|
||||
// and set the result(the *sessions.Session here) to the controller's field.
|
||||
//
|
||||
// If dependencies are registered without field or function's input arguments as
|
||||
// consumers then those dependencies are being ignored before the server ran,
|
||||
// so you can bind many dependecies and use them in different controllers.
|
||||
// sess.Start, // However after version 12.2 sessions are automatically binded.
|
||||
time.Now(),
|
||||
)
|
||||
visitApp.Register(time.Now())
|
||||
// The `VisitController.Session` is automatically binded to the current `sessions.Session`.
|
||||
|
||||
visitApp.Handle(new(VisitController))
|
||||
|
||||
return app
|
||||
@@ -60,7 +49,7 @@ func newApp() *iris.Application {
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// 1. open the browser
|
||||
// 1. Prepare a client, e.g. your browser
|
||||
// 2. navigate to http://localhost:8080
|
||||
// 3. refresh the page some times
|
||||
// 4. close the browser
|
||||
|
||||
@@ -24,6 +24,17 @@ func main() {
|
||||
DisableSubdomainPersistence: false,
|
||||
// Allow getting the session value stored by the request from the same request.
|
||||
AllowReclaim: true,
|
||||
/*
|
||||
SessionIDGenerator: func(ctx iris.Context) string {
|
||||
id:= ctx.GetHeader("X-Session-Id")
|
||||
if id == "" {
|
||||
id = // [generate ID here and set the header]
|
||||
ctx.Header("X-Session-Id", id)
|
||||
}
|
||||
|
||||
return id
|
||||
},
|
||||
*/
|
||||
})
|
||||
|
||||
app := example.NewApp(sess)
|
||||
|
||||
Reference in New Issue
Block a user