Compare commits
13 Commits
a52b2d13f0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3be4ead1d | ||
|
|
8765608603 | ||
|
|
9d0240bf3f | ||
|
|
b86c76a2fc | ||
|
|
392d4925cf | ||
|
|
ec65a99b99 | ||
| 920c910c23 | |||
| 1db56d6741 | |||
| 58df1359e1 | |||
| 971c62afc5 | |||
| 1d37f50604 | |||
| a43658af84 | |||
| ca62651c3c |
@@ -1,6 +1,6 @@
|
|||||||
# Gitea Workflows
|
# Gitea Workflows
|
||||||
|
|
||||||
This directory contains Gitea Actions workflows for CI/CD.
|
This directory contains Gitea Actions workflows for CI/CD Pipelines.
|
||||||
|
|
||||||
## Workflows
|
## Workflows
|
||||||
|
|
||||||
|
|||||||
@@ -35,11 +35,17 @@ jobs:
|
|||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
with:
|
||||||
|
driver: docker
|
||||||
|
|
||||||
- name: Configure Docker Socket
|
- name: Configure Docker Socket
|
||||||
run: |
|
run: |
|
||||||
# Detect Docker socket location (handles rootless Docker)
|
# Prefer DOCKER_HOST if set (runner injects the real host socket path).
|
||||||
if [ -S "/run/user/$(id -u)/docker.sock" ]; then
|
# This ensures pack passes the correct host path to lifecycle containers,
|
||||||
|
# which Podman can bind-mount without "mkdir permission denied".
|
||||||
|
if [ -n "$DOCKER_HOST" ]; then
|
||||||
|
echo "PACK_DOCKER_HOST=$DOCKER_HOST" >> $GITEA_ENV
|
||||||
|
elif [ -S "/run/user/$(id -u)/docker.sock" ]; then
|
||||||
echo "PACK_DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock" >> $GITEA_ENV
|
echo "PACK_DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock" >> $GITEA_ENV
|
||||||
elif [ -S "/var/run/docker.sock" ]; then
|
elif [ -S "/var/run/docker.sock" ]; then
|
||||||
echo "PACK_DOCKER_HOST=unix:///var/run/docker.sock" >> $GITEA_ENV
|
echo "PACK_DOCKER_HOST=unix:///var/run/docker.sock" >> $GITEA_ENV
|
||||||
@@ -62,7 +68,6 @@ jobs:
|
|||||||
pack config default-builder paketobuildpacks/builder-jammy-tiny:latest
|
pack config default-builder paketobuildpacks/builder-jammy-tiny:latest
|
||||||
|
|
||||||
- name: Prepare build environment
|
- name: Prepare build environment
|
||||||
working-directory: backend
|
|
||||||
run: |
|
run: |
|
||||||
# Create .env.production for build (no secrets, just structure)
|
# Create .env.production for build (no secrets, just structure)
|
||||||
cat > .env.production << EOF
|
cat > .env.production << EOF
|
||||||
@@ -77,15 +82,15 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
PACK_ARGS=(
|
PACK_ARGS=(
|
||||||
"${IMAGE_NAME}:${IMAGE_TAG}"
|
"${IMAGE_NAME}:${IMAGE_TAG}"
|
||||||
--path backend
|
--path .
|
||||||
)
|
)
|
||||||
|
|
||||||
if [ -n "$PACK_DOCKER_HOST" ]; then
|
if [ -n "$PACK_DOCKER_HOST" ]; then
|
||||||
PACK_ARGS+=(--docker-host "$PACK_DOCKER_HOST")
|
PACK_ARGS+=(--docker-host "$PACK_DOCKER_HOST")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "backend/.env.production" ]; then
|
if [ -f ".env.production" ]; then
|
||||||
PACK_ARGS+=(--env-file backend/.env.production)
|
PACK_ARGS+=(--env-file .env.production)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
pack build "${PACK_ARGS[@]}"
|
pack build "${PACK_ARGS[@]}"
|
||||||
@@ -107,16 +112,26 @@ jobs:
|
|||||||
- name: Push to registry
|
- name: Push to registry
|
||||||
if: env.REGISTRY != ''
|
if: env.REGISTRY != ''
|
||||||
run: |
|
run: |
|
||||||
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login ${{ env.REGISTRY }} -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
|
# docker login sends POST /auth to Podman which incorrectly tries HTTPS even for
|
||||||
|
# insecure registries. Pre-populate config.json instead — docker push goes through
|
||||||
|
# the Podman daemon which correctly uses HTTP (insecure=true in registries.conf).
|
||||||
|
mkdir -p ~/.docker
|
||||||
|
AUTH=$(echo -n "${{ secrets.REGISTRY_USERNAME }}:${{ secrets.REGISTRY_PASSWORD }}" | base64 -w 0)
|
||||||
|
# Auth key must be the registry hostname only (e.g. host:port), not the full path
|
||||||
|
REGISTRY_HOST=$(echo "${{ env.REGISTRY }}" | cut -d'/' -f1)
|
||||||
|
echo "{\"auths\":{\"${REGISTRY_HOST}\":{\"auth\":\"${AUTH}\"}}}" > ~/.docker/config.json
|
||||||
docker push "${{ steps.image.outputs.full }}"
|
docker push "${{ steps.image.outputs.full }}"
|
||||||
|
|
||||||
- name: Save image artifact
|
- name: Save image to file
|
||||||
|
if: env.REGISTRY == ''
|
||||||
|
run: |
|
||||||
|
docker save "${IMAGE_NAME}:${IMAGE_TAG}" -o /tmp/image.tar
|
||||||
|
|
||||||
|
- name: Upload image artifact
|
||||||
|
if: env.REGISTRY == ''
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: docker-image
|
name: docker-image
|
||||||
path: /tmp/image.tar
|
path: /tmp/image.tar
|
||||||
retention-days: 1
|
retention-days: 1
|
||||||
if: env.REGISTRY == ''
|
|
||||||
run: |
|
|
||||||
docker save "${IMAGE_NAME}:${IMAGE_TAG}" -o /tmp/image.tar
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
|
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
|
||||||
environment: production
|
environment: production
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -53,7 +53,7 @@ jobs:
|
|||||||
- name: Prepare deployment files
|
- name: Prepare deployment files
|
||||||
run: |
|
run: |
|
||||||
mkdir -p deployment/tmp
|
mkdir -p deployment/tmp
|
||||||
|
|
||||||
# Create .env.production
|
# Create .env.production
|
||||||
cat > deployment/tmp/.env.production << EOF
|
cat > deployment/tmp/.env.production << EOF
|
||||||
PORT=${{ secrets.PORT || '8080' }}
|
PORT=${{ secrets.PORT || '8080' }}
|
||||||
@@ -67,38 +67,38 @@ jobs:
|
|||||||
FIREBASE_STORAGE_BUCKET=${{ secrets.FIREBASE_STORAGE_BUCKET }}
|
FIREBASE_STORAGE_BUCKET=${{ secrets.FIREBASE_STORAGE_BUCKET }}
|
||||||
FIREBASE_CREDENTIALS_FILE=${{ secrets.FIREBASE_CREDENTIALS_FILE_PATH || './firebase-credentials.json' }}
|
FIREBASE_CREDENTIALS_FILE=${{ secrets.FIREBASE_CREDENTIALS_FILE_PATH || './firebase-credentials.json' }}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# Create deployment script
|
# Create deployment script
|
||||||
cat > deployment/tmp/deploy.sh << 'DEPLOY_SCRIPT'
|
cat > deployment/tmp/deploy.sh << 'DEPLOY_SCRIPT'
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
IMAGE_NAME="${{ env.IMAGE_NAME }}"
|
IMAGE_NAME="${{ env.IMAGE_NAME }}"
|
||||||
IMAGE_TAG="${{ env.IMAGE_TAG }}"
|
IMAGE_TAG="${{ env.IMAGE_TAG }}"
|
||||||
CONTAINER_NAME="jd-book-uploader"
|
CONTAINER_NAME="jd-book-uploader"
|
||||||
|
|
||||||
set -a
|
set -a
|
||||||
source .env.production
|
source .env.production
|
||||||
set +a
|
set +a
|
||||||
|
|
||||||
# Stop existing container
|
# Stop existing container
|
||||||
if podman ps -a --format "{{.Names}}" | grep -q "^${CONTAINER_NAME}$"; then
|
if podman ps -a --format "{{.Names}}" | grep -q "^${CONTAINER_NAME}$"; then
|
||||||
podman stop "${CONTAINER_NAME}" 2>/dev/null || true
|
podman stop "${CONTAINER_NAME}" 2>/dev/null || true
|
||||||
podman rm "${CONTAINER_NAME}" 2>/dev/null || true
|
podman rm "${CONTAINER_NAME}" 2>/dev/null || true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Load image if artifact provided
|
# Load image if artifact provided
|
||||||
if [ -f image.tar ]; then
|
if [ -f image.tar ]; then
|
||||||
podman load -i image.tar
|
podman load -i image.tar
|
||||||
rm -f image.tar
|
rm -f image.tar
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Pull from registry if configured
|
# Pull from registry if configured
|
||||||
if [ -n "${{ env.REGISTRY }}" ]; then
|
if [ -n "${{ env.REGISTRY }}" ]; then
|
||||||
podman pull "${{ env.REGISTRY }}/${IMAGE_NAME}:${IMAGE_TAG}"
|
podman pull --tls-verify=false "${{ env.REGISTRY }}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||||
podman tag "${{ env.REGISTRY }}/${IMAGE_NAME}:${IMAGE_TAG}" "${IMAGE_NAME}:${IMAGE_TAG}"
|
podman tag "${{ env.REGISTRY }}/${IMAGE_NAME}:${IMAGE_TAG}" "${IMAGE_NAME}:${IMAGE_TAG}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Build run command
|
# Build run command
|
||||||
PODMAN_CMD=(
|
PODMAN_CMD=(
|
||||||
podman run -d
|
podman run -d
|
||||||
@@ -107,7 +107,7 @@ jobs:
|
|||||||
--user root
|
--user root
|
||||||
--restart=unless-stopped
|
--restart=unless-stopped
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add environment variables
|
# Add environment variables
|
||||||
while IFS='=' read -r key value; do
|
while IFS='=' read -r key value; do
|
||||||
[[ "$key" =~ ^#.*$ ]] && continue
|
[[ "$key" =~ ^#.*$ ]] && continue
|
||||||
@@ -117,19 +117,19 @@ jobs:
|
|||||||
PODMAN_CMD+=(-e "${key}=${value}")
|
PODMAN_CMD+=(-e "${key}=${value}")
|
||||||
fi
|
fi
|
||||||
done < .env.production
|
done < .env.production
|
||||||
|
|
||||||
# Mount Firebase credentials
|
# Mount Firebase credentials
|
||||||
FIREBASE_CREDS="${FIREBASE_CREDENTIALS_FILE}"
|
FIREBASE_CREDS="${FIREBASE_CREDENTIALS_FILE}"
|
||||||
if [ -f "$FIREBASE_CREDS" ]; then
|
if [ -f "$FIREBASE_CREDS" ]; then
|
||||||
PODMAN_CMD+=(-v "${FIREBASE_CREDS}:/app/firebase-credentials.json:ro,z")
|
PODMAN_CMD+=(-v "${FIREBASE_CREDS}:/app/firebase-credentials.json:ro,z")
|
||||||
PODMAN_CMD+=(-e "FIREBASE_CREDENTIALS_FILE=/app/firebase-credentials.json")
|
PODMAN_CMD+=(-e "FIREBASE_CREDENTIALS_FILE=/app/firebase-credentials.json")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
PODMAN_CMD+=("${IMAGE_NAME}:${IMAGE_TAG}")
|
PODMAN_CMD+=("${IMAGE_NAME}:${IMAGE_TAG}")
|
||||||
|
|
||||||
"${PODMAN_CMD[@]}"
|
"${PODMAN_CMD[@]}"
|
||||||
sleep 3
|
sleep 3
|
||||||
|
|
||||||
if podman ps --format "{{.Names}}" | grep -q "^${CONTAINER_NAME}$"; then
|
if podman ps --format "{{.Names}}" | grep -q "^${CONTAINER_NAME}$"; then
|
||||||
echo "✓ Container started"
|
echo "✓ Container started"
|
||||||
podman logs "${CONTAINER_NAME}" --tail 20
|
podman logs "${CONTAINER_NAME}" --tail 20
|
||||||
@@ -139,32 +139,40 @@ jobs:
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
DEPLOY_SCRIPT
|
DEPLOY_SCRIPT
|
||||||
|
|
||||||
chmod +x deployment/tmp/deploy.sh
|
chmod +x deployment/tmp/deploy.sh
|
||||||
|
|
||||||
- name: Transfer files
|
- name: Transfer files
|
||||||
run: |
|
run: |
|
||||||
scp -r deployment/tmp/* ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${{ secrets.DEPLOY_PATH }}/deployment/
|
# Ensure remote deployment directory exists
|
||||||
|
ssh ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} "mkdir -p ${{ secrets.DEPLOY_PATH }}/deployment"
|
||||||
|
# Copy files explicitly — glob (*) skips dotfiles like .env.production
|
||||||
|
scp deployment/tmp/deploy.sh ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${{ secrets.DEPLOY_PATH }}/deployment/
|
||||||
|
scp deployment/tmp/.env.production ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${{ secrets.DEPLOY_PATH }}/deployment/
|
||||||
if [ -f image.tar ]; then
|
if [ -f image.tar ]; then
|
||||||
scp image.tar ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${{ secrets.DEPLOY_PATH }}/image.tar
|
scp image.tar ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${{ secrets.DEPLOY_PATH }}/image.tar
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Deploy
|
- name: Deploy
|
||||||
run: |
|
run: |
|
||||||
ssh ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} << ENDSSH
|
ssh -T ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} << ENDSSH
|
||||||
set -e
|
set -e
|
||||||
cd ${{ secrets.DEPLOY_PATH }}
|
cd ${{ secrets.DEPLOY_PATH }}
|
||||||
|
|
||||||
if [ -f image.tar ]; then
|
if [ -f image.tar ]; then
|
||||||
podman load -i image.tar
|
podman load -i image.tar
|
||||||
rm -f image.tar
|
rm -f image.tar
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f "${{ secrets.FIREBASE_CREDENTIALS_FILE_PATH || './firebase-credentials.json' }}" ]; then
|
if [ ! -f "${{ secrets.FIREBASE_CREDENTIALS_FILE_PATH || './firebase-credentials.json' }}" ]; then
|
||||||
echo "Error: Firebase credentials not found"
|
echo "Error: Firebase credentials not found"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ -n "${{ env.REGISTRY }}" ]; then
|
||||||
|
echo "${{ secrets.REGISTRY_PASSWORD }}" | podman login "${{ env.REGISTRY }}" -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin --tls-verify=false
|
||||||
|
fi
|
||||||
|
|
||||||
cd deployment
|
cd deployment
|
||||||
./deploy.sh
|
./deploy.sh
|
||||||
ENDSSH
|
ENDSSH
|
||||||
@@ -173,16 +181,15 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
sleep 5
|
sleep 5
|
||||||
HEALTH_URL="http://${{ secrets.DEPLOY_HOST }}:${{ secrets.PORT || '8080' }}/api/health"
|
HEALTH_URL="http://${{ secrets.DEPLOY_HOST }}:${{ secrets.PORT || '8080' }}/api/health"
|
||||||
|
|
||||||
for i in {1..10}; do
|
for i in {1..10}; do
|
||||||
if curl -f -s "$HEALTH_URL" > /dev/null; then
|
if curl -f -s "$HEALTH_URL" > /dev/null; then
|
||||||
echo "✓ Health check passed"
|
echo "✓ Health check passed"
|
||||||
curl -s "$HEALTH_URL" | jq .
|
curl -s "$HEALTH_URL"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
sleep 3
|
sleep 3
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "✗ Health check failed"
|
echo "✗ Health check failed"
|
||||||
exit 1
|
exit 1
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,20 @@ on:
|
|||||||
- production
|
- production
|
||||||
- develop
|
- develop
|
||||||
paths:
|
paths:
|
||||||
- 'backend/**'
|
- '**/*.go'
|
||||||
|
- 'go.mod'
|
||||||
|
- 'go.sum'
|
||||||
|
- '.gitea/workflows/test.yml'
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
- production
|
- production
|
||||||
- develop
|
- develop
|
||||||
paths:
|
paths:
|
||||||
- 'backend/**'
|
- '**/*.go'
|
||||||
|
- 'go.mod'
|
||||||
|
- 'go.sum'
|
||||||
|
- '.gitea/workflows/test.yml'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
@@ -34,29 +40,28 @@ jobs:
|
|||||||
uses: actions/cache@v4
|
uses: actions/cache@v4
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('backend/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('go.sum') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-go-
|
${{ runner.os }}-go-
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
working-directory: backend
|
|
||||||
run: go mod download
|
run: go mod download
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
working-directory: backend
|
|
||||||
run: go test -v -race -coverprofile=coverage.out ./...
|
run: go test -v -race -coverprofile=coverage.out ./...
|
||||||
|
|
||||||
- name: Upload coverage
|
# - name: Upload coverage
|
||||||
uses: codecov/codecov-action@v4
|
# uses: codecov/codecov-action@v4
|
||||||
if: always()
|
# if: always()
|
||||||
with:
|
# with:
|
||||||
file: ./backend/coverage.out
|
# file: ./coverage.out
|
||||||
flags: backend
|
# flags: backend
|
||||||
name: backend-coverage
|
# name: backend-coverage
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
name: Lint Code
|
name: Lint Code
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
continue-on-error: true # Allow failure until golangci-lint supports Go 1.25
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
@@ -67,8 +72,13 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
go-version: '1.25'
|
go-version: '1.25'
|
||||||
|
|
||||||
|
- name: Install golangci-lint from source
|
||||||
|
run: |
|
||||||
|
# Install golangci-lint from source using Go 1.25 to ensure compatibility
|
||||||
|
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||||
|
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
|
||||||
|
|
||||||
- name: Run golangci-lint
|
- name: Run golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v4
|
run: |
|
||||||
with:
|
golangci-lint --version
|
||||||
version: latest
|
golangci-lint run --timeout=5m
|
||||||
working-directory: backend
|
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -49,5 +49,7 @@ build/
|
|||||||
.env.production
|
.env.production
|
||||||
.env.local
|
.env.local
|
||||||
.env.production.example
|
.env.production.example
|
||||||
|
|
||||||
|
# Gitea workflows
|
||||||
.gitea/workflows/deploy.yml
|
.gitea/workflows/deploy.yml
|
||||||
.gitea/workflows/build.yml
|
.gitea/workflows/build.yml
|
||||||
|
|||||||
@@ -22,18 +22,37 @@ func TestUploadBook(t *testing.T) {
|
|||||||
writer := multipart.NewWriter(body)
|
writer := multipart.NewWriter(body)
|
||||||
|
|
||||||
// Add form fields
|
// Add form fields
|
||||||
writer.WriteField("book_name", "Test Book")
|
if err := writer.WriteField("book_name", "Test Book"); err != nil {
|
||||||
writer.WriteField("cost", "10.50")
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
writer.WriteField("price", "15.99")
|
}
|
||||||
writer.WriteField("quantity", "100")
|
if err := writer.WriteField("cost", "10.50"); err != nil {
|
||||||
writer.WriteField("publisher_author", "Test Publisher")
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
writer.WriteField("category", "Fiction")
|
}
|
||||||
|
if err := writer.WriteField("price", "15.99"); err != nil {
|
||||||
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.WriteField("quantity", "100"); err != nil {
|
||||||
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.WriteField("publisher_author", "Test Publisher"); err != nil {
|
||||||
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.WriteField("category", "Fiction"); err != nil {
|
||||||
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Add image file
|
// Add image file
|
||||||
part, _ := writer.CreateFormFile("image", "test.png")
|
part, err := writer.CreateFormFile("image", "test.png")
|
||||||
part.Write([]byte("fake image data"))
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create form file: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := part.Write([]byte("fake image data")); err != nil {
|
||||||
|
t.Fatalf("Failed to write image data: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
writer.Close()
|
if err := writer.Close(); err != nil {
|
||||||
|
t.Fatalf("Failed to close writer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
req := httptest.NewRequest("POST", "/api/books", body)
|
req := httptest.NewRequest("POST", "/api/books", body)
|
||||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
@@ -55,8 +74,12 @@ func TestUploadBook_ValidationErrors(t *testing.T) {
|
|||||||
// Test missing required field
|
// Test missing required field
|
||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
writer := multipart.NewWriter(body)
|
writer := multipart.NewWriter(body)
|
||||||
writer.WriteField("book_name", "") // Empty book name
|
if err := writer.WriteField("book_name", ""); err != nil { // Empty book name
|
||||||
writer.Close()
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.Close(); err != nil {
|
||||||
|
t.Fatalf("Failed to close writer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
req := httptest.NewRequest("POST", "/api/books", body)
|
req := httptest.NewRequest("POST", "/api/books", body)
|
||||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
|
|||||||
@@ -22,18 +22,37 @@ func TestUploadStationery(t *testing.T) {
|
|||||||
writer := multipart.NewWriter(body)
|
writer := multipart.NewWriter(body)
|
||||||
|
|
||||||
// Add form fields
|
// Add form fields
|
||||||
writer.WriteField("stationery_name", "Test Pen")
|
if err := writer.WriteField("stationery_name", "Test Pen"); err != nil {
|
||||||
writer.WriteField("cost", "2.50")
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
writer.WriteField("price", "5.99")
|
}
|
||||||
writer.WriteField("quantity", "200")
|
if err := writer.WriteField("cost", "2.50"); err != nil {
|
||||||
writer.WriteField("category", "Writing")
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
writer.WriteField("color", "Blue")
|
}
|
||||||
|
if err := writer.WriteField("price", "5.99"); err != nil {
|
||||||
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.WriteField("quantity", "200"); err != nil {
|
||||||
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.WriteField("category", "Writing"); err != nil {
|
||||||
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.WriteField("color", "Blue"); err != nil {
|
||||||
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Add image file
|
// Add image file
|
||||||
part, _ := writer.CreateFormFile("image", "test.png")
|
part, err := writer.CreateFormFile("image", "test.png")
|
||||||
part.Write([]byte("fake image data"))
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create form file: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := part.Write([]byte("fake image data")); err != nil {
|
||||||
|
t.Fatalf("Failed to write image data: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
writer.Close()
|
if err := writer.Close(); err != nil {
|
||||||
|
t.Fatalf("Failed to close writer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
req := httptest.NewRequest("POST", "/api/stationery", body)
|
req := httptest.NewRequest("POST", "/api/stationery", body)
|
||||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
@@ -55,8 +74,12 @@ func TestUploadStationery_ValidationErrors(t *testing.T) {
|
|||||||
// Test missing required field
|
// Test missing required field
|
||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
writer := multipart.NewWriter(body)
|
writer := multipart.NewWriter(body)
|
||||||
writer.WriteField("stationery_name", "") // Empty stationery name
|
if err := writer.WriteField("stationery_name", ""); err != nil { // Empty stationery name
|
||||||
writer.Close()
|
t.Fatalf("Failed to write field: %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.Close(); err != nil {
|
||||||
|
t.Fatalf("Failed to close writer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
req := httptest.NewRequest("POST", "/api/stationery", body)
|
req := httptest.NewRequest("POST", "/api/stationery", body)
|
||||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -26,7 +26,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to connect to database: %v", err)
|
log.Fatalf("Failed to connect to database: %v", err)
|
||||||
}
|
}
|
||||||
defer services.CloseDB()
|
// Note: CloseDB is called explicitly in graceful shutdown, not in defer
|
||||||
log.Println("Database connected successfully")
|
log.Println("Database connected successfully")
|
||||||
|
|
||||||
// Initialize Firebase
|
// Initialize Firebase
|
||||||
|
|||||||
@@ -43,23 +43,25 @@ func ErrorHandler(c *fiber.Ctx) error {
|
|||||||
// RecoverHandler recovers from panics and returns a proper error response
|
// RecoverHandler recovers from panics and returns a proper error response
|
||||||
func RecoverHandler() fiber.Handler {
|
func RecoverHandler() fiber.Handler {
|
||||||
return func(c *fiber.Ctx) error {
|
return func(c *fiber.Ctx) error {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
// Log panic with context
|
// Log panic with context
|
||||||
log.Printf("Panic recovered: %v | Method: %s | Path: %s | IP: %s",
|
log.Printf("Panic recovered: %v | Method: %s | Path: %s | IP: %s",
|
||||||
r,
|
r,
|
||||||
c.Method(),
|
c.Method(),
|
||||||
c.Path(),
|
c.Path(),
|
||||||
c.IP(),
|
c.IP(),
|
||||||
)
|
)
|
||||||
|
|
||||||
// Return error response
|
// Return error response
|
||||||
c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
if err := c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"error": "Internal Server Error",
|
"error": "Internal Server Error",
|
||||||
})
|
}); err != nil {
|
||||||
}
|
log.Printf("Failed to send error response: %v", err)
|
||||||
}()
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
return c.Next()
|
return c.Next()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user