Add installer script and serve static files
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
.env
|
||||
86
install.sh
Executable file
86
install.sh
Executable file
@@ -0,0 +1,86 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Backstor UI Installer Script
|
||||
|
||||
set -e
|
||||
|
||||
# Colors
|
||||
GREEN='\033[0;32m'
|
||||
RED='\033[0;31m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
echo -e "${GREEN}Starting Backstor UI Installation...${NC}"
|
||||
|
||||
# Check for root/sudo
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo -e "${RED}Please run as root (use sudo)${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PROJECT_DIR=$(pwd)
|
||||
SERVER_DIR="$PROJECT_DIR/server"
|
||||
SERVICE_NAME="backstor"
|
||||
|
||||
# 1. Install Node.js Dependencies
|
||||
echo "Installing Node.js dependencies..."
|
||||
if [ -d "$SERVER_DIR" ]; then
|
||||
cd "$SERVER_DIR"
|
||||
if [ ! -d "node_modules" ]; then
|
||||
npm install
|
||||
else
|
||||
echo "node_modules already exists. Skipping npm install."
|
||||
fi
|
||||
else
|
||||
echo -e "${RED}Error: server directory not found at $SERVER_DIR${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 2. Setup Environment Variables
|
||||
echo "Setting up environment configuration..."
|
||||
if [ ! -f ".env" ]; then
|
||||
if [ -f ".env.example" ]; then
|
||||
cp .env.example .env
|
||||
echo -e "${GREEN}Created .env from .env.example.${NC}"
|
||||
echo "Please edit $SERVER_DIR/.env with your database configuration."
|
||||
else
|
||||
echo -e "${RED}Warning: .env.example not found.${NC}"
|
||||
fi
|
||||
else
|
||||
echo ".env already exists."
|
||||
fi
|
||||
|
||||
# 3. Create Systemd Service
|
||||
echo "Creating systemd service..."
|
||||
|
||||
# Detect the user who owns the project directory to run the service as
|
||||
REAL_USER=$(stat -c '%U' "$PROJECT_DIR")
|
||||
NODE_PATH=$(which node)
|
||||
|
||||
cat > /etc/systemd/system/$SERVICE_NAME.service <<EOF
|
||||
[Unit]
|
||||
Description=Backstor UI Service
|
||||
After=network.target postgresql.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=$REAL_USER
|
||||
WorkingDirectory=$SERVER_DIR
|
||||
ExecStart=$NODE_PATH index.js
|
||||
Restart=on-failure
|
||||
Environment=PORT=3000
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
echo -e "${GREEN}Service file created at /etc/systemd/system/$SERVICE_NAME.service${NC}"
|
||||
|
||||
# 4. Enable and Start Service
|
||||
echo "Enabling and starting service..."
|
||||
systemctl daemon-reload
|
||||
systemctl enable $SERVICE_NAME
|
||||
systemctl restart $SERVICE_NAME
|
||||
|
||||
echo -e "${GREEN}Installation Complete!${NC}"
|
||||
echo -e "You can check the status with: ${GREEN}systemctl status $SERVICE_NAME${NC}"
|
||||
echo -e "Access the UI at: ${GREEN}http://$(hostname -I | awk '{print $1}'):3000${NC}"
|
||||
@@ -1,5 +1,6 @@
|
||||
require('dotenv').config();
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
const cors = require('cors');
|
||||
const { Pool } = require('pg');
|
||||
const { exec } = require('child_process');
|
||||
@@ -10,6 +11,9 @@ const PORT = process.env.PORT || 3000;
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// Serve static files from the parent directory (frontend)
|
||||
app.use(express.static(path.join(__dirname, '../')));
|
||||
|
||||
// Database Connection Pool (PostgreSQL)
|
||||
const pool = new Pool({
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
|
||||
Reference in New Issue
Block a user