You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -10,31 +10,215 @@ description: Using Parsley for dependency injection in an application.
10
10
11
11
This example demonstrates integrating the [Parsley dependency injection framework](https://github.com/matzefriedrich/parsley) into a GoFiber web application. The goal is to showcase how dependency injection can create a clean, maintainable, and modular structure in your GoFiber projects.
12
12
13
+
14
+
## Prerequisites
15
+
16
+
* Go 1.23+
17
+
18
+
13
19
## Overview
14
20
15
-
In this example, we use Parsley to:
21
+
In this example, we use [Parsley](https://github.com/matzefriedrich/parsley) to:
22
+
23
+
***Bootstrap the application:** Set up and configure the Fiber app using Parsley’s DI container.
24
+
***Register dependencies:** Define and register services and route handlers with the DI container.
25
+
***Resolve dependencies:** Automatically resolve and inject them where needed.
16
26
17
-
***Bootstrap the Application:** Set up and configure the Fiber app using Parsley’s DI container.
18
-
***Register Dependencies:** Define and register services and route handlers with the DI container.
19
-
***Resolve Dependencies:** Automatically resolve and inject them where needed.
20
27
21
-
## Key Features
28
+
###Key features
22
29
23
-
***Modular Configuration:** Services are registered in modules, allowing for a clean separation of concerns.
***Simplified route management:** Route handlers are registered and managed via the DI container, making it easy to extend and maintain.
26
33
27
-
## How It Works
34
+
35
+
## How it works
28
36
29
37
* The `main` function bootstraps the application using Parsley’s `RunParsleyApplication` function.
30
38
* Modules define how services (such as the Fiber app and route handlers) are registered and configured.
31
-
* Route handlers are implemented as services that receive their dependencies (like the `Greeter` service) via constructor injection.
32
-
The `Greeter` service is a simple example of how services can be injected and used within route handlers to handle HTTP requests.
39
+
* Route handlers are implemented as services that receive their dependencies (like the `Greeter` service) via constructor injection. The `Greeter` service is a simple example of how services can be injected and used within route handlers to handle requests.
40
+
41
+
42
+
## The recipe - step by step
43
+
44
+
This guide demonstrates integrating the Parsley dependency injection framework with the GoFiber web framework. You can either clone the GoFiber recipes repository and navigate to the **parsley** example, or replicate each module while following the article:
45
+
46
+
```sh
47
+
git clone https://github.com/gofiber/recipes.git
48
+
cd recipes/parsley
49
+
```
50
+
51
+
The main entry point of the application is in the `cmd/main.go`.
In this file, the `RunParsleyApplication` function bootstraps the application. It initializes the Parsley application context and configures the GoFiber server with the necessary services and route handlers. Parsley's `bootstrap` package is generic and could also be used with other web application frameworks; the glue is the `NewApp` method, representing a constructor function that must return a `bootstrap.Application` instance.
77
+
78
+
The last parameter of the `RunParsleyApplication` function is an ellipsis parameter accepting `ModuleFunc` values representing service registration functions, which are invoked before calling the constructor function for `bootstrap.Application`. Here, the `ConfigureFiber` and `ConfigureGreeter` functions are specified; those are defined by the `modules` package.
79
+
80
+
81
+
### Configure and register the Fiber instance
82
+
83
+
The `ConfigureFiber` function sets up the Fiber application and registers it as a singleton service within the Parsley framework:
This configuration ensures that the Fiber instance is initialized and available for dependency injection.
115
+
116
+
117
+
### Define and register the application service(s)
118
+
119
+
The `Greeter` service generates greeting messages based on input parameters. In the recipe example application, this service is a dependency required by the handler of the `say-hello` route.
This handler responds to GET requests at `/say-hello` with a greeting message, utilizing the `Greeter` service injected via the constructor function.
210
+
211
+
212
+
## Run the application
213
+
214
+
To start the application, execute:
33
215
34
-
## Running the Example
216
+
```sh
217
+
go run ./cmd/main.go
218
+
```
35
219
36
-
To run this example:
220
+
Once running, you can test the `say-hello` endpoint via the browser, or from the terminal using `curl`. For this recipe, the default listening port is `5502`:
37
221
38
-
* Clone the repository and navigate to the example directory.
39
-
* Run `go run main.go` to start the application.
40
-
* Access the application by navigating to `http://localhost:5502/say-hello?name=YourName`. This will return a greeting message, demonstrating the integration of Parsley with GoFiber.
0 commit comments