Skip to content

Commit

Permalink
fetching most data properly just not sparkline or snmp
Browse files Browse the repository at this point in the history
  • Loading branch information
mfreeman451 committed Mar 1, 2025
1 parent aa92943 commit 1889ea2
Show file tree
Hide file tree
Showing 18 changed files with 540 additions and 329 deletions.
1 change: 0 additions & 1 deletion cmd/cloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func run() error {
apiServer := api.NewAPIServer(
api.WithMetricsManager(server.GetMetricsManager()),
api.WithSNMPManager(server.GetSNMPManager()),
api.WithAPIKey(cfg.APIKey),
)

server.SetAPIServer(apiServer)
Expand Down
23 changes: 6 additions & 17 deletions pkg/cloud/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"encoding/json"
"log"
"net/http"
"os"
"time"

"github.com/carverauto/serviceradar/pkg/checker/snmp"
"github.com/carverauto/serviceradar/pkg/db"
srHttp "github.com/carverauto/serviceradar/pkg/http"
"github.com/carverauto/serviceradar/pkg/metrics"
"github.com/gorilla/mux"
Expand All @@ -19,14 +19,13 @@ func NewAPIServer(options ...func(server *APIServer)) *APIServer {
s := &APIServer{
nodes: make(map[string]*NodeStatus),
router: mux.NewRouter(),
apiKey: "", // Default empty API key
}

for _, o := range options {
o(s)
}

s.setupRoutes(s.apiKey)
s.setupRoutes()

return s
}
Expand All @@ -43,23 +42,13 @@ func WithSNMPManager(m snmp.SNMPManager) func(server *APIServer) {
}
}

func WithAPIKey(apiKey string) func(server *APIServer) {
return func(server *APIServer) {
server.apiKey = apiKey
}
}

func WithDB(db db.Service) func(server *APIServer) {
return func(server *APIServer) {
server.db = db
}
}

