package template const templateRouter = `package register import ( "net" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "gitter.top/coco/coco" "google.golang.org/grpc" "google.golang.org/grpc/reflection" "{{.Name}}/config" "{{.Name}}/gen/wire" ) type Register struct{ *config.ServerConf } func NewRegister() *Register { return &Register{config.Get()} } // NewRouter 注册路由 func (receiver *Register) NewRouter() error { // 从这里开始实例化路由注册器 var register = coco.NewRegister() register.DefaultRouter(coco.WithListenAddress(receiver.ServerListen), coco.WithGinMode(gin.ReleaseMode), coco.WithCors(), coco.WithRecovery()) // register.RegisterStruct(wire.InitFileService()) _ = register.PreRun(func() error { engine := register.RawEngine() engine.Static("/assets", "./webui/dist/assets") engine.NoRoute(func(ctx *gin.Context) { ctx.File("./webui/dist/index.html") }) return nil }) logrus.Infof("start api server: http://%s", receiver.ServerListen) register.Run() return nil } // NewGrpc 注册grpc服务端 func (receiver *Register) NewGrpc() error { lis, err := net.Listen("tcp", receiver.GrpcListen) if err != nil { logrus.Fatalf("failed to listen grpc: %v", err) } s := grpc.NewServer() //genv1.RegisterFileServiceServer(s, &rpcv1.FileService{}) // Register reflection service on gRPC server. reflection.Register(s) logrus.Infof("grpc server: %s", receiver.GrpcListen) if err := s.Serve(lis); err != nil { logrus.Fatalf("failed to serve grpc: %v", err) } return nil } `