Skip to content

Commit

Permalink
chat server created
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMartynenko committed Feb 14, 2024
1 parent 575adfb commit ac1fb87
Show file tree
Hide file tree
Showing 11 changed files with 1,007 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Go

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
cache-dependency-path: go.sum

- name: Build
run: go build -o ./bin/ -v ./...

- name: Test
run: go test -v ./...

linter:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: v1.53

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
#
# Note: By default, the `.golangci.yml` file should be at the root of the repository.
# The location of the configuration file can be changed by using `--config=`
args: --timeout=30m --config=./.golangci.pipeline.yaml --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true, then all caching functionality will be completely disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true, then the action won't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true, then the action won't cache or restore ~/.cache/go-build.
# skip-build-cache: true

# Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
/bin/
Empty file added .golangci.pipeline.yaml
Empty file.
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
LOCAL_BIN:=$(CURDIR)/bin

install-golangci-lint:
GOBIN=$(LOCAL_BIN) go install github.com/golangci/golangci-lint/cmd/[email protected]

lint:
GOBIN=$(LOCAL_BIN) golangci-lint run ./... --config .golangci.pipeline.yaml

install-deps:
GOBIN=$(LOCAL_BIN) go install google.golang.org/protobuf/cmd/[email protected]
GOBIN=$(LOCAL_BIN) go install -mod=mod google.golang.org/grpc/cmd/[email protected]

get-deps:
go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc


generate:
make generate-chat-api

generate-chat-api:
mkdir -p pkg/chat_v1
protoc --proto_path api/chat_v1 \
--go_out=pkg/chat_v1 --go_opt=paths=source_relative \
--plugin=protoc-gen-go=bin/protoc-gen-go \
--go-grpc_out=pkg/chat_v1 --go-grpc_opt=paths=source_relative \
--plugin=protoc-gen-go-grpc=bin/protoc-gen-go-grpc \
api/chat_v1/chat_api.proto
46 changes: 46 additions & 0 deletions api/chat_v1/chat_api.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
syntax = "proto3";

package chat_v1;

import "google/protobuf/timestamp.proto";
import "google/protobuf/empty.proto";

option go_package = "github.com/AndreiMartynenko/chat-server/grpc/pkg/chat_v1;chat_v1";

service ChatAPIServices {
rpc Create (CreateNewChatRequest) returns (CreateNewChatResponse);
rpc Delete (DeleteChatRequest) returns (DeleteChatResponse);
rpc SendMessage (SendMessageRequest) returns (SendMessageResponse);
}

// Create NewChat Request
message CreateNewChatRequest {
repeated string usernames = 1;
}

// Create NewChat Response
message CreateNewChatResponse {
int64 id = 1;
}

// Delete Chat Request
message DeleteChatRequest {
int64 id = 1;
}

message DeleteChatResponse {
google.protobuf.Empty delete_response = 1;
}

// SendMessage Request
message SendMessageRequest {
string from = 1;
string text = 2;
google.protobuf.Timestamp timestamp = 3;
}

// SendMessage Response
message SendMessageResponse {
google.protobuf.Empty send_message_response = 1;
}

39 changes: 39 additions & 0 deletions cmd/grpc_client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"context"
"log"
"time"

"github.com/AndreiMartynenko/chat-server/pkg/chat_v1"
"github.com/fatih/color"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

const (
address = "localhost:50051"
)

var usernames = []string{"Bill", "Jack"}

func main() {
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("failed toconnect to server: %v", err)
}
defer conn.Close()

c := chat_v1.NewChatAPIServicesClient(conn)

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

r, err := c.Create(ctx, &chat_v1.CreateNewChatRequest{Usernames: usernames})
if err != nil {
log.Fatalf("failed to get user by id: %v", err)
}
log.Printf("New chat created with ID: %d", r.Id)
log.Printf(color.RedString("chat info: \n"), color.GreenString("%+v", r.Id))

}
74 changes: 74 additions & 0 deletions cmd/grpc_server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

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

"github.com/AndreiMartynenko/chat-server/pkg/chat_v1"
"github.com/brianvoe/gofakeit"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

const grpcPort = 50051

type server struct {
chat_v1.UnimplementedChatAPIServicesServer
}

func (srv *server) Create(ctx context.Context, req *chat_v1.CreateNewChatRequest) (*chat_v1.CreateNewChatResponse, error) {
log.Printf("Create New Chat request received: %v", req)

//For testing purposes
// response := &chat_v1.CreateNewChatResponse{
// Id: 1345,
// }
// return response, nil
return &chat_v1.CreateNewChatResponse{
Id: gofakeit.Int64(),
}, nil

}

func (srv *server) Delete(ctx context.Context, req *chat_v1.DeleteChatRequest) (*chat_v1.DeleteChatResponse, error) {
log.Printf("Delete Chat request received: %v", req)

return &chat_v1.DeleteChatResponse{DeleteResponse: &empty.Empty{}}, nil

}

func (srv *server) SendMessage(ctx context.Context, req *chat_v1.SendMessageRequest) (*chat_v1.SendMessageResponse, error) {
log.Printf("SendMessageRequest received: %v", req)

return &chat_v1.SendMessageResponse{SendMessageResponse: &empty.Empty{}}, nil
}

func main() {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", grpcPort))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

srv := grpc.NewServer()

/*
Reflection in this context allows gRPC clients to query information
about the gRPC server's services dynamically at runtime.
It enables tools like gRPC's command-line interface (grpc_cli)
and gRPC's web-based GUI (grpcui) to inspect the server's
services and make RPC calls without needing to know
the specifics of each service beforehand.
*/
reflection.Register(srv)
chat_v1.RegisterChatAPIServicesServer(srv, &server{})

log.Printf("server listening at %v", lis.Addr())

if err = srv.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}

}
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/AndreiMartynenko/chat-server

go 1.20

require (
github.com/brianvoe/gofakeit v3.18.0+incompatible
github.com/fatih/color v1.16.0
github.com/golang/protobuf v1.5.3
google.golang.org/grpc v1.61.1
google.golang.org/protobuf v1.32.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
)
31 changes: 31 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 h1:Jyp0Hsi0bmHXG6k9eATXoYtjd6e2UzZ1SCn/wIupY14=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:oQ5rr10WTTMvP4A36n8JpR1OrO1BEiV4f78CneXZxkA=
google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY=
google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
Loading

0 comments on commit ac1fb87

Please sign in to comment.