# kratos-layout **Repository Path**: esas/kratos-layout ## Basic Information - **Project Name**: kratos-layout - **Description**: Kratos Project Template - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-02-11 - **Last Updated**: 2024-04-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Kratos Project Template ## Install Kratos ``` git clone https://gitee.com/esas/kratos.git cd kratos make all && make install ``` ## Install protoc URI: https://github.com/protocolbuffers/protobuf/releases/tag/v21.1 ``` # MAC OS PROTOC_ZIP=protoc-21.1-osx-x86_64.zip wget -O $PROTOC_ZIP https://github.com/protocolbuffers/protobuf/releases/download/v21.1/$PROTOC_ZIP sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*' rm -f $PROTOC_ZIP # Window PROTOC_ZIP=protoc-21.1-win64.zip # Linux PROTOC_ZIP=protoc-21.1-linux-x86_64.zip wget -O $PROTOC_ZIP https://github.com/protocolbuffers/protobuf/releases/download/v21.1/$PROTOC_ZIP sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*' rm -f $PROTOC_ZIP ``` ### Prepare environment ``` # Install base tools make init ``` ## Create a service ``` # Create a template project # server: app name or service name kratos new server cd server # Add a proto template kratos proto add api/server/server.proto # Generate the proto code kratos proto client api/server/server.proto # Generate the source code of service by proto file kratos proto server api/server/server.proto -t internal/service # Generate the source code of service by proto file, field `id` is mongo object ID kratos proto server api/server/server.proto -t internal/service --use-object-id # Generate the source code of config by proto file(internal/config/conf.proto) make config # Generate model file # go:generate kratos model mongo server.go -m Server -t ../data -c server ## server.go: file name of model ## -m: model name ## -t: target directory ## -c: mongo collection name # gorm:"unique" - The field is unique, generate Add/Delete/Count function for the field # gorm:"index" - The filed is not unique, generate Add/Delete/Count function for the field cat > internal/model/server.go << EOF package model //go:generate kratos model mongo server.go -m Server -t ../data -c server type Server struct { Id string \`json:"id" bson:"_id" gorm:"unique"\` Name string \`json:"name" bson:"name" gorm:"unique"\` Tags []string \`json:"tags" bson:"tags"\` } EOF # Generate source code by Go template make generate # Compile make build # Other compile command go build -o ./bin/ ./... # Execute ./bin/server -conf ./configs ``` ## Generate other auxiliary files by Makefile ``` # Download and update dependencies make init # Generate API files (include: pb.go, http, grpc, validate, swagger) by proto file make api # Generate all files make all ``` ## Automated Initialization (wire) ``` # install wire go get github.com/google/wire/cmd/wire # generate wire cd cmd/server wire ``` ## Docker ```bash # build docker build -t . # run docker run --rm -p 8000:8000 -p 9000:9000 -v :/data/conf ``` ## protobuf常见问题 1. protobuf中保留字段名的下划线 ```proto message XXXEntity { string _id = 1[json_name = "_id"]; string first_name = 2[json_name = "first_name"]; } ``` 2. protobuf类型int64问题 请参考:https://protobuf.dev/programming-guides/proto3/#json 以下定义,protobuf返回的数据number是字符串类型。 ```proto message XXXEntity { int64 number = 1; } ``` 解决方案: 1. 定义double类型,double对应golang的float64 ```proto message XXXEntity { double number = 1; } ``` 注意:当数的大小超过2^53次方时,使用double可能会有问题。 3. 前端定义timestamp为int64类型 与问题2相同,可以在protobuf中定义double,double数的长度足够。 4. 如何支持HTTP GET请求携带body 在proto文件中定义body字段,并添加http选项。 5. 如何在响应中携带不同结构体 目前,只能定义一个包含全部字段的结构体,然后不需要填写的字段使用omitempty实现,待验证 protobuf没有类似golang interface的类型 ## 常见问题 1. 有model但是没有service的情况 在service/greeter.go中添加相关usecase的引用,程序则可以通过访问model全局usecase的方法来访问model。 2. 不需要监听端口 将配置文件中的http和grpc地址均修改为空。程序通过ctrl+c来停止。 3. 不需要mongo 将配置文件中的database.driver修改为空