Skip to content

Commit

Permalink
chore: fix the indentation of Markdown fenced code blocks
Browse files Browse the repository at this point in the history
There are no such a thing as an indented fenced code block in Markdown.

This commit fixes the indentation of fenced code blocks.

Files are then formatted with mdsf tool.
  • Loading branch information
ccoVeille committed Jan 18, 2025
1 parent fa30ecf commit 838a8d7
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 110 deletions.
94 changes: 49 additions & 45 deletions docs/advanced-guide/grpc/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@ GoFr enables you to create gRPC handlers efficiently while leveraging GoFr's con
**1. Protocol Buffer Compiler (`protoc`) Installation:**

- **Linux (using `apt` or `apt-get`):**

```bash
sudo apt install -y protobuf-compiler
protoc --version # Ensure compiler version is 3+
```
sudo apt install -y protobuf-compiler
protoc --version # Ensure compiler version is 3+
```

- **macOS (using Homebrew):**

```bash
brew install protobuf
protoc --version # Ensure compiler version is 3+
brew install protobuf
protoc --version # Ensure compiler version is 3+
```

**2. Go Plugins for Protocol Compiler:**

a. Install protocol compiler plugins for Go:

```bash
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
```
```bash
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
```

b. Update `PATH` for `protoc` to locate the plugins:

```bash
export PATH="$PATH:$(go env GOPATH)/bin"
```
```bash
export PATH="$PATH:$(go env GOPATH)/bin"
```

## Creating Protocol Buffers

Expand All @@ -44,16 +45,16 @@ For a detailed guide, refer to the official gRPC documentation's tutorial: {% ne

Create a `.proto` file (e.g., `customer.proto`) to define your service and the RPC methods it provides:

```protobuf
// Indicates the protocol buffer version that is being used
syntax = "proto3";
// Indicates the go package where the generated file will be produced
option go_package = "path/to/your/proto/file";
```protobuf
// Indicates the protocol buffer version that is being used
syntax = "proto3";
// Indicates the go package where the generated file will be produced
option go_package = "path/to/your/proto/file";
service {serviceName}Service {
rpc {serviceMethod} ({serviceRequest}) returns ({serviceResponse}) {}
}
```
service {serviceName}Service {
rpc {serviceMethod} ({serviceRequest}) returns ({serviceResponse}) {}
}
```

**2. Specify Request and Response Types:**

Expand All @@ -79,14 +80,14 @@ string address = 3;

Run the following command to generate Go code using the Go gRPC plugins:

```bash
protoc \
--go_out=. \
--go_opt=paths=source_relative \
--go-grpc_out=. \
--go-grpc_opt=paths=source_relative \
{serviceName}.proto
```
```bash
protoc \
--go_out=. \
--go_opt=paths=source_relative \
--go-grpc_out=. \
--go-grpc_opt=paths=source_relative \
{serviceName}.proto
```

This command generates two files, `{serviceName}.pb.go` and `{serviceName}_grpc.pb.go`, containing the necessary code for performing RPC calls.

Expand All @@ -101,9 +102,10 @@ go install gofr.dev/cli/gofr@latest
```

**1. Use the `gofr wrap grpc` Command:**
```bash
gofr wrap grpc -proto=./path/your/proto/file
```

```bash
gofr wrap grpc -proto=./path/your/proto/file
```

This command leverages the `gofr-cli` to generate a `{serviceName}_server.go` file (e.g., `CustomerServer.go`)
containing a template for your gRPC server implementation, including context support, in the same directory as
Expand All @@ -120,24 +122,26 @@ that of the specified proto file.

**1. Import Necessary Packages:**

```go
import (
"gofr.dev/pkg/gofr"
"path/to/your/generated-grpc-server/packageName"
)
```
```go
import (
"path/to/your/generated-grpc-server/packageName"

"gofr.dev/pkg/gofr"
)
```

**2. Register the Service in your `main.go`:**

```go
func main() {
app := gofr.New()
```go
func main() {
app := gofr.New()

packageName.Register{serviceName}ServerWithGofr(app, &packageName.{serviceName}GoFrServer{})

packageName.Register{serviceName}ServerWithGofr(app, &packageName.{serviceName}GoFrServer{})
app.Run()
}
```

app.Run()
}
```
>Note: By default, gRPC server will run on port 9000, to customize the port users can set `GRPC_PORT` config in the .env
> ##### Check out the example of setting up a gRPC server in GoFr: [Visit GitHub](https://github.com/gofr-dev/gofr/blob/main/examples/grpc-server/main.go)
31 changes: 16 additions & 15 deletions docs/advanced-guide/setting-custom-response-headers/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ GoFr simplifies the process of adding custom HTTP response headers and metadata
- Keys must be strings, and values can be of any type.

