Skip to content

Commit

Permalink
【添加】grpc和http服务
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-lvl committed Jul 25, 2019
1 parent 1368a19 commit 2ff3bc3
Show file tree
Hide file tree
Showing 12 changed files with 818 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GRPC_PROTO_PATH=./grpc/pb
PROTO_FILE=$(GRPC_PROTO_PATH)/snow.proto
.PHONY: gen-proto
go-proto:
protoc --go_out=plugins=grpc:. $(PROTO_FILE)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ go get -u github.com/CyrivlClth/snowserver
## 运行

```shell
snowserver -gp=50051 -hp=8080 -d run
snowserver -gp=50051 -hp=8080 server

# 获取更多运行信息
snowserver -h
Expand Down
76 changes: 76 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package commands

import (
"fmt"

"github.com/gin-gonic/gin"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"

gsrv "github.com/CyrivlClth/snowserver/grpc/server"
hsrv "github.com/CyrivlClth/snowserver/http/server"
)

var (
grpcPort int
httpPort int
)

var ServerFlags = []cli.Flag{
cli.IntFlag{
Name: "gp",
Usage: "指定grpc服务的端口号,默认50051",
Value: 50051,
Destination: &grpcPort,
},
cli.IntFlag{
Name: "hp",
Usage: "指定http服务的端口号,默认50051",
Value: 8080,
Destination: &httpPort,
},
cli.BoolFlag{
Name: "d",
Usage: "指定服务后台运行",
},
}

var ServerCommand = cli.Command{
Name: "server",
Usage: "运行grpc和http服务",
Action: RunAllAction,
Subcommands: []cli.Command{
{
Name: "grpc",
Usage: "仅运行grpc服务",
Action: RunGrpcAction,
},
{
Name: "http",
Usage: "仅运行http服务",
Action: RunHttpAction,
},
},
}

func RunAllAction(c *cli.Context) error {
g := errgroup.Group{}
g.Go(func() error {
return RunGrpcAction(c)
})

g.Go(func() error {
return RunHttpAction(c)
})

return g.Wait()
}

func RunGrpcAction(c *cli.Context) error {
return gsrv.Run(fmt.Sprintf(":%d", grpcPort))
}

func RunHttpAction(c *cli.Context) error {
gin.SetMode(gin.ReleaseMode)
return hsrv.Run(fmt.Sprintf(":%d", httpPort))
}
19 changes: 19 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"github.com/CyrivlClth/snowflake"
"github.com/CyrivlClth/snowflake/idgen"
)

var idGen idgen.IDGenerator

// Init 初始化config
func Init(workerID, dataCenterID int64) (err error) {
idGen, err = snowflake.New(workerID, dataCenterID)
return
}

// IDGenerator 获取全局ID生成器
func IDGenerator() idgen.IDGenerator {
return idGen
}
1 change: 1 addition & 0 deletions grpc/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package client
Loading

0 comments on commit 2ff3bc3

Please sign in to comment.