-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
Β·245 lines (223 loc) Β· 6.84 KB
/
setup.sh
File metadata and controls
executable file
Β·245 lines (223 loc) Β· 6.84 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash
# Aframp Backend Setup Script
# This script helps set up the development environment
set -e # Exit on any error
# Detect operating system
detect_os() {
case "$(uname -s)" in
Linux*) OS="linux";;
Darwin*) OS="macos";;
CYGWIN*) OS="cygwin";;
MINGW*) OS="mingw";;
MSYS*) OS="msys";;
*) OS="unknown";;
esac
echo "$OS"
}
# Detect Linux distribution
detect_distro() {
if [ -f /etc/os-release ]; then
. /etc/os-release
if [ -n "$ID" ]; then
echo "$ID"
elif [ -n "$ID_LIKE" ]; then
echo "${ID_LIKE%% *}"
else
echo "unknown"
fi
elif [ -f /etc/arch-release ]; then
echo "arch"
elif [ -f /etc/debian_version ]; then
echo "debian"
else
echo "unknown"
fi
}
normalize_distro() {
case "$1" in
arch|archlinux|manjaro|endeavouros|blackarch|garuda|artix|cachyos)
echo "arch"
;;
debian|ubuntu|linuxmint|pop)
echo "debian"
;;
fedora|rhel|centos|rocky|almalinux)
echo "fedora"
;;
*)
echo "$1"
;;
esac
}
detect_arch_pgdata() {
if [ -n "${PGDATA:-}" ]; then
echo "$PGDATA"
return
fi
local pgroot
pgroot="$(systemctl cat postgresql 2>/dev/null | sed -n 's/.*Environment=PGROOT=\([^[:space:]]*\).*/\1/p' | head -n1)"
if [ -n "$pgroot" ]; then
echo "$pgroot/data"
return
fi
for dir in /var/lib/postgres/data /var/lib/postgresql/data /var/lib/pgsql/data; do
if [ -d "$dir" ] || [ -f "$dir/PG_VERSION" ]; then
echo "$dir"
return
fi
done
echo "/var/lib/postgres/data"
}
OS=$(detect_os)
DISTRO=""
# Validate supported OS and detect distribution
echo "π Detecting operating system..."
case "$OS" in
linux)
DISTRO=$(normalize_distro "$(detect_distro)")
echo "β
Detected OS: Linux ($DISTRO)"
;;
macos)
echo "β
Detected OS: macOS"
;;
*)
echo "β Unsupported operating system: $OS"
echo "This setup script supports Linux and macOS only."
echo "Please install the required dependencies manually:"
echo " - Rust (https://rustup.rs/)"
echo " - PostgreSQL (https://www.postgresql.org/download/)"
echo " - Redis (https://redis.io/download)"
exit 1
;;
esac
echo "π Setting up Aframp Backend Development Environment"
# Check if Rust is installed
if ! command -v rustc &> /dev/null; then
echo "π¦ Installing Rust..."
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
else
echo "β
Rust is already installed"
fi
# Check if PostgreSQL is installed
if ! command -v psql &> /dev/null; then
echo "π Installing PostgreSQL..."
if [[ "$OS" == "linux" ]]; then
case "$DISTRO" in
arch)
sudo pacman -Sy --noconfirm postgresql
;;
debian)
sudo apt update
sudo apt install -y postgresql postgresql-contrib
;;
fedora)
sudo dnf install -y postgresql-server postgresql-contrib
;;
*)
echo "β Unsupported Linux distribution: $DISTRO"
echo "Please install PostgreSQL manually."
exit 1
;;
esac
elif [[ "$OS" == "macos" ]]; then
brew install postgresql
else
echo "β Unsupported OS. Please install PostgreSQL manually."
exit 1
fi
else
echo "β
PostgreSQL is already installed"
fi
# Check if Redis is installed
if ! command -v redis-cli &> /dev/null; then
echo "π§ Installing Redis..."
if [[ "$OS" == "linux" ]]; then
case "$DISTRO" in
arch)
sudo pacman -Sy --noconfirm redis
;;
debian)
sudo apt install -y redis-server
;;
fedora)
sudo dnf install -y redis
;;
*)
echo "β Unsupported Linux distribution: $DISTRO"
echo "Please install Redis manually."
exit 1
;;
esac
elif [[ "$OS" == "macos" ]]; then
brew install redis
else
echo "β Unsupported OS. Please install Redis manually."
exit 1
fi
else
echo "β
Redis is already installed"
fi
# Start services
echo "π Starting services..."
if [[ "$OS" == "linux" ]]; then
case "$DISTRO" in
arch)
ARCH_PGDATA="$(detect_arch_pgdata)"
echo "βΉοΈ Using PostgreSQL data directory: $ARCH_PGDATA"
# Initialize PostgreSQL database if not already done
if sudo -u postgres test -f "$ARCH_PGDATA/PG_VERSION"; then
echo "β
Existing PostgreSQL cluster detected"
elif sudo find "$ARCH_PGDATA" -mindepth 1 -maxdepth 1 -print -quit 2>/dev/null | grep -q .; then
echo "β οΈ PostgreSQL data directory is not empty and PG_VERSION is missing."
echo "β οΈ Skipping initdb to avoid overwriting existing files."
else
sudo install -d -m 700 -o postgres -g postgres "$ARCH_PGDATA"
sudo -u postgres initdb -D "$ARCH_PGDATA"
fi
sudo systemctl start postgresql
sudo systemctl start redis
;;
debian|fedora)
sudo systemctl start postgresql
sudo systemctl start redis
;;
esac
elif [[ "$OS" == "macos" ]]; then
brew services start postgresql
brew services start redis
fi
# Create database
echo "π Creating database..."
sudo -u postgres createdb aframp 2>/dev/null || echo "β
Database already exists"
sudo -u postgres createuser -s $USER 2>/dev/null || echo "β
User already exists"
# Install sqlx CLI
if ! command -v sqlx &> /dev/null; then
echo "π§ Installing sqlx CLI..."
cargo install --features postgres sqlx-cli --quiet
else
echo "β
sqlx CLI is already installed"
fi
# Run migrations
echo "π Running database migrations..."
DATABASE_URL=postgresql:///aframp sqlx migrate run
# Create .env file if it doesn't exist
if [ ! -f .env ]; then
echo "π Creating .env file..."
cp .env.example .env
echo "β
Created .env file. Please review and update as needed."
else
echo "β
.env file already exists"
fi
# Build the project
echo "ποΈ Building the project..."
cargo build
echo ""
echo "π Setup complete!"
echo ""
echo "Next steps:"
echo "1. Review and update the .env file with your configuration"
echo "2. Run the server: cargo run"
echo "3. For development: cargo watch -x run (install cargo-watch first)"
echo ""
echo "For more information, check the README.md and QUICK_START.md files"