Skip to content

Commit

Permalink
Merge pull request #3 from xeeetu/dev-branch
Browse files Browse the repository at this point in the history
сервер
  • Loading branch information
xeeetu authored Aug 16, 2024
2 parents 7be594d + cfefa42 commit 59967b8
Show file tree
Hide file tree
Showing 4 changed files with 640 additions and 2 deletions.
77 changes: 75 additions & 2 deletions cmd/chat_server/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,80 @@
package main

import "fmt"
import (
"context"
"fmt"
"log"
"net"

"github.com/brianvoe/gofakeit"
desc "github.com/xeeetu/chat-server/pkg/chat_v1"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/types/known/emptypb"
)

const (
address = "localhost:50051"
)

type server struct {
desc.UnimplementedChatV1Server
}

// Create создаёт чат, получает массив пользователей
func (s *server) Create(ctx context.Context, in *desc.CreateRequest) (*desc.CreateResponse, error) {
select {
case <-ctx.Done():
// Если контекст отменён, возвращаем ошибку
return nil, ctx.Err()
default:
// Продолжаем выполнение
}

fmt.Printf("Create chat with users: %v", in.Usernames)
return &desc.CreateResponse{Id: gofakeit.Int64()}, nil
}

// Delete удаляет чат по id
func (s *server) Delete(ctx context.Context, in *desc.DeleteRequest) (*emptypb.Empty, error) {
select {
case <-ctx.Done():
// Если контекст отменён, возвращаем ошибку
return nil, ctx.Err()
default:
// Продолжаем выполнение
}

fmt.Printf("Delete chat with id: %d", in.Id)
return &emptypb.Empty{}, nil
}

// SendMessage отправляет сообщение на сервер
func (s *server) SendMessage(ctx context.Context, in *desc.SendMessageRequest) (*emptypb.Empty, error) {
select {
case <-ctx.Done():
// Если контекст отменён, возвращаем ошибку
return nil, ctx.Err()
default:
// Продолжаем выполнение
}

fmt.Printf("Send message: %v", in)
return &emptypb.Empty{}, nil
}

func main() {
fmt.Println("Hello World")
lis, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

s := grpc.NewServer()
reflection.Register(s)

desc.RegisterChatV1Server(s, &server{})

if err = s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
github.com/brianvoe/gofakeit v3.18.0+incompatible h1:wDOmHc9DLG4nRjUVVaxA+CEglKOW72Y5+4WNxUIkjM8=
github.com/brianvoe/gofakeit v3.18.0+incompatible/go.mod h1:kfwdRA90vvNhPutZWfH7WPaDzUjz+CZFqG+rPkOjGOc=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 h1:Zy9XzmMEflZ/MAaA7vNcoebnRAld7FsPW1EeBB7V0m8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
Loading

0 comments on commit 59967b8

Please sign in to comment.