forked from 18F/couch-rules-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
Β·117 lines (102 loc) Β· 4.57 KB
/
setup.sh
File metadata and controls
executable file
Β·117 lines (102 loc) Β· 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
# CouchDB Rules Engine Setup Script
#
# This script sets up a complete development environment including:
# - CouchDB Docker container with admin user
# - Rules database with validation rules
# - CORS configuration for web interface
# - npm dependencies installation
#
# Usage: ./setup.sh
# Environment variables (optional):
# COUCHDB_USER (default: admin)
# COUCHDB_PASSWORD (default: password)
# COUCHDB_PORT (default: 5984)
# DB_NAME (default: rules_db)
# Exit immediately if a command exits with a non-zero status
set -e
# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
echo "Error: Docker does not appear to be running. Please start Docker and try again."
exit 1
fi
# Check if npm is available
if ! command -v npm &> /dev/null; then
echo "Error: npm is not installed. Please install Node.js and npm first."
echo "Visit: https://nodejs.org/"
exit 1
fi
COUCHDB_USER=${COUCHDB_USER:-admin}
COUCHDB_PASSWORD=${COUCHDB_PASSWORD:-password}
COUCHDB_PORT=${COUCHDB_PORT:-5984}
DB_NAME=${DB_NAME:-rules_db}
echo "Pulling CouchDB Docker image..."
docker pull couchdb
echo "Running CouchDB container..."
if docker ps -a --format '{{.Names}}' | grep -q "^couchdb-rules-engine$"; then
echo "β οΈ A Docker container named 'couchdb-rules-engine' already exists."
echo ""
read -p "Do you want to remove it and start fresh? (y/N): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Removing existing container..."
docker rm -f couchdb-rules-engine
else
echo "Setup cancelled. To start the existing container, run:"
echo " docker start couchdb-rules-engine"
exit 0
fi
fi
docker run -d --name couchdb-rules-engine -p ${COUCHDB_PORT}:5984 \
-e COUCHDB_USER=${COUCHDB_USER} \
-e COUCHDB_PASSWORD=${COUCHDB_PASSWORD} \
couchdb
echo "Waiting for CouchDB to start..."
until curl -s http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@localhost:${COUCHDB_PORT}/_up | grep -q '"status":"ok"'; do
sleep 2
done
echo "Creating test database '${DB_NAME}'..."
curl -s -X PUT http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@localhost:${COUCHDB_PORT}/${DB_NAME}
echo "Configuring CORS for web interface..."
# Enable CORS in HTTP daemon (critical step!)
curl -s -X PUT "http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@localhost:${COUCHDB_PORT}/_node/_local/_config/chttpd/enable_cors" -d '"true"' > /dev/null
# Configure CORS settings
curl -s -X PUT "http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@localhost:${COUCHDB_PORT}/_node/_local/_config/cors/enable" -d '"true"' > /dev/null
curl -s -X PUT "http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@localhost:${COUCHDB_PORT}/_node/_local/_config/cors/origins" -d '"*"' > /dev/null
curl -s -X PUT "http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@localhost:${COUCHDB_PORT}/_node/_local/_config/cors/credentials" -d '"true"' > /dev/null
curl -s -X PUT "http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@localhost:${COUCHDB_PORT}/_node/_local/_config/cors/methods" -d '"GET, PUT, POST, HEAD, DELETE"' > /dev/null
curl -s -X PUT "http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@localhost:${COUCHDB_PORT}/_node/_local/_config/cors/headers" -d '"accept, authorization, content-type, origin, referer, x-csrf-token"' > /dev/null
echo "Verifying CORS configuration..."
# Give CORS configuration a moment to take effect
sleep 2
if curl -s -I -H "Origin: http://127.0.0.1:8080" "http://localhost:${COUCHDB_PORT}/${DB_NAME}/_all_docs" | grep -q "Access-Control-Allow-Origin"; then
echo "β
CORS configured successfully"
else
echo "β οΈ CORS configuration may need manual verification"
echo " Run: curl -I -H \"Origin: http://127.0.0.1:8080\" \"http://localhost:${COUCHDB_PORT}/${DB_NAME}/_all_docs\""
fi
echo "Installing npm dependencies..."
npm install
echo "Loading validation rules into CouchDB..."
npm run load ${DB_NAME} ${COUCHDB_USER} ${COUCHDB_PASSWORD}
echo ""
echo "π Setup complete!"
echo ""
echo "π What's available:"
echo " β’ CouchDB running on port ${COUCHDB_PORT}"
echo " β’ Database '${DB_NAME}' created with validation rules"
echo " β’ CORS configured for web interface access"
echo ""
echo "π Quick start:"
echo " β’ Web interface: npm run serve:web β http://127.0.0.1:8080"
echo " β’ Run tests: npm test"
echo " β’ Direct API: http://localhost:${COUCHDB_PORT}/_utils (CouchDB admin)"
echo ""
echo "π Documentation:"
echo " β’ README.md - Getting started guide"
echo " β’ TROUBLESHOOTING.md - Common issues and solutions"
echo ""
echo "π§ Troubleshooting:"
echo " β’ If CORS issues persist, see TROUBLESHOOTING.md"
echo " β’ Container name: couchdb-rules-engine"
echo " β’ To restart: docker restart couchdb-rules-engine"