mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user