From 79a6ffda84985f9dc97550ea4fbd8651b603be5d Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Sun, 19 May 2024 18:44:55 +0700 Subject: [PATCH] add README.md --- .gitea/template | 3 ++ README.md | 14 ++++++++- cmd/${REPO_NAME_SNAKE}/main.go | 53 ++++++++++++++++++++++++++++++++-- go.mod | 18 ++++++++++++ internal/app/server.go | 18 ++++++++++++ 5 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 go.mod create mode 100644 internal/app/server.go diff --git a/.gitea/template b/.gitea/template index 6319596..bc052bb 100644 --- a/.gitea/template +++ b/.gitea/template @@ -3,3 +3,6 @@ api/*.proto Makefile cmd/** +internal/** + +README.md diff --git a/README.md b/README.md index b018c21..6e6ff5a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ # template -Шалон для Go сервисов \ No newline at end of file +Шалон для Go сервисов + +Генерация контракта + +```shell +make generate +``` + +Запуск + +```shell +make run +``` diff --git a/cmd/${REPO_NAME_SNAKE}/main.go b/cmd/${REPO_NAME_SNAKE}/main.go index a3dd973..3eb7e8b 100644 --- a/cmd/${REPO_NAME_SNAKE}/main.go +++ b/cmd/${REPO_NAME_SNAKE}/main.go @@ -1,7 +1,56 @@ package main -import "fmt" +import ( + "${REPO_NAME_SNAKE}/internal/app" + proto "${REPO_NAME_SNAKE}/proto" + "context" + "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "log" + "net" + "net/http" +) func main() { - fmt.Println("Hello, World!") + // Create a listener on TCP port + lis, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln("Failed to listen:", err) + } + + // Create a gRPC server object + s := grpc.NewServer() + // Attach the Greeter service to the server + proto.Register${REPO_NAME_PASCAL}Server(s, app.NewServer()) + // Serve gRPC server + log.Println("Serving gRPC on 0.0.0.0:8080") + go func() { + log.Fatalln(s.Serve(lis)) + }() + + // Create a client connection to the gRPC server we just started + // This is where the gRPC-Gateway proxies the requests + conn, err := grpc.NewClient( + "0.0.0.0:8080", + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + if err != nil { + log.Fatalln("Failed to dial server:", err) + } + + gwmux := runtime.NewServeMux() + // Register Greeter + err = proto.Register${REPO_NAME_PASCAL}Handler(context.Background(), gwmux, conn) + if err != nil { + log.Fatalln("Failed to register gateway:", err) + } + + gwServer := &http.Server{ + Addr: ":8090", + Handler: gwmux, + } + + log.Println("Serving gRPC-Gateway on http://0.0.0.0:8090") + log.Fatalln(gwServer.ListenAndServe()) } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..43b10e9 --- /dev/null +++ b/go.mod @@ -0,0 +1,18 @@ +module cake_crm + +go 1.22 + +require ( + github.com/go-pkgz/routegroup v1.1.1 + github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 + google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8 + google.golang.org/grpc v1.64.0 + google.golang.org/protobuf v1.34.1 +) + +require ( + golang.org/x/net v0.23.0 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect +) diff --git a/internal/app/server.go b/internal/app/server.go new file mode 100644 index 0000000..4a1d789 --- /dev/null +++ b/internal/app/server.go @@ -0,0 +1,18 @@ +package app + +import ( + proto "${REPO_NAME_SNAKE}/proto" + "context" +) + +type Server struct { + proto.UnimplementedCRMServer +} + +func NewServer() *Server { + return &Server{} +} + +func (s *Server) Ping(_ context.Context, _ *proto.PingReq) (*proto.PingRsp, error) { + return &proto.PingRsp{}, nil +}