generated from well-known-components/template-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ws and rpc server handlers (#8)
* new packages * ws & rpc connections * default expiration * local proto
- Loading branch information
Showing
15 changed files
with
3,317 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ dist/ | |
node_modules/ | ||
**/.DS_Store | ||
coverage | ||
.env | ||
.env | ||
protoc3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
PROTOBUF_VERSION = 3.19.1 | ||
PROTOC ?= protoc | ||
UNAME := $(shell uname) | ||
PROTO_FILES := $(wildcard src/*.proto) | ||
export PATH := ts-server/node_modules/.bin:/usr/local/include/:protoc3/bin:$(PATH) | ||
|
||
ifeq ($(UNAME),Darwin) | ||
PROTOBUF_ZIP = protoc-$(PROTOBUF_VERSION)-osx-x86_64.zip | ||
else | ||
PROTOBUF_ZIP = protoc-$(PROTOBUF_VERSION)-linux-x86_64.zip | ||
endif | ||
|
||
# for ts | ||
install_compiler: | ||
@# remove local folder | ||
rm -rf protoc3 || true | ||
|
||
@# Make sure you grab the latest version | ||
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOBUF_VERSION)/$(PROTOBUF_ZIP) | ||
|
||
@# Unzip | ||
unzip $(PROTOBUF_ZIP) -d protoc3 | ||
@# delete the files | ||
rm $(PROTOBUF_ZIP) | ||
|
||
@# move protoc to /usr/local/bin/ | ||
chmod +x protoc3/bin/protoc | ||
|
||
build: install_compiler | ||
@./build-local-proto.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
./protoc3/bin/protoc \ | ||
--plugin=./node_modules/.bin/protoc-gen-ts_proto \ | ||
--ts_proto_opt=esModuleInterop=true,returnObservable=false,outputServices=generic-definitions \ | ||
--ts_proto_out="$(pwd)/src" -I="$(pwd)" \ | ||
"$(pwd)/friendships_ea.proto" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// THIS LIVES HERE FOR NOW, BUT IT SHOULD BE MOVED TO @dcl/protcol when release | ||
|
||
syntax = "proto3"; | ||
package decentraland.social.friendships_ea; | ||
|
||
import "google/protobuf/empty.proto"; | ||
|
||
|
||
// This message is a response that is sent from the server to the client | ||
message FriendshipEventResponse { | ||
oneof body { | ||
RequestResponse request = 1; | ||
AcceptResponse accept = 2; | ||
RejectResponse reject = 4; | ||
DeleteResponse delete = 5; | ||
CancelResponse cancel = 6; | ||
} | ||
} | ||
|
||
message FriendshipEventResponses { | ||
repeated FriendshipEventResponse responses = 1; | ||
} | ||
|
||
message FriendshipEventPayload { | ||
oneof body { | ||
RequestPayload request = 1; | ||
AcceptPayload accept = 2; | ||
RejectPayload reject = 4; | ||
DeletePayload delete = 5; | ||
CancelPayload cancel = 6; | ||
} | ||
} | ||
|
||
message User { string address = 1; } | ||
|
||
message Users { repeated User users = 1; } | ||
|
||
message RequestResponse { | ||
User user = 1; | ||
int64 created_at = 2; | ||
optional string message = 3; | ||
} | ||
|
||
message RequestPayload { | ||
User user = 1; | ||
optional string message = 3; | ||
} | ||
|
||
message Requests { | ||
int64 total = 1; // Total amount of friendship requests | ||
repeated RequestResponse items = 2; | ||
} | ||
|
||
message RequestEvents { | ||
Requests outgoing = 1; // Requests the authed user have sent to users | ||
Requests incoming = 2; // Requests the authed user have received from users | ||
} | ||
|
||
message AcceptResponse { User user = 1; } | ||
|
||
message AcceptPayload { User user = 1; } | ||
|
||
message RejectResponse { User user = 1; } | ||
|
||
message RejectPayload { User user = 1; } | ||
|
||
message DeleteResponse { User user = 1; } | ||
|
||
message DeletePayload { User user = 1; } | ||
|
||
message CancelResponse { User user = 1; } | ||
|
||
message CancelPayload { User user = 1; } | ||
|
||
message UpdateFriendshipPayload { | ||
FriendshipEventPayload event = 1; | ||
} | ||
|
||
message MutualFriendsPayload { | ||
User user = 1; | ||
} | ||
|
||
message BadRequestError { | ||
string message = 1; | ||
} | ||
message UnauthorizedError { | ||
string message = 1; | ||
} | ||
message ForbiddenError { | ||
string message = 1; | ||
} | ||
message TooManyRequestsError { | ||
string message = 1; | ||
} | ||
message InternalServerError { | ||
string message = 1; | ||
} | ||
|
||
message UsersResponse { | ||
oneof response { | ||
Users users = 1; | ||
InternalServerError internal_server_error = 2; | ||
UnauthorizedError unauthorized_error = 3; | ||
ForbiddenError forbidden_error = 4; | ||
TooManyRequestsError too_many_requests_error = 5; | ||
BadRequestError bad_request_error = 6; | ||
} | ||
} | ||
|
||
message RequestEventsResponse { | ||
oneof response { | ||
RequestEvents events = 1; | ||
InternalServerError internal_server_error = 2; | ||
UnauthorizedError unauthorized_error = 3; | ||
ForbiddenError forbidden_error = 4; | ||
TooManyRequestsError too_many_requests_error = 5; | ||
} | ||
} | ||
|
||
message UpdateFriendshipResponse { | ||
oneof response { | ||
FriendshipEventResponse event = 1; | ||
InternalServerError internal_server_error = 2; | ||
UnauthorizedError unauthorized_error = 3; | ||
ForbiddenError forbidden_error = 4; | ||
TooManyRequestsError too_many_requests_error = 5; | ||
BadRequestError bad_request_error = 6; | ||
} | ||
} | ||
|
||
message SubscribeFriendshipEventsUpdatesResponse { | ||
oneof response { | ||
FriendshipEventResponses events = 1; | ||
InternalServerError internal_server_error = 2; | ||
UnauthorizedError unauthorized_error = 3; | ||
ForbiddenError forbidden_error = 4; | ||
TooManyRequestsError too_many_requests_error = 5; | ||
} | ||
} | ||
|
||
service FriendshipsService { | ||
// Get the list of friends for the authenticated user | ||
rpc GetFriends(google.protobuf.Empty) returns (stream UsersResponse) {} | ||
|
||
// Get the list of mutual friends between the authenticated user and the one in the parameter | ||
rpc GetMutualFriends(MutualFriendsPayload) returns (stream UsersResponse) {} | ||
|
||
// Get the list of request events for the authenticated user | ||
rpc GetRequestEvents(google.protobuf.Empty) returns (RequestEventsResponse) {} | ||
|
||
// Update friendship status: REQUEST, ACCEPT, REJECT, CANCEL, DELETE | ||
rpc UpdateFriendshipEvent(UpdateFriendshipPayload) | ||
returns (UpdateFriendshipResponse) {} | ||
|
||
// Subscribe to updates of friendship status: REQUEST, ACCEPT, REJECT, CANCEL, DELETE | ||
rpc SubscribeFriendshipEventsUpdates(google.protobuf.Empty) | ||
returns (stream SubscribeFriendshipEventsUpdatesResponse) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { createRpcServer } from '@dcl/rpc' | ||
import { registerService } from '@dcl/rpc/dist/codegen' | ||
import { | ||
FriendshipsServiceDefinition, | ||
UsersResponse, | ||
SubscribeFriendshipEventsUpdatesResponse, | ||
RequestEventsResponse, | ||
UpdateFriendshipResponse | ||
} from '../friendships_ea' | ||
import { AppComponents, RpcServerContext } from '../types' | ||
|
||
export default function createRpcServerComponent(components: Pick<AppComponents, 'logs'>) { | ||
const { logs } = components | ||
|
||
const server = createRpcServer<RpcServerContext>({ | ||
logger: logs.getLogger('rpc-server') | ||
}) | ||
|
||
const _logger = logs.getLogger('rpc-server-handler') | ||
// Mocked server until we get the new service definition & db queries done | ||
server.setHandler(async function handler(port) { | ||
registerService(port, FriendshipsServiceDefinition, async () => ({ | ||
getFriends(_request, _context) { | ||
const generator = async function* () { | ||
const response: UsersResponse = { | ||
users: { users: [] } | ||
} | ||
yield response | ||
} | ||
|
||
return generator() | ||
}, | ||
getMutualFriends(_request, _context) { | ||
const generator = async function* () { | ||
const response: UsersResponse = { | ||
users: { users: [] } | ||
} | ||
yield response | ||
} | ||
|
||
return generator() | ||
}, | ||
async getRequestEvents(_request, _context) { | ||
const res: RequestEventsResponse = { | ||
events: { | ||
outgoing: { items: [], total: 0 }, | ||
incoming: { items: [], total: 0 } | ||
} | ||
} | ||
return res | ||
}, | ||
async updateFriendshipEvent(_request, _context) { | ||
const res: UpdateFriendshipResponse = { | ||
event: { | ||
accept: { | ||
user: { | ||
address: '0xa' | ||
} | ||
} | ||
} | ||
} | ||
return res | ||
}, | ||
subscribeFriendshipEventsUpdates(_request, _context) { | ||
const generator = async function* () { | ||
const response: SubscribeFriendshipEventsUpdatesResponse = { | ||
events: { | ||
responses: [] | ||
} | ||
} | ||
yield response | ||
} | ||
|
||
return generator() | ||
} | ||
})) | ||
}) | ||
|
||
return server | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { WebSocketServer } from 'ws' | ||
import { IWebSocketComponent } from '../types' | ||
|
||
export async function createWsComponent(): Promise<IWebSocketComponent> { | ||
let wss: WebSocketServer | undefined | ||
|
||
async function start() { | ||
if (wss) return | ||
|
||
wss = new WebSocketServer({ noServer: true }) | ||
} | ||
|
||
async function stop() { | ||
wss?.close() | ||
wss = undefined | ||
} | ||
|
||
await start() | ||
|
||
return { | ||
start, | ||
stop, | ||
ws: wss! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.