When metadata is included, the response structure is:

```json
{
"data": {},
Expand Down Expand Up @@ -92,26 +93,26 @@ When metadata is included, the response contains the metadata field:

```json
{
"data": {
"message": "Hello, World!"
},
"metadata": {
"environment": "staging",
"timestamp": "2024-12-23T12:34:56Z"
}
}
```
"data": {
"message": "Hello, World!"
},
"metadata": {
"environment": "staging",
"timestamp": "2024-12-23T12:34:56Z"
}
}
```

#### Response without Metadata:
If no metadata is provided, the response only includes the data field:

```json
{
"data": {
"message": "Hello, World!"
}
}
```
{
"data": {
"message": "Hello, World!"
}
}
```


This functionality offers a convenient, structured way to include additional response information without altering the
Expand Down
108 changes: 58 additions & 50 deletions docs/references/context/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,70 +18,78 @@ user access to dependencies.
parts of the request.

- `Context()` - to access the context associated with the incoming request
```go
ctx.Request.Context()
```

```go
ctx.Request.Context()
```

- `Param(string)` - to access the query parameters present in the request, it returns the value of the key provided
```go
// Example: Request is /configs?key1=value1&key2=value2
value := ctx.Request.Param("key1")
// value = "value1"
```

```go
// Example: Request is /configs?key1=value1&key2=value2
value := ctx.Request.Param("key1")
// value = "value1"
```

- `PathParam(string)` - to retrieve the path parameters
```go
// Consider the path to be /employee/{id}
id := ctx.Request.PathParam("id")
```

```go
// Consider the path to be /employee/{id}
id := ctx.Request.PathParam("id")
```
- `Bind(interface{})` - to access a decoded format of the request body, the body is mapped to the interface provided

```go
// incoming request body is
// {
// "name" : "trident",
// "category" : "snacks"
// }

type product struct{
Name string `json:"name"`
Category string `json:"category"`
}

var p product
ctx.Bind(&p)
// the Bind() method will map the incoming request to variable p
```
- `Binding multipart-form data / urlencoded form data `
- To bind multipart-form data or url-encoded form, you can use the Bind method similarly. The struct fields should be tagged appropriately
```go
// incoming request body is
// {
// "name" : "trident",
// "category" : "snacks"
// }

type product struct{
Name string `json:"name"`
Category string `json:"category"`
}

var p product
ctx.Bind(&p)
// the Bind() method will map the incoming request to variable p
```

- `Binding multipart-form data / urlencoded form data `
- To bind multipart-form data or url-encoded form, you can use the Bind method similarly. The struct fields should be tagged appropriately
to map the form fields to the struct fields. The supported content types are `multipart/form-data` and `application/x-www-form-urlencoded`

```go
type Data struct {
Name string `form:"name"`

Compressed file.Zip `file:"upload"`
```go
type Data struct {
Name string `form:"name"`

FileHeader *multipart.FileHeader `file:"file_upload"`
}
```
Compressed file.Zip `file:"upload"`

FileHeader *multipart.FileHeader `file:"file_upload"`
}
```

- The `form` tag is used to bind non-file fields.
- The `file` tag is used to bind file fields. If the tag is not present, the field name is used as the key.


- `HostName()` - to access the host name for the incoming request
```go
// for example if request is made from xyz.com
host := ctx.Request.HostName()
// the host would be http://xyz.com
// Note: the protocol if not provided in the headers will be set to http by default
```

```go
// for example if request is made from xyz.com
host := ctx.Request.HostName()
// the host would be http://xyz.com
// Note: the protocol if not provided in the headers will be set to http by default
```

- `Params(string)` - to access all query parameters for a given key returning slice of strings.
```go
// Example: Request is /search?category=books,electronics&category=tech
values := ctx.Request.Params("category")
// values = []string{"books", "electronics", "tech"}
```

```go
// Example: Request is /search?category=books,electronics&category=tech
values := ctx.Request.Params("category")
// values = []string{"books", "electronics", "tech"}
```

## Accessing dependencies

Expand Down

0 comments on commit 838a8d7

Please sign in to comment.