Skip to content

Commit 2b7e0e5

Browse files
authored
feat: user login/logout (#5)
1 parent a340df8 commit 2b7e0e5

File tree

30 files changed

+1045
-147
lines changed

30 files changed

+1045
-147
lines changed

.github/ci/docker-compose.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ services:
1717
context: ../../server
1818
dockerfile: Dockerfile
1919
container_name: ceramicraft-user-mservice
20+
environment:
21+
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
22+
- SMTP_PASSWORD=${SMTP_PASSWORD}
23+
- SMTP_EMAIL_FROM=${SMTP_EMAIL_FROM}
24+
- JWT_SECRET=${JWT_SECRET}
2025
depends_on:
2126
- mysql
2227
networks:
2328
- ci-net
2429
ports:
2530
- "8080:8080" # 只给宿主机(ZAP)用
26-
environment:
27-
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
2831
command: ["sh", "-c", "apt-get update && apt-get install -y netcat-openbsd && chmod +x /app/wait-for.sh && ./wait-for.sh mysql-container 3306 ./main"]
2932
volumes:
3033
- ./wait-for.sh:/app/wait-for.sh # CI 专用配置

.github/workflows/deploy.yml

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ on:
1919
- rollback
2020

2121
jobs:
22+
generate-env:
23+
name: Generate .env file
24+
runs-on: ubuntu-latest
25+
outputs:
26+
env-path: ${{ steps.set-output.outputs.env-path }}
27+
steps:
28+
- name: Generate .env file
29+
run: |
30+
echo "MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }}" >> env-file
31+
echo "SMTP_PASSWORD=${{ secrets.SMTP_PASSWORD }}" >> env-file
32+
echo "SMTP_EMAIL_FROM=${{ secrets.SMTP_EMAIL_FROM }}" >> env-file
33+
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> env-file
34+
- name: Upload env-file as artifact
35+
uses: actions/upload-artifact@v4
36+
with:
37+
name: env-file
38+
path: env-file
2239
build:
2340
if: ${{ github.event.inputs.command == 'deploy' }} # ← 仅在 deploy 时构建
2441
runs-on: ubuntu-latest
@@ -40,19 +57,26 @@ jobs:
4057
docker push "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
4158
deploy:
4259
if: ${{ github.event.inputs.command == 'deploy' }}
43-
needs: build
60+
needs:
61+
- build
62+
- generate-env
4463
runs-on: self-hosted
4564
env:
4665
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
4766
steps:
67+
- name: Download .env file
68+
uses: actions/download-artifact@v4
69+
with:
70+
name: env-file
71+
path: .
4872
- name: pull the docker image
4973
run: |
5074
docker pull "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
5175
- name: run docker container
5276
run: |
5377
docker stop ceramicraft-user-mservice || true
5478
docker rm ceramicraft-user-mservice || true
55-
docker run -d --name ceramicraft-user-mservice --network ceramicraft-network -e MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }} -p 8080:8080 "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
79+
docker run -d --name ceramicraft-user-mservice --network ceramicraft-network --env-file env-file "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
5680
restart:
5781
if: ${{ github.event.inputs.command == 'restart' }}
5882
runs-on: self-hosted
@@ -63,14 +87,20 @@ jobs:
6387
rollback:
6488
if: ${{ github.event.inputs.command == 'rollback' }}
6589
runs-on: self-hosted
90+
needs: generate-env
6691
env:
6792
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
6893
steps:
94+
- name: Download .env file
95+
uses: actions/download-artifact@v4
96+
with:
97+
name: env-file
98+
path: .
6999
- name: pull the docker image
70100
run: |
71101
docker pull "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
72102
- name: run docker container
73103
run: |
74104
docker stop ceramicraft-user-mservice || true
75105
docker rm ceramicraft-user-mservice || true
76-
docker run -d --name ceramicraft-user-mservice --network ceramicraft-network -p 8080:8080 "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
106+
docker run -d --name ceramicraft-user-mservice --network ceramicraft-network --env-file env-file "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"

