Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

сервер #3

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не пон откуда ручки взялись? ты Прото создал?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Прото в api/chat_v1

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
Loading