func (s *APIServer) setupRoutes(apiKey string) {
func (s *APIServer) setupRoutes() {
// Create a middleware chain
middlewareChain := func(next http.Handler) http.Handler {
log.Printf("SERVER API_KEY: %s", os.Getenv("API_KEY"))

// Order matters: first API key check, then CORS headers
return srHttp.CommonMiddleware(srHttp.APIKeyMiddleware(apiKey)(next))
return srHttp.CommonMiddleware(srHttp.APIKeyMiddleware(os.Getenv("API_KEY"))(next))
}

// Add middleware to router
Expand Down
1 change: 0 additions & 1 deletion pkg/cloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type Config struct {
Metrics Metrics `json:"metrics"`
SNMP snmp.Config `json:"snmp"`
Security *models.SecurityConfig `json:"security"`
APIKey string `json:"api_key,omitempty"`
}

type Server struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func APIKeyMiddleware(apiKeyParam string) func(next http.Handler) http.Handler {
return
}

// print out the full request header
log.Printf("Request Header: %v", r.Header)

// Check API key in header or query parameter
requestKey := r.Header.Get("X-API-Key")
if requestKey == "" {
Expand Down
42 changes: 10 additions & 32 deletions scripts/setup-deb-cloud.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# setup-deb-cloud.sh
# setup-deb-cloud.sh - UPDATED
set -e # Exit on any error

echo "Setting up package structure..."
Expand Down Expand Up @@ -61,6 +61,7 @@ After=network.target
[Service]
Type=simple
User=serviceradar
EnvironmentFile=/etc/serviceradar/api.env
ExecStart=/usr/local/bin/serviceradar-cloud -config /etc/serviceradar/cloud.json
Restart=always
RestartSec=10
Expand Down Expand Up @@ -136,26 +137,14 @@ mkdir -p /var/lib/serviceradar
chown -R serviceradar:serviceradar /var/lib/serviceradar
chmod 755 /var/lib/serviceradar
# Configure Nginx
# Only enable if serviceradar-web is not installed
if [ ! -f /etc/nginx/conf.d/serviceradar-web.conf ] && [ ! -f /etc/nginx/sites-enabled/serviceradar-web.conf ]; then
echo "Configuring Nginx for API-only access..."
# Disable default site if it exists
if [ -f /etc/nginx/sites-enabled/default ]; then
rm -f /etc/nginx/sites-enabled/default
fi
# Create symbolic link if Nginx uses sites-enabled pattern
if [ -d /etc/nginx/sites-enabled ]; then
ln -sf /etc/nginx/conf.d/serviceradar-cloud.conf /etc/nginx/sites-enabled/
fi
# Test and reload Nginx
nginx -t || { echo "Warning: Nginx configuration test failed. Please check your configuration."; }
systemctl reload nginx || systemctl restart nginx || echo "Warning: Failed to reload/restart Nginx."
else
echo "Detected serviceradar-web configuration, skipping API-only Nginx setup."
# Generate API key if it doesn't exist
if [ ! -f "/etc/serviceradar/api.env" ]; then
echo "Generating API key..."
API_KEY=\$(openssl rand -hex 32)
echo "API_KEY=\$API_KEY" > /etc/serviceradar/api.env
chmod 600 /etc/serviceradar/api.env
chown serviceradar:serviceradar /etc/serviceradar/api.env
echo "API key generated and stored in /etc/serviceradar/api.env"
fi
# Enable and start service
Expand All @@ -182,17 +171,6 @@ set -e
systemctl stop serviceradar-cloud || true
systemctl disable serviceradar-cloud || true
# Remove Nginx symlink if exists and if it's our configuration
if [ -f /etc/nginx/sites-enabled/serviceradar-cloud.conf ]; then
rm -f /etc/nginx/sites-enabled/serviceradar-cloud.conf
# Reload Nginx if running
if systemctl is-active --quiet nginx; then
systemctl reload nginx || true
fi
fi
exit 0
EOF

chmod 755 "${PKG_ROOT}/DEBIAN/prerm"
Expand Down
30 changes: 18 additions & 12 deletions scripts/setup-deb-web.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# setup-deb-web.sh
# setup-deb-web.sh - UPDATED
set -e # Exit on any error

echo "Setting up package structure for Next.js web interface..."
Expand All @@ -25,8 +25,7 @@ if ! grep -q '"next": ' package.json; then
exit 1
fi

# Install dependencies with pnpm
#npm install -g pnpm
# Install dependencies with npm
npm install

# Build the Next.js application
Expand All @@ -38,19 +37,15 @@ echo "Copying Next.js standalone build to package..."
cp -r .next/standalone/* "../${PKG_ROOT}/usr/local/share/serviceradar-web/"
cp -r .next/standalone/.next "../${PKG_ROOT}/usr/local/share/serviceradar-web/"

# Ensure styled-jsx is properly included
mkdir -p "../${PKG_ROOT}/usr/local/share/serviceradar-web/node_modules/styled-jsx/dist"
cp -r node_modules/styled-jsx/dist/* "../${PKG_ROOT}/usr/local/share/serviceradar-web/node_modules/styled-jsx/dist/"
cp node_modules/styled-jsx/package.json "../${PKG_ROOT}/usr/local/share/serviceradar-web/node_modules/styled-jsx/"
# Make sure static files are copied
mkdir -p "../${PKG_ROOT}/usr/local/share/serviceradar-web/.next/static"
cp -r .next/static "../${PKG_ROOT}/usr/local/share/serviceradar-web/.next/"

# Copy public files if they exist
if [ -d "public" ]; then
cp -r public "../${PKG_ROOT}/usr/local/share/serviceradar-web/"
fi

# Cleanup temp directory
rm -rf "$TEMP_DIR"

cd ..

echo "Creating package files..."
Expand Down Expand Up @@ -93,7 +88,7 @@ server {
# Main app - proxy all requests to Next.js
location / {
proxy_pass http://localhost:3000;
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
Expand Down Expand Up @@ -136,6 +131,7 @@ User=serviceradar
WorkingDirectory=/usr/local/share/serviceradar-web
Environment=NODE_ENV=production
Environment=PORT=3000
EnvironmentFile=/etc/serviceradar/api.env
ExecStart=/usr/bin/node server.js
Restart=always
RestartSec=10
Expand Down Expand Up @@ -173,6 +169,17 @@ chown -R serviceradar:serviceradar /etc/serviceradar/web.json
chmod 755 /usr/local/share/serviceradar-web
chmod 644 /etc/serviceradar/web.json
# Check for API key from cloud package
if [ ! -f "/etc/serviceradar/api.env" ]; then
echo "WARNING: API key file not found. The serviceradar-cloud package should be installed first."
echo "Creating a temporary API key file..."
API_KEY=\$(openssl rand -hex 32)
echo "API_KEY=\$API_KEY" > /etc/serviceradar/api.env
chmod 600 /etc/serviceradar/api.env
chown serviceradar:serviceradar /etc/serviceradar/api.env
echo "For proper functionality, please reinstall serviceradar-cloud package."
fi
# Configure Nginx
if [ -f /etc/nginx/sites-enabled/default ]; then
echo "Disabling default Nginx site..."
Expand All @@ -197,7 +204,6 @@ systemctl start serviceradar-web || echo "Failed to start service, please check
echo "ServiceRadar Web Interface installed successfully!"
echo "Web UI is running on port 3000"
echo "Nginx configured as reverse proxy - you can access the UI at http://localhost/"
echo "Note: For full functionality, install the serviceradar-cloud package"
exit 0
EOF
Expand Down
6 changes: 6 additions & 0 deletions web/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const nextConfig: NextConfig = {
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8090',
NEXT_PUBLIC_BACKEND_URL: process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8090/',
},
serverRuntimeConfig: {
// Will only be available on the server side
apiKey: process.env.API_KEY || '',
}
};

console.log('Next.js configuration loaded with API_KEY length:', process.env.API_KEY)

export default nextConfig;
Loading

0 comments on commit 1889ea2

Please sign in to comment.