1
0
mirror of https://github.com/go-kratos/kratos.git synced 2026-06-09 20:42:40 +02:00
TONY dc13681fec fix(transport/http): bind named body field for websocket client streaming (#3837)
Client-streaming RPCs are served over WebSocket, whose handshake is always
an HTTP GET. A rule declaring a named body field (e.g. body: "data") on such
a method produced two problems in protoc-gen-go-http:

1. buildHTTPRule emitted a spurious "GET ... body should not be declared"
   warning, even though the body is legitimate for a streamed GET.
2. The named field never bound: the client sent the whole message while the
   server decoded each frame into the top-level message, so the body field
   stayed empty.

Fix both by agreeing on framing at generation time. A new methodDesc.BodyMessage
flag (set when the body field is a singular message-kind field) gates both the
client Send and the server stream option, so the two sides cannot diverge:

- http.go: skip the GET/DELETE body warnings for client-streaming methods;
  unary/SSE GET-with-body still warns. Record BodyMessage on named bodies.
- httpTemplate.tpl: for a streamable message body, the client streams the body
  sub-field and the server receives http.WithStreamBodyField(...); otherwise the
  whole message is sent/decoded.
- transport/http/stream.go: add WithStreamBodyField option; serverStream.Recv
  decodes each frame into the named message field then overlays query + path
  vars. Invalid body-field declarations now return an explicit error instead of
  silently falling back.

Adds template tests for message vs scalar named bodies, a websocket round-trip
test, and a negative test for invalid body fields. Also gitignores the compiled
plugin binary in cmd/protoc-gen-go-http.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-05 14:10:19 +08:00
2021-02-17 17:14:47 +08:00
2024-10-31 10:00:25 +08:00
2023-03-13 15:42:19 +08:00

Build Status GoDoc DeepWiki codeCov Go Report Card License Awesome Go Discord

go-kratos%2Fkratos | Trendshift Go Kratos - A Go framework for microservices. | Product Hunt

Translations: English | 简体中文

Kratos

Kratos is a lightweight Go framework for building cloud-native microservices. It provides small, explicit APIs for transport, middleware, registry, configuration, logging, encoding, and code generation so applications can focus on business logic.

Features

  • API-first development with Protobuf and generated HTTP/gRPC code.
  • Unified transport layer for HTTP and gRPC.
  • Composable middleware for recovery, logging, validation, tracing, metrics, auth, and more.
  • Pluggable registry, configuration, and encoding components.
  • Standard-library log/slog based logging with OpenTelemetry extensions in contrib packages.
  • Consistent metadata, errors, validation, OpenAPI, and code-generation workflows.
  • A contrib ecosystem for optional integrations such as registries, config stores, middleware, encodings, and observability.

Installation

Requirements

Install the CLI

go install github.com/go-kratos/kratos/cmd/kratos/v3@latest
kratos upgrade

Create a Service

kratos new helloworld
cd helloworld
go mod tidy
kratos run

Visit http://localhost:8000/helloworld/kratos after the service starts.

For a fuller generated service flow:

kratos proto add api/helloworld/helloworld.proto
kratos proto client api/helloworld/helloworld.proto
kratos proto server api/helloworld/helloworld.proto -t internal/service
go generate ./...
kratos run

Usage Example

package main

import (
	"github.com/go-kratos/kratos/v3"
	"github.com/go-kratos/kratos/v3/transport/grpc"
	"github.com/go-kratos/kratos/v3/transport/http"
)

func main() {
	httpSrv := http.NewServer(http.Address(":8000"))
	grpcSrv := grpc.NewServer(grpc.Address(":9000"))

	app := kratos.New(
		kratos.Name("helloworld"),
		kratos.Version("v1.0.0"),
		kratos.Server(httpSrv, grpcSrv),
	)
	if err := app.Run(); err != nil {
		panic(err)
	}
}

v3 Migration

Kratos v3 reduces core dependencies and makes previously implicit behavior explicit. Review the v2 to v3 migration guide before upgrading production services.

Further Reading

Development

make test
make lint

Community

Security

If you discover a security vulnerability in Kratos, please contact go-kratos@googlegroups.com. Security reports are handled privately before disclosure.

Contributors

Thank you for contributing to Kratos. The contribution guide is available in the Kratos documentation.

Kratos contributors

Acknowledgments

The following projects influenced Kratos design:

License

Kratos is open-sourced software licensed under the MIT license.

S
Description
Your ultimate Go microservices framework for the cloud-native era.
Readme MIT 80 MiB
Languages
Go 98.2%
Go Template 1.1%
Shell 0.4%
Makefile 0.3%