Skip to content

Commit

Permalink
SSR WIP - not getting data back from checkers for api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mfreeman451 committed Feb 26, 2025
1 parent fc0ce96 commit 1b486a7
Show file tree
Hide file tree
Showing 44 changed files with 180 additions and 5,936 deletions.
23 changes: 0 additions & 23 deletions pkg/cloud/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
package api

import (
"embed"
"encoding/json"
"io/fs"
"log"
"net/http"
"sync"
Expand Down Expand Up @@ -98,23 +96,6 @@ func WithDB(db db.Service) func(server *APIServer) {
server.db = db
}
}

//go:embed web/dist/*
var webContent embed.FS

// setupStaticFileServing configures static file serving for the embedded web files.
func (s *APIServer) setupStaticFileServing() {
// Setting up static file serving using the embedded FS
// This is used for non-containerized builds
fsys, err := fs.Sub(webContent, "web/dist")
if err != nil {
log.Printf("Error setting up static file serving: %v", err)
return
}

s.router.PathPrefix("/").Handler(http.FileServer(http.FS(fsys)))
}

func (s *APIServer) setupRoutes() {
// Create a middleware chain
middlewareChain := func(next http.Handler) http.Handler {
Expand Down Expand Up @@ -142,10 +123,6 @@ func (s *APIServer) setupRoutes() {

// SNMP endpoints
s.router.HandleFunc("/api/nodes/{id}/snmp", s.getSNMPData).Methods("GET")

// Configure static file serving based on build tags
// This is managed via build tags in a separate file
s.configureStaticServing()
}

// getSNMPData retrieves SNMP data for a specific node.
Expand Down
34 changes: 0 additions & 34 deletions pkg/cloud/api/static_serve_container.go

This file was deleted.

10 changes: 0 additions & 10 deletions pkg/cloud/api/static_serve_non_container.go

This file was deleted.

38 changes: 19 additions & 19 deletions scripts/setup-deb-cloud.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e # Exit on any error

echo "Setting up package structure..."

VERSION=${VERSION:-1.0.19}
VERSION=${VERSION:-1.0.20}
BUILD_TAGS=${BUILD_TAGS:-""}

# Create package directory structure
Expand All @@ -13,28 +13,28 @@ mkdir -p "${PKG_ROOT}/DEBIAN"
mkdir -p "${PKG_ROOT}/usr/local/bin"
mkdir -p "${PKG_ROOT}/etc/serviceradar"
mkdir -p "${PKG_ROOT}/lib/systemd/system"
mkdir -p "${PKG_ROOT}/usr/local/share/serviceradar-cloud/web"
#mkdir -p "${PKG_ROOT}/usr/local/share/serviceradar-cloud/web"

echo "Building web interface..."
#echo "Building web interface..."

# Build web interface if not already built
if [ ! -d "web/dist" ]; then
cd ./web
npm install
npm run build
cd ..
fi
#if [ ! -d "web/dist" ]; then
# cd ./web
# npm install
# npm run build
# cd ..
#fi

# Create a directory for the embedded content
mkdir -p pkg/cloud/api/web
cp -r web/dist pkg/cloud/api/web/
#mkdir -p pkg/cloud/api/web
#cp -r web/dist pkg/cloud/api/web/

# Only copy web assets to package directory for container builds
# For non-container builds, they're embedded in the binary
if [[ "$BUILD_TAGS" == *"containers"* ]]; then
cp -r web/dist "${PKG_ROOT}/usr/local/share/serviceradar-cloud/web/"
echo "Copied web assets for container build"
fi
#if [[ "$BUILD_TAGS" == *"containers"* ]]; then
# cp -r web/dist "${PKG_ROOT}/usr/local/share/serviceradar-cloud/web/"
# echo "Copied web assets for container build"
#fi

echo "Building Go binary..."

Expand Down Expand Up @@ -153,10 +153,10 @@ chown -R serviceradar:serviceradar /var/lib/serviceradar
chmod 755 /var/lib/serviceradar
# Set permissions for web assets
if [ -d "/usr/local/share/serviceradar-cloud/web" ]; then
chown -R serviceradar:serviceradar /usr/local/share/serviceradar-cloud
chmod -R 755 /usr/local/share/serviceradar-cloud
fi
#if [ -d "/usr/local/share/serviceradar-cloud/web" ]; then
# chown -R serviceradar:serviceradar /usr/local/share/serviceradar-cloud
# chmod -R 755 /usr/local/share/serviceradar-cloud
#fi
# Enable and start service
systemctl daemon-reload
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
34 changes: 34 additions & 0 deletions serviceradar-next/src/app/nodes/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Suspense } from 'react';
import NodeList from '../../components/NodeList';

export const revalidate = 10; // Revalidate this page every 10 seconds

// Async function to fetch data on the server
async function fetchNodes() {
try {
// When running on the server, use the full backend URL
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8090';
const response = await fetch(`${backendUrl}/api/nodes`);

if (!response.ok) {
throw new Error(`Nodes API request failed: ${response.status}`);
}

return await response.json();
} catch (error) {
console.error('Error fetching nodes:', error);
return [];
}
}

export default async function NodesPage() {
const initialNodes = await fetchNodes();

return (
<div>
<Suspense fallback={<div>Loading nodes...</div>}>
<NodeList initialNodes={initialNodes} />
</Suspense>
</div>
);
}
53 changes: 53 additions & 0 deletions serviceradar-next/src/app/providers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use client';

import { createContext, useState, useEffect, useContext } from 'react';
import Navbar from '../components/Navbar';

// Create context for theme management
export const ThemeContext = createContext({
darkMode: false,
setDarkMode: () => {},
});

export function Providers({ children }) {
const [darkMode, setDarkMode] = useState(false);
const [mounted, setMounted] = useState(false);

// Effect for initial load of dark mode preference
useEffect(() => {
const savedMode = localStorage.getItem('darkMode');
setDarkMode(savedMode === 'true');
setMounted(true);
}, []);

// Effect to save dark mode preference when it changes
useEffect(() => {
if (mounted) {
localStorage.setItem('darkMode', darkMode);
document.documentElement.classList.toggle('dark', darkMode);
}
}, [darkMode, mounted]);

// Prevent flash of incorrect theme
if (!mounted) {
return null;
}

return (
<ThemeContext.Provider value={{ darkMode, setDarkMode }}>
<div className={darkMode ? 'dark' : ''}>
<div className="min-h-screen bg-gray-100 dark:bg-gray-900 transition-colors">
<Navbar />
<main className="container mx-auto px-4 py-8">
{children}
</main>
</div>
</div>
</ThemeContext.Provider>
);
}

// Custom hook to use the theme context
export function useTheme() {
return useContext(ThemeContext);
}
72 changes: 72 additions & 0 deletions serviceradar-next/src/app/service/[nodeid]/[servicename]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Suspense } from 'react';
import ServiceDashboard from '../../../../components/ServiceDashboard';

