mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 14:06:28 +01:00
let's see
parent
bf7330a4d6
commit
263244a7ce
BIN
_assets/benchmarks.png
Normal file
BIN
_assets/benchmarks.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
BIN
_assets/iris-stream-writer.gif
Normal file
BIN
_assets/iris-stream-writer.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
BIN
_assets/localization_insideviews.png
Normal file
BIN
_assets/localization_insideviews.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
BIN
_assets/localization_templatesstyle_inisections.png
Normal file
BIN
_assets/localization_templatesstyle_inisections.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
6
_assets/replace-gitbook-escapes.txt
Normal file
6
_assets/replace-gitbook-escapes.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
[VS CODE]
|
||||
CTRL+H, select Use Regular Expresion
|
||||
|
||||
Replace and keep parenthesis:
|
||||
\\\((.*?)\\\)
|
||||
($1)
|
|
@ -4,9 +4,9 @@ gRPC[\*](https://grpc.io/) is a modern open source high performance Remote Proce
|
|||
|
||||
Iris and gRPC integration lives inside the [mvc](https://github.com/kataras/iris/tree/master/mvc) package.
|
||||
|
||||
Have you ever have difficulties converting your app or parts of it from HTTP to gGRPC or did you ever wish you had decent HTTP framework support as well for your gRPC services? Now, with Iris you have the best of two worlds. Without change a bit of your existing gRPC services code, you can register them as Iris HTTP routes through a Controller \(your service struct value\).
|
||||
Have you ever have difficulties converting your app or parts of it from HTTP to gGRPC or did you ever wish you had decent HTTP framework support as well for your gRPC services? Now, with Iris you have the best of two worlds. Without change a bit of your existing gRPC services code, you can register them as Iris HTTP routes through a Controller (your service struct value).
|
||||
|
||||

|
||||

|
||||
|
||||
> Learn more about our conversation at: [https://github.com/kataras/iris/issues/1449\#issuecomment-623260695](https://github.com/kataras/iris/issues/1449#issuecomment-623260695)
|
||||
|
||||
|
@ -69,7 +69,7 @@ func (c *Greeter) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloR
|
|||
}
|
||||
```
|
||||
|
||||
Iris automatically binds the standard "context" `context.Context` to `iris.Context.Request().Context()` and any other structure that is not mapping to a registered dependency as a payload \(depending on the request\), e.g XML, YAML, Query, Form, JSON, Protobuf.
|
||||
Iris automatically binds the standard "context" `context.Context` to `iris.Context.Request().Context()` and any other structure that is not mapping to a registered dependency as a payload (depending on the request), e.g XML, YAML, Query, Form, JSON, Protobuf.
|
||||
|
||||
**5.** Register your service to the gRPC server
|
||||
|
||||
|
@ -90,7 +90,7 @@ pb.RegisterGreeterServer(grpcServer, myService)
|
|||
|
||||
**6.** Register this `myService` to Iris
|
||||
|
||||
The `mvc.New(party).Handle(ctrl, mvc.GRPC{...})` option allows to register gRPC services per-party \(without the requirement of a full wrapper\) and optionally strict access to gRPC-only clients.
|
||||
The `mvc.New(party).Handle(ctrl, mvc.GRPC{...})` option allows to register gRPC services per-party (without the requirement of a full wrapper) and optionally strict access to gRPC-only clients.
|
||||
|
||||
Register MVC application controller for gRPC services. You can bind as many mvc gRpc services in the same Party or app, as the `ServiceName` differs.
|
||||
|
||||
|
@ -115,7 +115,7 @@ rootApp.Handle(myService, mvc.GRPC{
|
|||
|
||||
**7.** Generate TLS Keys
|
||||
|
||||
The Iris server **should ran under TLS** \(it's a gRPC requirement\).
|
||||
The Iris server **should ran under TLS** (it's a gRPC requirement).
|
||||
|
||||
```bash
|
||||
$ openssl genrsa -out server.key 2048
|
||||
|
|
|
@ -140,7 +140,7 @@ The `DB` depends on the \`Environment.
|
|||
|
||||
## Service
|
||||
|
||||
We'll need a service that will communicate with a database instance in behalf of our Controller\(s\).
|
||||
We'll need a service that will communicate with a database instance in behalf of our Controller(s).
|
||||
|
||||
In our case we will only need a single service, the Greet Service.
|
||||
|
||||
|
@ -226,11 +226,11 @@ type Response struct {
|
|||
}
|
||||
```
|
||||
|
||||
The server will accept a URL Query Parameter of `name` \(e.g. `/greet?name=kataras`\) and will reply back with a JSON message.
|
||||
The server will accept a URL Query Parameter of `name` (e.g. `/greet?name=kataras`) and will reply back with a JSON message.
|
||||
|
||||
## Controller
|
||||
|
||||
MVC Controllers are responsible for controlling the flow of the application execution. When you make a request \(means request a page\) to MVC Application, a controller is responsible for returning the response to that request.
|
||||
MVC Controllers are responsible for controlling the flow of the application execution. When you make a request (means request a page) to MVC Application, a controller is responsible for returning the response to that request.
|
||||
|
||||
We will only need the `GreetController` for our mini web-application. Create a file at `controller/greet_controller.go` which looks like that:
|
||||
|
||||
|
@ -257,7 +257,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 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\). And it will respond with a `model.Response` (JSON) or an error.
|
||||
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). And it will respond with a `model.Response` (JSON) or an error.
|
||||
|
||||
{% page-ref page="../dependency-injection/inputs.md" %}
|
||||
{% page-ref page="../dependency-injection/outputs.md" %}
|
||||
|
@ -376,4 +376,4 @@ $ docker-compose up
|
|||
|
||||
Visit [http://localhost:8080?name=kataras](http://localhost:8080?name=kataras).
|
||||
|
||||
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.
|
||||
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.
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
Iris has a trivial way of registering websocket events via a Go structure. The websocket controller is part of the [MVC](mvc.md) features.
|
||||
|
||||
Iris has its own `iris/mvc/Application.HandleWebsocket(v interface{}) *neffos.Struct` to register controllers in existing Iris MVC applications\(offering a fully featured dependency injection container for request values and static services\).
|
||||
Iris has its own `iris/mvc/Application.HandleWebsocket(v interface{}) *neffos.Struct` to register controllers in existing Iris MVC applications(offering a fully featured dependency injection container for request values and static services).
|
||||
|
||||
```go
|
||||
// HandleWebsocket handles a websocket specific controller.
|
||||
|
@ -64,13 +64,13 @@ mvcApp.Register(
|
|||
)
|
||||
```
|
||||
|
||||
**4.** We register one or more websocket controllers, each websocket controller maps to one namespace \(just one is enough, as in most of the cases you don't need more, but that depends on your app's needs and requirements\).
|
||||
**4.** We register one or more websocket controllers, each websocket controller maps to one namespace (just one is enough, as in most of the cases you don't need more, but that depends on your app's needs and requirements).
|
||||
|
||||
```go
|
||||
mvcApp.HandleWebsocket(&websocketController{Namespace: "default"})
|
||||
```
|
||||
|
||||
**5.** Next, we continue by mapping the mvc application as a connection handler to a websocket server \(you may use more than one mvc applications per websocket server via `neffos.JoinConnHandlers(mvcApp1, mvcApp2)`\).
|
||||
**5.** Next, we continue by mapping the mvc application as a connection handler to a websocket server (you may use more than one mvc applications per websocket server via `neffos.JoinConnHandlers(mvcApp1, mvcApp2)`).
|
||||
|
||||
```go
|
||||
websocketServer := neffos.New(websocket.DefaultGorillaUpgrader, mvcApp)
|
||||
|
|
18
mvc/mvc.md
18
mvc/mvc.md
|
@ -1,12 +1,12 @@
|
|||
# MVC
|
||||
|
||||

|
||||

|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
Iris has **first-class support for the MVC \(Model View Controller\) architectural pattern**, you'll not find these stuff anywhere else in the Go world. You will have to import the [iris/mvc](https://github.com/kataras/iris/tree/master/mvc) subpackage.
|
||||
Iris has **first-class support for the MVC (Model View Controller) architectural pattern**, you'll not find these stuff anywhere else in the Go world. You will have to import the [iris/mvc](https://github.com/kataras/iris/tree/master/mvc) subpackage.
|
||||
|
||||
```go
|
||||
import "github.com/kataras/iris/v12/mvc"
|
||||
|
@ -22,7 +22,7 @@ If you're new to back-end web development read about the MVC architectural patte
|
|||
|
||||
All HTTP Methods are supported, for example if want to serve `GET` then the controller should have a function named `Get()`, for `POST` verb use the `Post()`, for a parameter use the `By` keyword, e.g. `PostUserBy(id uint64)` which translates to POST: [...]/User/{id}.
|
||||
|
||||
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:
|
||||
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 (
|
||||
|
@ -67,13 +67,13 @@ func (m *MyController) MyCustomHandler(id int64) string {
|
|||
}
|
||||
```
|
||||
|
||||
Persistence data inside your Controller struct \(share data between requests\) by defining services to the Dependencies or have a `Singleton` controller scope.
|
||||
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...)`.
|
||||
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.
|
||||
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.
|
||||
|
||||
|
@ -282,9 +282,9 @@ func (c *ExampleController) AfterActivation(a mvc.AfterActivation) {}
|
|||
*/
|
||||
```
|
||||
|
||||
Every `exported` func prefixed with an HTTP Method\(`Get`, `Post`, `Put`, `Delete`...\) in a controller is callable as an HTTP endpoint. In the sample above, all funcs writes a string to the response. Note the comments preceding each method.
|
||||
Every `exported` func prefixed with an HTTP Method(`Get`, `Post`, `Put`, `Delete`...) in a controller is callable as an HTTP endpoint. In the sample above, all funcs writes a string to the response. Note the comments preceding each method.
|
||||
|
||||
An HTTP endpoint is a targetable URL in the web application, such as `http://localhost:8080/helloworld`, and combines the protocol used: HTTP, the network location of the web server \(including the TCP port\): `localhost:8080` and the target URI `/helloworld`.
|
||||
An HTTP endpoint is a targetable URL in the web application, such as `http://localhost:8080/helloworld`, and combines the protocol used: HTTP, the network location of the web server (including the TCP port): `localhost:8080` and the target URI `/helloworld`.
|
||||
|
||||
The first comment states this is an [HTTP GET](https://www.w3schools.com/tags/ref_httpmethods.asp) method that is invoked by appending "/helloworld" to the base URL. The third comment specifies an [HTTP GET](https://www.w3schools.com/tags/ref_httpmethods.asp) method that is invoked by appending "/helloworld/welcome" to the URL.
|
||||
|
||||
|
|
|
@ -117,6 +117,6 @@ func handler(ctx iris.Context) {
|
|||
|
||||
**Result**
|
||||
|
||||

|
||||

|
||||
|
||||
That's all. As you've noticed, the client receives messages while loading. Check the next chapter (Server-Sent Events) to see an alternative way of sending messages to the client with connection-alive and loaded page.
|
||||
|
|
Loading…
Reference in New Issue
Block a user