From 838a8d74481ddb8147f7e0515850783469f3c3c6 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Sat, 18 Jan 2025 22:48:43 +0100 Subject: [PATCH] chore: fix the indentation of Markdown fenced code blocks 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. --- docs/advanced-guide/grpc/page.md | 94 +++++++-------- .../setting-custom-response-headers/page.md | 31 ++--- docs/references/context/page.md | 108 ++++++++++-------- 3 files changed, 123 insertions(+), 110 deletions(-) diff --git a/docs/advanced-guide/grpc/page.md b/docs/advanced-guide/grpc/page.md index f24b9972f..cfa168e54 100644 --- a/docs/advanced-guide/grpc/page.md +++ b/docs/advanced-guide/grpc/page.md @@ -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/protoc-gen-go@v1.28 - go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 - ``` +```bash +go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 +go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 +``` 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 @@ -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:** @@ -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. @@ -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 @@ -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) diff --git a/docs/advanced-guide/setting-custom-response-headers/page.md b/docs/advanced-guide/setting-custom-response-headers/page.md index dbafc36b0..c058b98e3 100644 --- a/docs/advanced-guide/setting-custom-response-headers/page.md +++ b/docs/advanced-guide/setting-custom-response-headers/page.md @@ -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": {}, @@ -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 diff --git a/docs/references/context/page.md b/docs/references/context/page.md index 9b46f2abe..41ce4532d 100644 --- a/docs/references/context/page.md +++ b/docs/references/context/page.md @@ -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