Skip to content

Commit

Permalink
Merge pull request #267 from carverauto/updates/standalone_mode_build
Browse files Browse the repository at this point in the history
Updates/standalone mode build
  • Loading branch information
mfreeman451 authored Mar 2, 2025
2 parents f3e0fa6 + 4f8010d commit 4c3620e
Show file tree
Hide file tree
Showing 24 changed files with 1,133 additions and 491 deletions.
1 change: 1 addition & 0 deletions Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
libsqlite3-dev \
make \
nodejs \
iproute2 \
&& rm -rf /var/lib/apt/lists/*

# Verify installations
Expand Down
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), // Pass the API key from config
)

server.SetAPIServer(apiServer)
Expand Down
33 changes: 33 additions & 0 deletions packaging/cloud/config/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
server {
listen 80 default_server;
server_name _;

access_log /var/log/nginx/serviceradar.access.log;
error_log /var/log/nginx/serviceradar.error.log;

# API endpoints
location /api/ {
proxy_pass http://localhost:8090;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

# WebSocket support (for Next.js if needed)
location /_next/webpack-hmr {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

# Main UI
location / {
proxy_pass http://localhost: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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
26 changes: 4 additions & 22 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,11 @@ 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 {
// 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 Expand Up @@ -173,8 +160,6 @@ func (s *APIServer) getNodeHistory(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
nodeID := vars["id"]

log.Printf("Getting node history for: %s", nodeID)

if s.nodeHistoryHandler == nil {
http.Error(w, "History handler not configured", http.StatusInternalServerError)
return
Expand All @@ -188,8 +173,6 @@ func (s *APIServer) getNodeHistory(w http.ResponseWriter, r *http.Request) {
return
}

log.Printf("Fetched %d history points for node: %s", len(points), nodeID)

if err := s.encodeJSONResponse(w, points); err != nil {
log.Printf("Error encoding history response: %v", err)
http.Error(w, "Error encoding response", http.StatusInternalServerError)
Expand Down Expand Up @@ -291,7 +274,6 @@ func (s *APIServer) getNode(w http.ResponseWriter, r *http.Request) {

node, exists := s.getNodeByID(nodeID)
if !exists {
log.Printf("Node %s not found", nodeID)
http.Error(w, "Node not found", http.StatusNotFound)

return
Expand Down
3 changes: 0 additions & 3 deletions pkg/cloud/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/carverauto/serviceradar/pkg/checker/snmp"
"github.com/carverauto/serviceradar/pkg/db"
"github.com/carverauto/serviceradar/pkg/metrics"
"github.com/carverauto/serviceradar/pkg/models"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -55,7 +54,5 @@ type APIServer struct {
nodeHistoryHandler func(nodeID string) ([]NodeHistoryPoint, error)
metricsManager metrics.MetricCollector
snmpManager snmp.SNMPManager
db db.Service
knownPollers []string
apiKey string
}
16 changes: 0 additions & 16 deletions pkg/cloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ func NewServer(_ context.Context, config *Config) (*Server, error) {
config.Metrics.MaxNodes = 10000
}

// log the config.Metrics
log.Printf("Metrics config: %+v", config.Metrics)

metricsManager := metrics.NewManager(models.MetricsConfig{
Enabled: config.Metrics.Enabled,
Retention: config.Metrics.Retention,
Expand Down Expand Up @@ -336,13 +333,6 @@ func (s *Server) SetAPIServer(apiServer api.Service) {
return nil, fmt.Errorf("failed to get node history: %w", err)
}

// debug points
log.Printf("Fetched %d history points for node: %s", len(points), nodeID)
// log first 20 points
for i := 0; i < 20 && i < len(points); i++ {
log.Printf("Point %d: %v", i, points[i])
}

apiPoints := make([]api.NodeHistoryPoint, len(points))
for i, p := range points {
apiPoints[i] = api.NodeHistoryPoint{
Expand All @@ -356,8 +346,6 @@ func (s *Server) SetAPIServer(apiServer api.Service) {
}

func (s *Server) checkInitialStates() {
log.Printf("Checking initial states of all nodes")

likeConditions := make([]string, 0, len(s.pollerPatterns))
args := make([]interface{}, 0, len(s.pollerPatterns))

Expand Down Expand Up @@ -562,8 +550,6 @@ func (s *Server) processSNMPMetrics(nodeID string, details json.RawMessage, time
Metadata: metadata,
}

log.Printf("Storing SNMP metric %s for node %s, value: %s", oidName, nodeID, valueStr)

// Store in database
if err := s.db.StoreMetric(nodeID, metric); err != nil {
log.Printf("Error storing SNMP metric %s for node %s: %v", oidName, nodeID, err)
Expand Down Expand Up @@ -593,8 +579,6 @@ func (*Server) processSweepData(svc *api.ServiceStatus, now time.Time) error {
return fmt.Errorf("%w: %w", errInvalidSweepData, err)
}

log.Printf("Received sweep data with timestamp: %v", time.Unix(sweepData.LastSweep, 0).Format(time.RFC3339))

// If LastSweep is not set or is invalid (0 or negative), use current time
if sweepData.LastSweep > now.Add(oneDay).Unix() {
log.Printf("Invalid or missing LastSweep timestamp (%d), using current time", sweepData.LastSweep)
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
11 changes: 0 additions & 11 deletions scripts/build-web.sh

This file was deleted.

3 changes: 2 additions & 1 deletion scripts/buildAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
VERSION=${VERSION:-1.0.19}


./scripts/setup-deb-agent.sh
./scripts/setup-deb-poller.sh
./scripts/setup-deb-web.sh
./scripts/setup-deb-dusk-checker.sh
./scripts/setup-deb-agent.sh
./scripts/setup-deb-snmp-checker.sh

scp ./release-artifacts/serviceradar-poller_${VERSION}.deb [email protected]:~/
Expand Down
Loading

0 comments on commit 4c3620e

Please sign in to comment.