Skip to content

Commit 9b87848

Browse files
Umang01-hasharyanmehrotraVipul Rawatsrijan-27
authored
add godoc for datasource and service packages (#389)
* add godoc for datasource and service packages * add godoc comments for http package * add godoc comments for middleware package * mend add godocs in http and logger package * add godoc comments for metrics package * Improvise documentations (#401) * improvise docs * fix docs * remove extra line --------- Co-authored-by: Aryan Mehrotra <[email protected]> Co-authored-by: Vipul Rawat <[email protected]> Co-authored-by: Srijan Rastogi <[email protected]> Co-authored-by: mehrotra234 <[email protected]>
1 parent 3227708 commit 9b87848

File tree

33 files changed

+132
-61
lines changed

33 files changed

+132
-61
lines changed

docs/advanced-guide/circuit-breaker/page.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,3 @@ func main() {
4242

4343
Circuit breaker state changes to open when number of consecutive failed requests increases the threshold.
4444
When it is in open state, GoFr makes request to the aliveness endpoint (default being - /.well-known/alive) at an equal interval of time provided in config.
45-
46-
To override the default aliveness endpoint {% new-tab-link newtab=false title="refer" href="/docs/advanced-guide/monitoring-service-health" /%}.

docs/advanced-guide/http-authentication/page.md

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
# HTTP Authentication
2+
23
Authentication is a crucial aspect of web applications, controlling access to resources based on user roles or permissions.
3-
It is the process of verifying a user's identity to grant access to protected resources. It ensures only
4-
authenticated users can perform actions or access data within an application.
4+
It is the process of verifying a user's identity to grant access to protected resources. It ensures that only authenticated
5+
users can perform actions or access data within an application.
56

67
GoFr offer various approaches to implement authorization.
78

89
## 1. HTTP Basic Auth
910
*Basic Authentication* is a simple HTTP authentication scheme where the user's credentials (username and password) are
1011
transmitted in the request header in a Base64-encoded format.
1112

12-
Basic auth is the simplest way to authenticate your APIs. It's built on
13-
{% new-tab-link title="HTTP protocol authentication scheme" href="https://datatracker.ietf.org/doc/html/rfc7617" /%}. It involves sending the term
14-
15-
`Basic` trailed by the Base64-encoded `<username>:<password>` within the standard `Authorization` header.
13+
Basic auth is the simplest way to authenticate your APIs. It's built on
14+
{% new-tab-link title="HTTP protocol authentication scheme" href="https://datatracker.ietf.org/doc/html/rfc7617" /%}.
15+
It involves sending the prefix `Basic` trailed by the Base64-encoded `<username>:<password>` within the standard `Authorization` header.
1616

1717
### Basic Authentication in GoFr
1818

1919
GoFr offers two ways to implement basic authentication:
2020

2121
**1. Predefined Credentials**
2222

23-
Use `EnableBasicAuth(username, password)` to configure Gofr with pre-defined credentials.
23+
Use `EnableBasicAuth(username, password)` to configure GoFr with pre-defined credentials.
2424

2525
```go
2626
func main() {
@@ -39,7 +39,8 @@ func main() {
3939

4040
**2. Custom Validation Function**
4141

42-
Use `EnableBasicAuthWithFunc(validationFunc)` to implement your own validation logic for credentials. The `validationFunc` takes the username and password as arguments and returns true if valid, false otherwise.
42+
Use `EnableBasicAuthWithFunc(validationFunc)` to implement your own validation logic for credentials.
43+
The `validationFunc` takes the username and password as arguments and returns true if valid, false otherwise.
4344

4445
```go
4546
func validateUser(username string, password string) bool {
@@ -63,7 +64,6 @@ func main() {
6364
```
6465

6566
### Adding Basic Authentication to HTTP Services
66-
6767
This code snippet demonstrates how to add basic authentication to an HTTP service in GoFr and make a request with the appropriate Authorization header:
6868

6969
```go
@@ -73,13 +73,13 @@ app.AddHTTPService("order", "https://localhost:2000",
7373
```
7474

7575
## 2. API Keys Auth
76-
Users include a unique API key in the request header for validation against a store of authorized keys.
76+
*API Key Authentication* is an HTTP authentication scheme where a unique API key is included in the request header for validation against a store of authorized keys.
7777

7878
### Usage:
7979
GoFr offers two ways to implement API Keys authentication.
8080

8181
**1. Framework Default Validation**
82-
- Users can select the framework's default validation using **_EnableAPIKeyAuth(apiKeys ...string)_**
82+
- GoFr's default validation can be selected using **_EnableAPIKeyAuth(apiKeys ...string)_**
8383

8484
```go
8585
package main
@@ -97,7 +97,7 @@ func main() {
9797
```
9898

9999
**2. Custom Validation Function**
100-
- Users can create their own validator function `apiKeyValidator(apiKey string) bool` for validating APIKeys and pass the func in **_EnableAPIKeyAuthWithFunc(validator)_**
100+
- GoFr allows a custom validator function `apiKeyValidator(apiKey string) bool` for validating APIKeys and pass the func in **_EnableAPIKeyAuthWithFunc(validator)_**
101101

102102
```go
103103
package main
@@ -128,11 +128,10 @@ app.AddHTTPService("http-server-using-redis", "http://localhost:8000", &service.
128128
```
129129

130130
## 3. OAuth 2.0
131-
OAuth 2.0 is the industry-standard protocol for authorization.
131+
{% new-tab-link title="OAuth" href="https://www.rfc-editor.org/rfc/rfc6749" /%} 2.0 is the industry-standard protocol for authorization.
132132
It focuses on client developer simplicity while providing specific authorization flows for web applications, desktop applications, mobile phones, and living room devices.
133-
To know more about it refer {% new-tab-link title="here" href="https://www.rfc-editor.org/rfc/rfc6749" /%}
134133

135-
It involves sending the term `Bearer` trailed by the encoded token within the standard `Authorization` header.
134+
It involves sending the prefix `Bearer` trailed by the encoded token within the standard `Authorization` header.
136135

137136
### OAuth Authentication in GoFr
138137

@@ -141,7 +140,7 @@ GoFr supports authenticating tokens encoded by algorithm `RS256/384/512`.
141140
### App level Authentication
142141
Enable OAuth 2.0 with three-legged flow to authenticate requests
143142

144-
Use `EnableOAuth(jwks-endpoint,refresh_interval)` to configure Gofr with pre-defined credentials.
143+
Use `EnableOAuth(jwks-endpoint,refresh_interval)` to configure GoFr with pre-defined credentials.
145144

146145
```go
147146
func main() {
@@ -163,17 +162,17 @@ For server-to-server communication it follows two-legged OAuth, also known as "c
163162
where the client application directly exchanges its own credentials (ClientID and ClientSecret)
164163
for an access token without involving any end-user interaction.
165164

166-
This code snippet demonstrates how two-legged OAuth authentication is added to an HTTP service in GoFr and make a request with the appropriate Authorization header.
165+
This code snippet demonstrates how two-legged OAuth authentication is added to an HTTP service in GoFr and make a request with the appropriate Authorization header:
167166

168167
```go
169-
a.AddHTTPService("orders", "http://localhost:9000",
168+
app.AddHTTPService("orders", "http://localhost:9000",
170169
&service.OAuthConfig{ // Replace with your credentials
171-
ClientID: "0iyeGcLYWudLGqZfD6HvOdZHZ5TlciAJ",
172-
ClientSecret: "GQXTY2f9186nUS3C9WWi7eJz8-iVEsxq7lKxdjfhOJbsEPPtEszL3AxFn8k_NAER",
173-
TokenURL: "https://dev-zq6tvaxf3v7p0g7j.us.auth0.com/oauth/token",
174-
Scopes: []string{"read:order"},
175-
EndpointParams: map[string][]string{
176-
"audience": {"https://dev-zq6tvaxf3v7p0g7j.us.auth0.com/api/v2/"},
170+
ClientID: "0iyeGcLYWudLGqZfD6HvOdZHZ5TlciAJ",
171+
ClientSecret: "GQXTY2f9186nUS3C9WWi7eJz8-iVEsxq7lKxdjfhOJbsEPPtEszL3AxFn8k_NAER",
172+
TokenURL: "https://dev-zq6tvaxf3v7p0g7j.us.auth0.com/oauth/token",
173+
Scopes: []string{"read:order"},
174+
EndpointParams: map[string][]string{
175+
"audience": {"https://dev-zq6tvaxf3v7p0g7j.us.auth0.com/api/v2/"},
177176
},
178177
})
179-
```
178+
```

docs/advanced-guide/http-communication/page.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Interservice HTTP Calls
1+
# Inter-Service HTTP Calls
22

3-
GoFr promotes microservice architecture and to facilitate the same, it provides the support
4-
to initialize HTTP services at application level using `AddHTTPService()` method.
5-
Support for inter-service http calls provide the following benefits:
3+
GoFr promotes microservice architecture and to facilitate the same, it provides the support to initialize HTTP services
4+
at application level using `AddHTTPService()` method.
65

7-
1. Access to the method from container - GET, PUT, POST, PATCH, DELETE.
6+
Support for inter-service HTTP calls provide the following benefits:
7+
1. Access to the methods from container - GET, PUT, POST, PATCH, DELETE.
88
2. Logs and traces for the request.
99
3. {% new-tab-link newtab=false title="Circuit breaking" href="/docs/advanced-guide/circuit-breaker" /%} for enhanced resilience and fault tolerance.
1010
4. {% new-tab-link newtab=false title="Custom Health Check" href="/docs/advanced-guide/monitoring-service-health" /%} Endpoints
@@ -13,15 +13,15 @@ Support for inter-service http calls provide the following benefits:
1313

1414
### Registering a simple HTTP Service
1515

16-
Users can register a new HTTP service using the application method `AddHTTPService()`.
17-
It takes in a service name and service address argument to register your dependent service at application level.
18-
Users can easily register multiple dependent services easily, which is a common use case in a microservice architecture.
16+
GoFr allows registering a new HTTP service using the application method `AddHTTPService()`.
17+
It takes in a service name and service address argument to register the dependent service at application level.
18+
Registration of multiple dependent services is quite easier, which is a common use case in a microservice architecture.
1919

2020
> The services instances are maintained by the container.
2121
22-
Users can provide other options additionally to coat their basic http client with features like circuit-breaker and
23-
custom health check to add to the functionality of the HTTP service.
24-
The design choice for this was made so that user can add as many options as required and are order agnostic,
22+
Other provided options can be added additionally to coat the basic http client with features like circuit-breaker and
23+
custom health check and add to the functionality of the HTTP service.
24+
The design choice for this was made such as many options as required can be added and are order agnostic,
2525
i.e. the order of the options is not important.
2626

2727
> Service names are to be kept unique to one service.
@@ -31,7 +31,6 @@ app.AddHTTPService(<service_name> , <service_address>)
3131
```
3232

3333
#### Example
34-
3534
```go
3635
package main
3736

@@ -55,8 +54,9 @@ func main() {
5554

5655
### Accessing HTTP Service in handler
5756

58-
Users can access the HTTP service client from anywhere using the gofr.Context that gets passed on from the handler.
59-
The service name that was given at the time of registering the service.
57+
The HTTP service client is accessible anywhere from `gofr.Context` that gets passed on from the handler.
58+
Using the `GetHTTPService` method with the service name that was given at the time of registering the service,
59+
the client can be retrieved as shown below:
6060

6161
```go
6262
svc := ctx.GetHTTPService(<service_name>)

docs/advanced-guide/monitoring-service-health/page.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Monitoring Service Health
22

3-
Health check in microservices refers to a mechanism or process implemented within each service to assess its operational status and readiness to handle requests. It involves regularly querying the service to determine if it is functioning correctly, typically by evaluating its responsiveness and ability to perform essential tasks. Health checks play a critical role in ensuring service availability, detecting failures, preventing cascading issues, and facilitating effective traffic routing in distributed systems.
3+
Health check in microservices refers to a mechanism or process implemented within each service to assess its operational status
4+
and readiness to handle requests. It involves regularly querying the service to determine if it is functioning correctly,
5+
typically by evaluating its responsiveness and ability to perform essential tasks. Health checks play a critical role in ensuring service availability,
6+
detecting failures, preventing cascading issues, and facilitating effective traffic routing in distributed systems.
47

5-
## GoFr by default registers two endpoints which are :
8+
## GoFr by default registers two endpoints which are:
69

710
### 1. Aliveness - /.well-known/alive
811

9-
It is an endpoint which return the following response when the service is UP with a 200 response code.
12+
It is an endpoint which returns the following response with a 200 status code, when the service is UP.
1013

1114
```json
1215
{
@@ -18,8 +21,7 @@ It is an endpoint which return the following response when the service is UP wit
1821

1922
It is also used when state of {% new-tab-link newtab=false title="circuit breaker" href="/docs/advanced-guide/circuit-breaker" /%} is open.
2023

21-
To override the endpoint when registering HTTP Service pass the following option.
22-
24+
To override this endpoint, pass the following option while registering HTTP Service:
2325
```go
2426
&service.HealthConfig{
2527
HealthEndpoint: "breeds",
@@ -28,10 +30,9 @@ To override the endpoint when registering HTTP Service pass the following option
2830

2931
### 2. Health-Check - /.well-known/health-check
3032

31-
It returns if the service is UP or DOWN along with stats, host, status about the dependent datasources and services.
32-
33-
Sample response of how it appears when all the services, and connected data sources are up.
33+
It is an endpoint which returns whether the service is UP or DOWN along with stats, host, status about the dependent datasources and services.
3434

35+
Sample response of how it appears when all the services, and connected data sources are UP:
3536
```json
3637
{
3738
"data": {

docs/advanced-guide/publishing-custom-metrics/page.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Publishing Custom Metrics
22

3-
For GoFr default metrics refer: {% new-tab-link newtab=false title="observability" href="/docs/quick-start/observability" /%}.
3+
GoFr publishes some {% new-tab-link newtab=false title="default metrics" href="/docs/quick-start/observability" /%}.
44

55
GoFr can handle multiple different metrics concurrently, each uniquely identified by its name during initialization.
66
It supports the following {% new-tab-link title="metrics" href="https://opentelemetry.io/docs/specs/otel/metrics/" /%} types in prometheus format:
@@ -10,7 +10,7 @@ It supports the following {% new-tab-link title="metrics" href="https://opentele
1010
3. Histogram
1111
4. Gauge
1212

13-
If any metric other than defaults provided, you can create them using custom metrics as shown below.
13+
If any custom metric is required, it can be created by using custom metrics as shown below:
1414

1515
## Usage
1616

@@ -46,7 +46,7 @@ func main() {
4646
## 2. UpDown Counter Metrics
4747

4848
UpDownCounter is a {% new-tab-link title="synchronous Instrument" href="https://opentelemetry.io/docs/specs/otel/metrics/api/#synchronous-instrument-api" /%} which supports increments and decrements.
49-
Note: if the value is monotonically increasing, use Counter instead.
49+
Note: If the value is monotonically increasing, use Counter instead.
5050

5151
### Usage
5252

@@ -75,7 +75,8 @@ func main() {
7575

7676
## 3. Histogram Metrics
7777

78-
Histogram is a {% new-tab-link title="synchronous Instrument" href="https://opentelemetry.io/docs/specs/otel/metrics/api/#synchronous-instrument-api" /%} which can be used to report arbitrary values that are likely to be statistically meaningful. It is intended for statistics such as histograms, summaries, and percentile.
78+
Histogram is a {% new-tab-link title="synchronous Instrument" href="https://opentelemetry.io/docs/specs/otel/metrics/api/#synchronous-instrument-api" /%} which can be used to
79+
report arbitrary values that are likely to be statistically meaningful. It is intended for statistics such as histograms, summaries, and percentile.
7980

8081
### Usage
8182

@@ -141,12 +142,12 @@ func main() {
141142
**Good To Know**
142143

143144
```doc
144-
While registering a metrics two key pieces of information of required
145+
While registering a metrics 2 key pieces of information of required:
145146
- Name
146147
- Description
147148
148-
When a registered Metrics has to be used 3 key pieces of information are required:
149-
- Metrics name
149+
When a registered metrics has to be used 3 key pieces of information are required:
150+
- Name
150151
- Value
151152
- A set of key-value pairs called tags or labels.
152153

docs/advanced-guide/using-publisher-subscriber/page.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Publisher Subscriber
2+
23
Publisher Subscriber is an architectural design pattern for asynchronous communication between different entities.
34
These could be different applications or different instances of the same application.
45
Thus, the movement of messages between the components is made possible without the components being aware of each other's
@@ -7,6 +8,7 @@ This makes the application/system more flexible and scalable as each component c
78
scaled and maintained according to its own requirement.
89

910
## Design choice
11+
1012
In GoFr application if a user wants to use the Publisher-Subscriber design, it supports two message brokers—Apache Kafka
1113
and Google PubSub.
1214
The initialization of the PubSub is done in an IoC container which handles the PubSub client dependency.
@@ -17,6 +19,7 @@ to get a single message or publish a message on the message broker.
1719
> Container is part of the GoFr Context
1820
1921
## Configuration and Setup
22+
2023
Some of the configurations that are required to configure the PubSub backend that an application is to use
2124
that are specific for the type of message broker user wants to use.
2225
`PUBSUB_BACKEND` defines which message broker the application needs to use.

docs/quick-start/introduction/page.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ The `hello-world` server involves three essential steps:
7777
> HTTP Handler functions should follow the `func(ctx *gofr.Context) (interface{}, error)` signature.
7878
> They take a context as input, returning two values: the response data and an error (set to `nil` when there is no error).
7979
80-
In GoFr `ctx *gofr.Context` serves as a wrapper for requests, responses, and dependencies, providing various functionalities.
81-
82-
For more details about context, refer {% new-tab-link newtab=false title="here" href="/docs/references/context" /%}.
80+
GoFr {% new-tab-link newtab=false title="context" href="/docs/references/context" /%} `ctx *gofr.Context` serves as a wrapper for requests, responses, and dependencies, providing various functionalities.
8381

8482
3. **Starting the server**
8583

examples/using-subscriber/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
This GoFr example demonstrates a simple Subscriber that subscribes asynchronously to a given topic and commits based
44
on the handler response.
55

6-
76
### To run the example follow the below steps:
87

98
- Run the docker image of kafka and zookeeper and ensure that your provided topics are created before subscribing.

pkg/gofr/datasource/pubsub/google/google.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package google provides a client for interacting with Google Cloud Pub/Sub.This package facilitates interaction with
2+
// Google Cloud Pub/Sub, allowing publishing and subscribing to topics, managing subscriptions, and handling messages.
13
package google
24

35
import (

pkg/gofr/datasource/pubsub/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package pubsub provides a foundation for implementing pub/sub clients for various message brokers such as google pub-sub,
2+
// kafka and MQTT. It defines interfaces for publishing and subscribing to messages, managing topics, and handling messages.
13
package pubsub
24

35
import (

0 commit comments

Comments
 (0)