feat: add ci & fix some bug
All checks were successful
dev middleware install / deploy (push) Successful in 20s

This commit is contained in:
2026-05-14 22:45:35 +08:00
parent c58ad923c5
commit 37b16ebe5f
7 changed files with 131 additions and 0 deletions

1
.dockerignore Normal file
View File

@@ -0,0 +1 @@
src/main/resources/application.yml

View File

@@ -0,0 +1,35 @@
name: Docker Image CI
on:
push:
branches:
- main
jobs:
build:
runs-on: gitea-runner-group-myplayer
container:
image: ${{ vars.HARBOR_URL }}/candlelight/action_builder:v0.0.2
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: docker login
env:
HARBOR_USERNAME: ${{ secrets.HARBOR_ROBOT }}
HARBOR_PASSWORD: ${{ secrets.HARBOR_ROBOT_SECRET }}
HARBOR_URL: ${{ vars.HARBOR_URL }}
run: docker login ${HARBOR_URL} -u ${HARBOR_USERNAME} -p ${HARBOR_PASSWORD}
- name: Build and push Docker images
env:
HARBOR_URL: ${{ vars.HARBOR_URL }}
TAG: ${{ github.sha }}
REPOSITORY: ${{ github.repository }}
run: |
ROOT_DIR=$(pwd)
IMAGE_NAME="${HARBOR_URL}/testing/$REPOSITORY:${TAG}"
echo "Building image: ${IMAGE_NAME}"
docker build -t ${IMAGE_NAME} .
echo "Pushing image: ${IMAGE_NAME}"
docker push ${IMAGE_NAME}
echo "Successfully pushed: ${IMAGE_NAME}"
docker rmi ${IMAGE_NAME}
echo "cleaned up local image"

34
.gitea/workflows/tag.yaml Normal file
View File

@@ -0,0 +1,34 @@
name: Docker Image CI
on:
push:
tags:
- '*'
jobs:
build:
runs-on: gitea-runner-group-myplayer
container:
image: ${{ vars.HARBOR_URL }}/candlelight/action_builder:v0.0.2
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: docker login
env:
HARBOR_USERNAME: ${{ secrets.HARBOR_ROBOT }}
HARBOR_PASSWORD: ${{ secrets.HARBOR_ROBOT_SECRET }}
HARBOR_URL: ${{ vars.HARBOR_URL }}
run: docker login ${HARBOR_URL} -u ${HARBOR_USERNAME} -p ${HARBOR_PASSWORD}
- name: Build and push Docker images
env:
HARBOR_URL: ${{ vars.HARBOR_URL }}
REPOSITORY: ${{ github.repository }}
run: |
ROOT_DIR=$(pwd)
IMAGE_NAME="${HARBOR_URL}/$REPOSITORY:$GITHUB_REF_NAME"
echo "Building image: ${IMAGE_NAME}"
docker build -t ${IMAGE_NAME} .
echo "Pushing image: ${IMAGE_NAME}"
docker push ${IMAGE_NAME}
echo "Successfully pushed: ${IMAGE_NAME}"
docker rmi ${IMAGE_NAME}
echo "cleaned up local image"

1
.gitignore vendored
View File

@@ -19,6 +19,7 @@ src/main/resources/application.yaml
*.iws *.iws
*.iml *.iml
*.ipr *.ipr
src/main/resources/application.yml
### NetBeans ### ### NetBeans ###
/nbproject/private/ /nbproject/private/

34
Dockerfile Normal file
View File

@@ -0,0 +1,34 @@
# ===== build stage =====
FROM registry.merlin.xin/library/maven:4.0.0-rc-5-eclipse-temurin-17 AS builder
WORKDIR /app
COPY pom.xml .
RUN --mount=type=cache,target=/root/.m2 mvn -B -q dependency:go-offline
COPY . .
RUN --mount=type=cache,target=/root/.m2 mvn -B -q package -DskipTests
# ===== runtime stage =====
FROM registry.merlin.xin/library/eclipse-temurin:17-jre-alpine
# >>> Install debug tools <<<
#RUN apk update && apk add --no-cache \
# curl \
# bind-tools \
# busybox-extras \
# iproute2 \
# tcpdump \
# net-tools
WORKDIR /app
COPY --from=builder /app/target/*.jar app.jar
RUN addgroup -S spring && adduser -S spring -G spring
RUN mkdir -p /app/uploads/photo \
&& chown -R spring:spring /app/uploads
USER spring
ENTRYPOINT ["java","-jar","/app/app.jar","--spring.config.location=file:/app/application.yml"]

View File

@@ -39,6 +39,7 @@ public class OnlineWebSocketHandler extends TextWebSocketHandler {
onlineStatusService.online(userId); onlineStatusService.online(userId);
sessionManager.addSession(userId, session); sessionManager.addSession(userId, session);
sessionManager.getMessages(userId);
log.info("用户 {} 已连接", userId); log.info("用户 {} 已连接", userId);
} }

View File

@@ -1,18 +1,27 @@
package xin.merlin.myplayerbackend.utils.websocket; package xin.merlin.myplayerbackend.utils.websocket;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.WebSocketSession;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@Component @Component
@AllArgsConstructor
public class OnlineWebSocketSessionManager { public class OnlineWebSocketSessionManager {
private static final String MESSAGE_WAITING_LIST = "message:";
private final StringRedisTemplate redis;
private static final Map<Integer, WebSocketSession> websocketSessions = new ConcurrentHashMap<>(); private static final Map<Integer, WebSocketSession> websocketSessions = new ConcurrentHashMap<>();
public void addSession(Integer id, WebSocketSession session) {websocketSessions.put(id, session);} public void addSession(Integer id, WebSocketSession session) {websocketSessions.put(id, session);}
@@ -23,10 +32,26 @@ public class OnlineWebSocketSessionManager {
public Map<Integer, WebSocketSession> getSessions() {return websocketSessions;} public Map<Integer, WebSocketSession> getSessions() {return websocketSessions;}
public void getMessages(Integer userId) throws IOException {
WebSocketSession session = websocketSessions.get(userId);
String message = redis.opsForList().leftPop(MESSAGE_WAITING_LIST + userId);
do {
if (message != null) {
session.sendMessage(new TextMessage(message));
message = redis.opsForList().leftPop(MESSAGE_WAITING_LIST + userId);
}else {
break;
}
}
while (message!=null);
}
public void sendToUser(Integer userId, String message) throws IOException { public void sendToUser(Integer userId, String message) throws IOException {
WebSocketSession session = websocketSessions.get(userId); WebSocketSession session = websocketSessions.get(userId);
if (session != null && session.isOpen()) { if (session != null && session.isOpen()) {
session.sendMessage(new TextMessage(message)); session.sendMessage(new TextMessage(message));
}else {
redis.opsForList().leftPush(MESSAGE_WAITING_LIST + userId, message);
} }
} }