-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cmd api and merge service together
- Loading branch information
Showing
9 changed files
with
191 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net" | ||
|
||
"github.com/alice890308/blog-server/modules/api/dao" | ||
"github.com/alice890308/blog-server/modules/api/pb" | ||
"github.com/alice890308/blog-server/modules/api/service" | ||
"github.com/alice890308/blog-server/pkg/logkit" | ||
"github.com/alice890308/blog-server/pkg/mongokit" | ||
"github.com/alice890308/blog-server/pkg/runkit" | ||
flags "github.com/jessevdk/go-flags" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func newAPICommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "api", | ||
Short: "start api server", | ||
RunE: runAPI, | ||
} | ||
} | ||
|
||
type APIArgs struct { | ||
GRPCAddr string `long:"grpc_addr" env:"RCPC_ADDR" default:"8081"` | ||
runkit.GracefulConfig `group:"graceful" namespace:"graceful" env-namespace:"GRACEFUL"` | ||
logkit.LoggerConfig `group:"logger" namespace:"logger" env-namespace:"LOGGER"` | ||
mongokit.MongoConfig `group:"mongo" namespace:"mongo" env-namespace:"MONGO"` | ||
} | ||
|
||
func runAPI(_ *cobra.Command, _ []string) error { | ||
ctx := context.Background() | ||
|
||
var args APIArgs | ||
if _, err := flags.NewParser(&args, flags.Default).Parse(); err != nil { | ||
log.Fatal("failed to parse flag", err.Error()) | ||
} | ||
|
||
logger := logkit.NewLogger(&args.LoggerConfig) | ||
defer func() { | ||
_ = logger.Sync() | ||
}() | ||
|
||
ctx = logger.WithContext(ctx) | ||
|
||
mongoClient := mongokit.NewMongoClient(ctx, &args.MongoConfig) | ||
defer func() { | ||
if err := mongoClient.Close(); err != nil { | ||
logger.Fatal("failed to close mongo client", zap.Error(err)) | ||
} | ||
}() | ||
|
||
postDAO := dao.NewMongoPostDAO(mongoClient.Database().Collection("posts")) | ||
userDAO := dao.NewMongoUserDAO(mongoClient.Database().Collection("users")) | ||
svc := service.NewService(postDAO, userDAO) | ||
|
||
logger.Info("listen to gRPC addr", zap.String("grpc_addr", args.GRPCAddr)) | ||
lis, err := net.Listen("tcp", args.GRPCAddr) | ||
if err != nil { | ||
logger.Fatal("failed to list gRPC addr", zap.Error(err)) | ||
} | ||
defer func() { | ||
if err := lis.Close(); err != nil { | ||
logger.Fatal("failed to close gRPC listner", zap.Error(err)) | ||
} | ||
}() | ||
|
||
return runkit.GracefunRun(serveGRPC(lis, svc, logger), &args.GracefulConfig) | ||
} | ||
|
||
func serveGRPC(lis net.Listener, svc *service.Service, logger *logkit.Logger, opt ...grpc.ServerOption) runkit.GracefulRunFunc { | ||
grpcServer := grpc.NewServer(opt...) | ||
pb.RegisterPostServer(grpcServer, svc) | ||
pb.RegisterUserServer(grpcServer, svc) | ||
|
||
return func(ctx context.Context) error { | ||
go func() { | ||
if err := grpcServer.Serve(lis); err != nil { | ||
logger.Error("failed to run gRPC srever", zap.Error(err)) | ||
} | ||
}() | ||
|
||
<-ctx.Done() | ||
|
||
grpcServer.GracefulStop() | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package api | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func NewAPICommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "api [service]", | ||
Short: "start api's service", | ||
} | ||
|
||
cmd.AddCommand(newAPICommand()) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/alice890308/blog-server/cmd/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func main() { | ||
cmd := &cobra.Command{ | ||
Use: "blog-server [module]", | ||
Short: "Blog Server module entrypoints", | ||
} | ||
|
||
cmd.AddCommand(api.NewAPICommand()) | ||
|
||
if err := cmd.Execute(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/alice890308/blog-server/modules/api/dao" | ||
"github.com/alice890308/blog-server/modules/api/pb" | ||
) | ||
|
||
type Service struct { | ||
pb.UnimplementedPostServer | ||
pb.UnimplementedUserServer | ||
|
||
userDAO dao.UserDAO | ||
postDAO dao.PostDAO | ||
} | ||
|
||
func NewService(postDAO dao.PostDAO, userDAO dao.UserDAO) *Service { | ||
return &Service{ | ||
userDAO: userDAO, | ||
postDAO: postDAO, | ||
} | ||
} |
Oops, something went wrong.