.github/workflows/zap.yml

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,55 @@ on:
99
- main
1010

1111
jobs:
12-
zap:
13-
name: ZAP Scan
14-
runs-on: ubuntu-latest
15-
steps:
16-
- name: Checkout code
17-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
18-
- name: Set up Docker Buildx
19-
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435
20-
21-
- name: Build and run Docker Compose
22-
run: |
23-
MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }} docker compose -f .github/ci/docker-compose.yml up --build -d
24-
timeout 180 bash -c 'until [ "$(docker compose -f .github/ci/docker-compose.yml ps ceramicraft-user-mservice --format json | jq -r .Health)" = "healthy" ]; do sleep 2; done'
25-
- name: Check Docker Compose Logs
26-
if: failure()
27-
run: |
28-
docker compose -f .github/ci/docker-compose.yml logs
29-
- name: Run ZAP Scan
30-
uses: zaproxy/action-full-scan@75ee1686750ab1511a73b26b77a2aedd295053ed
31-
with:
32-
target: 'http://localhost:8080' # ZAP accesses the host-mapped port
33-
cmd_options: '-s'
34-
fail_action: false # Do not fail CI even if vulnerabilities are found
35-
allow_issue_writing: false # Disable automatic creation of GitHub Issues
36-
issue_title: 'ZAP Security Scan Alert'
37-
artifact_name: 'zap-report' # Attachment report
38-
- name: Tear down
39-
if: always()
40-
run: docker compose -f .github/ci/docker-compose.yml down --volumes --remove-orphans
12+
generate-env:
13+
name: Generate .env file
14+
runs-on: ubuntu-latest
15+
outputs:
16+
env-path: ${{ steps.set-output.outputs.env-path }}
17+
steps:
18+
- name: Generate .env file
19+
run: |
20+
echo "MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }}" >> env-file
21+
echo "SMTP_PASSWORD=${{ secrets.SMTP_PASSWORD }}" >> env-file
22+
echo "SMTP_EMAIL_FROM=${{ secrets.SMTP_EMAIL_FROM }}" >> env-file
23+
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> env-file
24+
- name: Upload env-file as artifact
25+
uses: actions/upload-artifact@v4
26+
with:
27+
name: env-file
28+
path: env-file
29+
zap:
30+
name: ZAP Scan
31+
runs-on: ubuntu-latest
32+
needs:
33+
- generate-env
34+
steps:
35+
- name: Checkout code
36+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
37+
- name: Set up Docker Buildx
38+
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435
39+
- name: Download .env file
40+
uses: actions/download-artifact@v4
41+
with:
42+
name: env-file
43+
path: .
44+
- name: Build and run Docker Compose
45+
run: |
46+
docker compose -f .github/ci/docker-compose.yml --env-file env-file up --build -d
47+
timeout 180 bash -c 'until [ "$(docker compose -f .github/ci/docker-compose.yml ps ceramicraft-user-mservice --format json | jq -r .Health)" = "healthy" ]; do sleep 2; done'
48+
- name: Check Docker Compose Logs
49+
if: failure()
50+
run: |
51+
docker compose -f .github/ci/docker-compose.yml logs
52+
- name: Run ZAP Scan
53+
uses: zaproxy/action-full-scan@75ee1686750ab1511a73b26b77a2aedd295053ed
54+
with:
55+
target: 'http://localhost:8080' # ZAP accesses the host-mapped port
56+
cmd_options: '-s'
57+
fail_action: false # Do not fail CI even if vulnerabilities are found
58+
allow_issue_writing: false # Disable automatic creation of GitHub Issues
59+
issue_title: 'ZAP Security Scan Alert'
60+
artifact_name: 'zap-report' # Attachment report
61+
- name: Tear down
62+
if: always()
63+
run: docker compose -f .github/ci/docker-compose.yml down --volumes --remove-orphans

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ go.work.sum
3232
.vscode/
3333