export const revalidate = 10; // Revalidate this page every 10 seconds

// Async function to fetch data on the server
async function fetchServiceData(nodeId, serviceName) {
try {
// When running on the server, use the full backend URL
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8090';

// Fetch nodes list
const nodesResponse = await fetch(`${backendUrl}/api/nodes`);
if (!nodesResponse.ok) {
throw new Error(`Nodes API request failed: ${nodesResponse.status}`);
}
const nodes = await nodesResponse.json();

// Find the specific node
const node = nodes.find((n) => n.node_id === nodeId);
if (!node) {
return { error: 'Node not found' };
}

// Find the specific service
const service = node.services?.find((s) => s.name === serviceName);
if (!service) {
return { error: 'Service not found' };
}

// Fetch metrics data
try {
const metricsResponse = await fetch(`${backendUrl}/api/nodes/${nodeId}/metrics`);
if (!metricsResponse.ok) {
throw new Error(`Metrics API request failed: ${metricsResponse.status}`);
}

const metrics = await metricsResponse.json();
const serviceMetrics = metrics.filter(
(m) => m.service_name === serviceName
);

return { service, metrics: serviceMetrics };
} catch (metricsError) {
console.error('Error fetching metrics data:', metricsError);
// Don't fail the whole request if metrics fail
return { service, metrics: [] };
}
} catch (err) {
console.error('Error fetching data:', err);
return { error: err.message };
}
}

export default async function ServicePage({ params }) {
const { nodeId, serviceName } = params;
const initialData = await fetchServiceData(nodeId, serviceName);

return (
<div>
<Suspense fallback={<div>Loading service data...</div>}>
<ServiceDashboard
nodeId={nodeId}
serviceName={serviceName}
initialService={initialData.service}
initialMetrics={initialData.metrics}
initialError={initialData.error}
/>
</Suspense>
</div>
);
}
21 changes: 0 additions & 21 deletions web/dist/index.html

This file was deleted.

20 changes: 0 additions & 20 deletions web/index.html

This file was deleted.

Loading

0 comments on commit 1b486a7

Please sign in to comment.