README
ΒΆ
pingkratos
Simple and fast Ping service package to support the Kratos framework, with both gRPC and HTTP protocols.
CHINESE README
Features
β Simple Design: Clean and straightforward service structure β Dual-Protocol Support: Supports both gRPC and HTTP protocols β Native Integration: Seamless integration with the Kratos framework β Built-in Tests: Comprehensive test coverage included β Modern Schema: Uses Protocol Buffers to define services β Zero Config: Out-of-the-box Ping service implementation
Installation
go get github.com/orzkratos/pingkratos
Usage
HTTP Example
package main
import (
"time"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/orzkratos/pingkratos/clientpingkratos"
"github.com/orzkratos/pingkratos/serverpingkratos"
"github.com/orzkratos/zapkratos"
"github.com/yyle88/must"
"github.com/yyle88/rese"
"github.com/yyle88/zaplog"
"go.uber.org/zap"
)
func main() {
// Setup logging to show ping request logs
zapKratos := zapkratos.NewZapKratos(zaplog.LOGGER, zapkratos.NewOptions())
// Setup HTTP service on port 8000
httpSrv := http.NewServer(
http.Address(":8000"),
http.Middleware(
recovery.Recovery(),
logging.Server(zapKratos.GetLogger("HTTP")),
),
http.Timeout(time.Minute),
)
// Setup ping service
pingService := serverpingkratos.NewPingService(zapKratos.GetLogger("PING"))
clientpingkratos.RegisterPingHTTPServer(httpSrv, pingService)
// Setup and start application
app := kratos.New(
kratos.Name("pingkratos-http-demo"),
kratos.Server(httpSrv),
)
zaplog.LOG.Info("Starting HTTP Ping service", zap.String("address", ":8000"))
// Run application (awaiting shutdown)
must.Done(app.Run())
defer rese.F0(app.Stop)
}
β¬οΈ Source: Source
gRPC Example
package main
import (
"time"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/orzkratos/pingkratos/clientpingkratos"
"github.com/orzkratos/pingkratos/serverpingkratos"
"github.com/orzkratos/zapkratos"
"github.com/yyle88/must"
"github.com/yyle88/rese"
"github.com/yyle88/zaplog"
"go.uber.org/zap"
)
func main() {
// Setup logging to show ping request logs
zapKratos := zapkratos.NewZapKratos(zaplog.LOGGER, zapkratos.NewOptions())
// Setup gRPC service on port 9000
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(
recovery.Recovery(),
logging.Server(zapKratos.GetLogger("GRPC")),
),
grpc.Timeout(time.Minute),
)
// Setup ping service
pingService := serverpingkratos.NewPingService(zapKratos.GetLogger("PING"))
clientpingkratos.RegisterPingServer(grpcSrv, pingService)
// Setup and start application
app := kratos.New(
kratos.Name("pingkratos-grpc-demo"),
kratos.Server(grpcSrv),
)
zaplog.LOG.Info("Starting gRPC Ping service", zap.String("address", ":9000"))
// Run application (awaiting shutdown)
must.Done(app.Run())
defer rese.F0(app.Stop)
}
β¬οΈ Source: Source
Dependencies
Core Dependencies
github.com/go-kratos/kratos/v2- Kratos frameworkgoogle.golang.org/grpc- gRPC supportgoogle.golang.org/protobuf- Protocol Buffersgithub.com/yyle88/*- Utilities
Examples
Integration with Kratos Project
To integrate pingkratos into the Kratos project, follow these steps:
1. Add Dependency
go get github.com/orzkratos/pingkratos
2. Setup Wire Provider
In internal/service/service.go:
import (
"github.com/google/wire"
"github.com/orzkratos/pingkratos/serverpingkratos"
)
var ProviderSet = wire.NewSet(
NewGreeterService,
serverpingkratos.NewPingService,
)
3. Setup HTTP Endpoint
In internal/server/http.go:
import (
"github.com/orzkratos/pingkratos/clientpingkratos"
"github.com/orzkratos/pingkratos/serverpingkratos"
)
func NewHTTPServer(
c *conf.Server,
greeter *service.GreeterService,
pingService *serverpingkratos.PingService,
logger log.Logger,
) *http.Server {
srv := http.NewServer(opts...)
v1.RegisterGreeterHTTPServer(srv, greeter)
clientpingkratos.RegisterPingHTTPServer(srv, pingService)
return srv
}
4. Setup gRPC Endpoint
In internal/server/grpc.go:
import (
"github.com/orzkratos/pingkratos/clientpingkratos"
"github.com/orzkratos/pingkratos/serverpingkratos"
)
func NewGRPCServer(
c *conf.Server,
greeter *service.GreeterService,
pingService *serverpingkratos.PingService,
logger log.Logger,
) *grpc.Server {
srv := grpc.NewServer(opts...)
v1.RegisterGreeterServer(srv, greeter)
clientpingkratos.RegisterPingServer(srv, pingService)
return srv
}
5. Generate Wire Code
wire ./cmd/demo-app/...
Demo Projects
Complete working examples:
- pingkratos-demos - Complete Kratos project integration
Unit test examples: TEST.
π License
MIT License. See LICENSE.
π€ Contributing
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Found a mistake? Open an issue on GitHub with reproduction steps
- π‘ Have a feature idea? Create an issue to discuss the suggestion
- π Documentation confusing? Report it so we can improve
- π Need new features? Share the use cases to help us understand requirements
- β‘ Performance issue? Help us optimize through reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo to get new releases and features
- π Success stories? Share how this package improved the workflow
- π¬ Feedback? We welcome suggestions and comments
π§ Development
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git). - Navigate: Navigate to the cloned project (
cd repo-name) - Branch: Create a feature branch (
git checkout -b feature/xxx). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes and use significant commit messages
- Stage: Stage changes (
git add .) - Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
π Support
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! πππ