3434
server/logs
35+
.env

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1-
# ceramicraft-user-mservice
1+
# ceramicraft-user-mservice
2+
## Functional Features
3+
1. support customer/platform admin login/logout, same API with cookie setted under different domain
4+
2. authenticate with jwt token
5+
3. support new customer registration, verified by email opt code
6+
4. provide auth filter for other microservice use
7+
8+
## Detail Disign
9+
[see the document](https://cerami-craft.atlassian.net/wiki/spaces/swe5006gro/pages/4554754/UserService)
10+
11+
## startup dependency
12+
1. Mysql
13+
2. email sending config
14+
3. docker startup
15+
```
16+
cd server
17+
docker build -t ${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:<version> .
18+
docker run -d --name ceramicraft-user-mservice -e MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }} -e SMTP_PASSWORD=${{ secrets.SMTP_PASSWORD }} -e SMTP_EMAIL_FROM=${{ secrets.SMTP_EMAIL_FROM }} "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
19+
```
20+
4. non-containerized startup
21+
```
22+
MYSQL_PASSWORD=your_password SMTP_PASSWORD=your_smtp_password SMTP_EMAIL_FROM=your_email@example.com go run server/main.go
23+
```

common/bo/user.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package bo
2+
3+
type UserBO struct {
4+
ID int `json:"id"`
5+
Email string `json:"email"`
6+
Password string `json:"password"`
7+
}

common/go.mod

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,42 @@ module github.com/NUS-ISS-Agile-Team/ceramicraft-user-mservice/common
33
go 1.24.0
44

55
require (
6+
github.com/gin-gonic/gin v1.11.0
7+
github.com/golang-jwt/jwt/v5 v5.2.2
68
google.golang.org/grpc v1.75.1
79
google.golang.org/protobuf v1.36.9
810
)
911

1012
require (
11-
golang.org/x/net v0.41.0 // indirect
12-
golang.org/x/sys v0.33.0 // indirect
13-
golang.org/x/text v0.26.0 // indirect
13+
github.com/bytedance/sonic v1.14.0 // indirect
14+
github.com/bytedance/sonic/loader v0.3.0 // indirect
15+
github.com/cloudwego/base64x v0.1.6 // indirect
16+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
17+
github.com/gin-contrib/sse v1.1.0 // indirect
18+
github.com/go-playground/locales v0.14.1 // indirect
19+
github.com/go-playground/universal-translator v0.18.1 // indirect
20+
github.com/go-playground/validator/v10 v10.27.0 // indirect
21+
github.com/goccy/go-json v0.10.2 // indirect
22+
github.com/goccy/go-yaml v1.18.0 // indirect
23+
github.com/json-iterator/go v1.1.12 // indirect
24+
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
25+
github.com/leodido/go-urn v1.4.0 // indirect
26+
github.com/mattn/go-isatty v0.0.20 // indirect
27+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
28+
github.com/modern-go/reflect2 v1.0.2 // indirect
29+
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
30+
github.com/quic-go/qpack v0.5.1 // indirect
31+
github.com/quic-go/quic-go v0.54.0 // indirect
32+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
33+
github.com/ugorji/go/codec v1.3.0 // indirect
34+
go.uber.org/mock v0.5.0 // indirect
35+
golang.org/x/arch v0.20.0 // indirect
36+
golang.org/x/crypto v0.40.0 // indirect
37+
golang.org/x/mod v0.25.0 // indirect
38+
golang.org/x/net v0.42.0 // indirect
39+
golang.org/x/sync v0.16.0 // indirect
40+
golang.org/x/sys v0.35.0 // indirect
41+
golang.org/x/text v0.27.0 // indirect
42+
golang.org/x/tools v0.34.0 // indirect
1443
google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 // indirect
1544
)

0 commit comments

Comments
 (0)