diff --git a/installer/fix-templates.sh b/installer/fix-templates.sh new file mode 100644 index 0000000..575c9b7 --- /dev/null +++ b/installer/fix-templates.sh @@ -0,0 +1,144 @@ +#!/bin/bash +# +# Quick fix script to copy web templates and static files to installation directory +# Run this on the remote machine where templates are missing +# +# Usage: sudo ./installer/fix-templates.sh [--install-dir /opt/atlas] +# + +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Default values +INSTALL_DIR="/opt/atlas" +REPO_DIR="" + +# Parse command line arguments +while [[ $# -gt 0 ]]; do + case $1 in + --install-dir) + INSTALL_DIR="$2" + shift 2 + ;; + --repo-dir) + REPO_DIR="$2" + shift 2 + ;; + -h|--help) + echo "Fix Templates Script" + echo "" + echo "Usage: sudo ./installer/fix-templates.sh [options]" + echo "" + echo "Options:" + echo " --install-dir DIR Installation directory (default: /opt/atlas)" + echo " --repo-dir DIR Repository directory (if not in current dir)" + echo " -h, --help Show this help message" + exit 0 + ;; + *) + echo "Unknown option: $1" + exit 1 + ;; + esac +done + +# Check if running as root +if [[ $EUID -ne 0 ]]; then + echo -e "${RED}Error: This script must be run as root (use sudo)${NC}" + exit 1 +fi + +# Get script directory +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# If script is in installer/ subdirectory, get parent (repo root) +if [[ "$(basename "$SCRIPT_DIR")" == "installer" ]]; then + REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +else + REPO_ROOT="$SCRIPT_DIR" +fi + +# Function to check if directory is valid repo root +is_repo_root() { + local dir="$1" + [[ -f "$dir/go.mod" ]] && [[ -d "$dir/internal" ]] && [[ -d "$dir/web/templates" ]] +} + +# Find source directory +SOURCE_DIR="" +if [[ -n "$REPO_DIR" ]] && is_repo_root "$REPO_DIR"; then + SOURCE_DIR="$(cd "$REPO_DIR" && pwd)" +elif is_repo_root "$REPO_ROOT"; then + SOURCE_DIR="$REPO_ROOT" +elif is_repo_root "."; then + SOURCE_DIR="$(pwd)" +fi + +if [[ -z "$SOURCE_DIR" ]]; then + echo -e "${RED}Error: Cannot find repository root with web/templates directory${NC}" + echo " Current directory: $(pwd)" + echo " Script directory: $SCRIPT_DIR" + echo "" + echo " Please run this script from the atlas repository root" + echo " Or specify path with: --repo-dir /path/to/atlas" + exit 1 +fi + +echo -e "${GREEN}Copying web files...${NC}" +echo " Source: $SOURCE_DIR/web" +echo " Destination: $INSTALL_DIR/web" + +# Create web directories in install directory +mkdir -p "$INSTALL_DIR/web/templates" +mkdir -p "$INSTALL_DIR/web/static" + +# Check if templates exist +if [[ ! -d "$SOURCE_DIR/web/templates" ]]; then + echo -e "${RED}Error: web/templates directory not found in $SOURCE_DIR${NC}" + exit 1 +fi + +# Copy templates +echo " Copying templates..." +if cp -r "$SOURCE_DIR/web/templates"/* "$INSTALL_DIR/web/templates/" 2>/dev/null; then + echo -e "${GREEN} ✓ Templates copied${NC}" +else + echo -e "${RED} ✗ Failed to copy templates${NC}" + exit 1 +fi + +# Copy static files if they exist +if [[ -d "$SOURCE_DIR/web/static" ]]; then + echo " Copying static files..." + if cp -r "$SOURCE_DIR/web/static"/* "$INSTALL_DIR/web/static/" 2>/dev/null; then + echo -e "${GREEN} ✓ Static files copied${NC}" + else + echo -e "${YELLOW} ⚠ Some static files may not have been copied${NC}" + fi +fi + +# Set ownership (use atlas user if it exists, otherwise root) +SERVICE_USER="atlas" +if id "$SERVICE_USER" &>/dev/null; then + chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR/web" + echo " Set ownership to $SERVICE_USER" +else + chown -R root:root "$INSTALL_DIR/web" + echo " Set ownership to root (atlas user not found)" +fi + +# Set permissions +chmod -R 755 "$INSTALL_DIR/web" + +echo "" +echo -e "${GREEN}Web files copied successfully!${NC}" +echo " Templates: $INSTALL_DIR/web/templates" +echo " Static: $INSTALL_DIR/web/static" +echo "" +echo "You may need to restart the atlas-api service:" +echo " sudo systemctl restart atlas-api" +echo "" diff --git a/installer/install.sh b/installer/install.sh index 0cf8c9c..47ef384 100755 --- a/installer/install.sh +++ b/installer/install.sh @@ -785,6 +785,75 @@ MAINGO echo " Global commands: atlas-api, atlas-tui" } +# Copy web files (templates and static assets) +copy_web_files() { + echo -e "${GREEN}Copying web files...${NC}" + + # Use BUILD_DIR from build_binaries if available, otherwise detect it + local source_dir="" + + # Function to check if directory is valid repo root + is_repo_root() { + local dir="$1" + [[ -f "$dir/go.mod" ]] && [[ -d "$dir/internal" ]] + } + + # Try REPO_DIR first + if [[ -n "$REPO_DIR" ]] && is_repo_root "$REPO_DIR"; then + source_dir="$(cd "$REPO_DIR" && pwd)" + # Try REPO_ROOT (if script is in installer/ subdirectory) + elif [[ -n "$REPO_ROOT" ]] && is_repo_root "$REPO_ROOT"; then + source_dir="$REPO_ROOT" + # Try current directory + elif is_repo_root "."; then + source_dir="$(pwd)" + # Try script directory + elif is_repo_root "$SCRIPT_DIR"; then + source_dir="$SCRIPT_DIR" + # Try parent of script directory + elif is_repo_root "$SCRIPT_DIR/.."; then + source_dir="$(cd "$SCRIPT_DIR/.." && pwd)" + fi + + if [[ -z "$source_dir" ]]; then + echo -e "${YELLOW}Warning: Cannot find repository root, skipping web files copy${NC}" + echo " Templates and static files may not be available" + return + fi + + # Check if web/templates exists + if [[ ! -d "$source_dir/web/templates" ]]; then + echo -e "${YELLOW}Warning: web/templates directory not found in $source_dir${NC}" + echo " Templates may not be available" + else + # Create web directories in install directory + mkdir -p "$INSTALL_DIR/web/templates" + mkdir -p "$INSTALL_DIR/web/static" + + # Copy templates + echo " Copying templates from $source_dir/web/templates..." + cp -r "$source_dir/web/templates"/* "$INSTALL_DIR/web/templates/" 2>/dev/null || { + echo -e "${YELLOW}Warning: Failed to copy some template files${NC}" + } + + # Copy static files if they exist + if [[ -d "$source_dir/web/static" ]]; then + echo " Copying static files from $source_dir/web/static..." + cp -r "$source_dir/web/static"/* "$INSTALL_DIR/web/static/" 2>/dev/null || { + echo -e "${YELLOW}Warning: Failed to copy some static files${NC}" + } + fi + + # Set ownership + chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR/web" + chmod -R 755 "$INSTALL_DIR/web" + + echo -e "${GREEN}Web files copied successfully${NC}" + echo " Templates: $INSTALL_DIR/web/templates" + echo " Static: $INSTALL_DIR/web/static" + fi +} + # Create systemd service create_systemd_service() { echo -e "${GREEN}Creating systemd service...${NC}" @@ -1244,6 +1313,9 @@ main() { # Step 6: Build binaries build_binaries + # Step 6.5: Copy web files (templates and static assets) + copy_web_files + # Step 7: Create configuration create_config generate_jwt_secret diff --git a/internal/httpapp/handlers.go b/internal/httpapp/handlers.go index a00bee3..00e9a55 100644 --- a/internal/httpapp/handlers.go +++ b/internal/httpapp/handlers.go @@ -151,7 +151,8 @@ func (a *App) render(w http.ResponseWriter, name string, data any) { w.Header().Set("Content-Type", "text/html; charset=utf-8") // base.html defines layout; dashboard.html will invoke it via template inheritance style. if err := a.tmpl.ExecuteTemplate(w, name, data); err != nil { - log.Printf("template render error: %v", err) + log.Printf("template render error: %v (template: %s, templates dir: %s)", err, name, a.cfg.TemplatesDir) + // In production, don't expose internal error details to client http.Error(w, "template render error", http.StatusInternalServerError) return }