my go-kratos Project Template base on offical kratos layout
The BSR allows 10 unauthenticated code generation requests per hour, with a burst of up to 10 requests. If you send more than 10 unauthenticated requests per hour using remote plugins, you’ll receive a rate limit error. https://buf.build/docs/bsr/rate-limits
- use go-task rather than make
- use buf for proto build
- built in zap with lumbjack
- use goreleaser to cross build
goreleaser (Optional)
go install github.com/goreleaser/goreleaser@latestTask (Optional)
go install github.com/go-task/task/v3/cmd/task@latestBuf
go install github.com/bufbuild/buf/cmd/buf@latestfor more info, please refer buf document
AIP Go
This template tracks aip-go as a Go tool dependency and uses
go tool protoc-gen-go-aip from buf.gen.yaml, so the generator version is kept in go.mod.
The generated *_aip.go resource-name helpers are produced when your protobuf APIs use
standard Google AIP google.api.resource annotations. Runtime helpers
for pagination, filtering, ordering, and field masks are available from go.einride.tech/aip.
aip-go also provides helpers for common AIP patterns:
| Package | AIP | Purpose |
|---|---|---|
pagination |
AIP-132 | Parse and generate offset-based pagination tokens. |
filtering |
AIP-160 | Parse filter expressions such as filter=name="foo". |
ordering |
AIP-132 | Parse order_by strings. |
fieldmask |
AIP-134 / AIP-161 | Validate and apply field masks for update APIs. |
fieldbehavior |
AIP-203 | Handle field behavior annotations such as REQUIRED and OUTPUT_ONLY. |
resourcename |
AIP-122 | Validate and parse resource names such as shelves/123/books/456. |
resourceid |
AIP-122 | Generate and validate resource IDs. |
validation |
General | Validate common request parameters. |
How to use these helpers in this template:
- Define the public API shape in
proto/.- Use
google.api.resourceon resource messages when you want generated resource-name helpers. - Add standard request fields such as
page_size,page_token,filter,order_by, andupdate_maskwhen your API needs List or Update behavior. - Keep HTTP routes in
google.api.http; the Kratos HTTP plugin will keep generating the route handlers.
- Use
- Run
buf generate.- Protobuf, gRPC, HTTP, validation, OpenAPI, and AIP helper files are generated into
api/anddocs/. *_aip.gofiles come fromprotoc-gen-go-aipand are generated from resource annotations.
- Protobuf, gRPC, HTTP, validation, OpenAPI, and AIP helper files are generated into
- Use the helpers in
internal/serviceorinternal/biz.- Resource names: use generated helpers such as
v1.ShelfResourceName.UnmarshalString(request.GetName()). - Pagination: use
pagination.ParsePageToken(request)and setresponse.NextPageToken = pageToken.Next(request).String()when there is another page. - Filtering: build declarations with
filtering.NewDeclarations(...), then callfiltering.ParseFilter(request, declarations). - Ordering: call
ordering.ParseOrderBy(request), then validate allowed paths withorderBy.ValidateForPaths(...)ororderBy.ValidateForMessage(...). - Update masks: validate with
fieldmask.Validate(request.GetUpdateMask(), request.GetResource()), then apply withfieldmask.Update(...). - Field behavior: use
fieldbehavior.ClearFields(...),ValidateRequiredFields(...), orValidateImmutableFieldsNotChanged(...)around create and update requests. - Resource IDs: use
resourceid.ValidateUserSettable(id)for user-provided IDs orresourceid.NewSystemGenerated()for server-generated IDs.
- Resource names: use generated helpers such as
The AIP helpers parse, validate, and normalize API-level input. They do not build SQL or storage queries for you; convert the parsed filter, order, page token, or field mask into repository query parameters in your own internal/biz or internal/data code.
Example:
proto/helloworld/v1/greeter.proto defines a Shelf resource with the pattern
shelves/{shelf} and a GetShelf API. After buf generate, api/helloworld/v1/greeter_aip.go
contains the generated ShelfResourceName helper. Run the service and request
GET /v1/shelves/default to see the example response.
kratos
go install github.com/go-kratos/kratos/cmd/kratos/v3@latest
kratos upgradeif you want to build all platform simply,you may need install goreleaser,and build with
goreleaser build --snapshot --cleanalso you can create a release in your github,the compile is auto completed
run the command:
kratos new <your App Name> -r https://github.com/tpl-x/kratos.git
or
kratos new <your App Name> -r git@github.com:tpl-x/kratos.git
to create your first Application
to generate api only using buf ,you can follow the steps below:
step 1:update buf dep
buf dep updateif you want to clean up unused dep. use
buf dep prunestep 2 :generate api from protobuf
buf generate