-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b7d8b4
commit 3aab4d5
Showing
21 changed files
with
506 additions
and
118 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"nest-server": major | ||
"vite-vue3": patch | ||
--- | ||
|
||
feat: nestjs socket.io 集成 |
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,72 @@ | ||
name: nest_server_cicd | ||
|
||
on: | ||
push: | ||
tags: | ||
- 'nest-server@*' | ||
|
||
env: | ||
SSH_PRIVATE_KEY: ${{secrets.SSH_PRIVATE_KEY}} | ||
|
||
jobs: | ||
extract: | ||
runs-on: ubuntu-latest | ||
outputs: | ||
version: ${{ steps.extract_version.outputs.version }} | ||
steps: | ||
- id: extract_version | ||
run: | | ||
echo "version=$(echo ${{ github.ref_name }} | sed 's/nest-server@//')" >> $GITHUB_OUTPUT | ||
build: | ||
needs: extract | ||
runs-on: ubuntu-latest | ||
env: | ||
IMAGE_VERSION: ${{ needs.extract.outputs.version }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Build & Push Image | ||
run: | | ||
echo "${{env.IMAGE_VERSION}}" | ||
docker login --username=${{secrets.DOCKER_USERNAME}} --password=${{secrets.DOCKER_PASSWORD}} ${{secrets.DOCKER_REGISTRY}} | ||
docker build --target nestjs-backend -t ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/fullstack-blog-nest:${{env.IMAGE_VERSION}} . | ||
docker push ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/fullstack-blog-nest:${{env.IMAGE_VERSION}} | ||
deploy: | ||
needs: [extract, build] | ||
runs-on: ubuntu-latest | ||
env: | ||
IMAGE_VERSION: ${{ needs.extract.outputs.version }} | ||
steps: | ||
- name: SSH Auth && Deploy Image | ||
run: | | ||
ssh -V | ||
mkdir -p ~/.ssh | ||
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | ||
chmod 600 ~/.ssh/id_rsa | ||
cat >>~/.ssh/config <<END | ||
Host remote | ||
HostName ${{secrets.SSH_HOST}} | ||
Port 22 | ||
User ${{secrets.SSH_USERNAME}} | ||
IdentityFile ~/.ssh/id_rsa | ||
StrictHostKeyChecking no | ||
END | ||
cat >>~/remote.sh <<END | ||
docker ps | ||
docker login --username=${{secrets.DOCKER_USERNAME}} --password=${{secrets.DOCKER_PASSWORD}} ${{secrets.DOCKER_REGISTRY}} | ||
docker pull ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/fullstack-blog-nest:${{env.IMAGE_VERSION}} | ||
cd ${{secrets.PROJECT_DIR}} | ||
sed -i 's/^NEST_SERVER_VERSION=.*/NEST_SERVER_VERSION=${{env.IMAGE_VERSION}}/' .env.docker.local | ||
docker compose --env-file .env.docker.local up -d | ||
echo "Done" | ||
END | ||
ssh remote < ~/remote.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
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
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,8 @@ | ||
{ | ||
"apps": [ | ||
{ | ||
"name": "blog-nest-server", | ||
"script": "dist/main.js" | ||
} | ||
] | ||
} |
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
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,71 @@ | ||
import { | ||
WebSocketGateway, | ||
SubscribeMessage, | ||
MessageBody, | ||
WebSocketServer, | ||
OnGatewayConnection, | ||
OnGatewayDisconnect, | ||
ConnectedSocket, | ||
} from "@nestjs/websockets"; | ||
import { Server, Socket } from "socket.io"; | ||
|
||
@WebSocketGateway({ | ||
path: "/socket.io", | ||
namespace: "/chatroom", | ||
allowEIO3: true, | ||
cors: { | ||
origin: (origin, callback) => { | ||
const list = process.env.WEB_SOCKET_WHITE_LIST.split(","); | ||
if (list.includes(origin)) { | ||
callback(null, true); | ||
} else { | ||
callback(new Error("Not allowed by CORS")); | ||
} | ||
}, | ||
// origin: "http://localhost:3000", | ||
credentials: true, | ||
}, | ||
}) | ||
export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { | ||
@WebSocketServer() | ||
server: Server; | ||
|
||
private getId(client: Socket) { | ||
return client.id.replace("/chatroom#", ""); | ||
} | ||
|
||
handleConnection(client: Socket) { | ||
client.emit("singleMsg", { | ||
event: "greet_from_server", | ||
data: { | ||
content: "hello,欢迎您加入在线聊天室!", | ||
}, | ||
}); | ||
client.broadcast.emit("broadcast", { | ||
event: "new_user_join", | ||
data: { | ||
user: this.getId(client), | ||
}, | ||
}); | ||
} | ||
|
||
handleDisconnect(client: Socket) { | ||
client.broadcast.emit("broadcast", { | ||
event: "someone_exit", | ||
data: { | ||
user: this.getId(client), | ||
}, | ||
}); | ||
} | ||
|
||
@SubscribeMessage("chat") | ||
onChat(@MessageBody() data: Record<string, any>, @ConnectedSocket() client: Socket) { | ||
client.broadcast.emit("broadcast", { | ||
event: "new_chat_content", | ||
data: { | ||
user: this.getId(client), | ||
content: data, | ||
}, | ||
}); | ||
} | ||
} |
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,7 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { ChatGateway } from "./chat.gateway"; | ||
|
||
@Module({ | ||
providers: [ChatGateway], | ||
}) | ||
export class ChatModule {} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# 开发环境变量 | ||
VITE_APP_SOCKET_SERVER=http://localhost:8002 | ||
VITE_APP_SOCKET_SERVER=http://localhost:8012 |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# 生产环境变量 | ||
VITE_APP_SOCKET_SERVER=https://blog.wbjiang.cn | ||
VITE_APP_SOCKET_SERVER=https://blog.wbjiang.cn |
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
Oops, something went wrong.