Skip to content

Commit

Permalink
Merge pull request #256 from carverauto/253-refactorweb-switch-to-ssr…
Browse files Browse the repository at this point in the history
…next

253 refactorweb switch to ssrnext
  • Loading branch information
mfreeman451 authored Feb 27, 2025
2 parents fc0ce96 + 2b6fc73 commit 58eb361
Show file tree
Hide file tree
Showing 74 changed files with 8,365 additions and 5,226 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ jobs:
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: 'web/package-lock.json'
- name: Build web UI
run: ./scripts/build-web.sh
- name: Move web artifacts
run: mkdir -p pkg/cloud/api/web/ && cp -r web/dist pkg/cloud/api/web/
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ jspm_packages/
lib
.idea
.bin
bin

pnpm-lock.yaml

Expand Down
1 change: 1 addition & 0 deletions cmd/cloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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
83 changes: 10 additions & 73 deletions pkg/cloud/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,30 @@
package api

import (
"embed"
"encoding/json"
"io/fs"
"log"
"net/http"
"sync"
"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/carverauto/serviceradar/pkg/models"
"github.com/gorilla/mux"
)

type ServiceStatus struct {
Name string `json:"name"`
Available bool `json:"available"`
Message string `json:"message"`
Type string `json:"type"` // e.g., "process", "port", "blockchain", etc.
Details json.RawMessage `json:"details"` // Flexible field for service-specific data
}

type NodeStatus struct {
NodeID string `json:"node_id"`
IsHealthy bool `json:"is_healthy"`
LastUpdate time.Time `json:"last_update"`
Services []ServiceStatus `json:"services"`
UpTime string `json:"uptime"`
FirstSeen time.Time `json:"first_seen"`
Metrics []models.MetricPoint `json:"metrics,omitempty"`
}

type SystemStatus struct {
TotalNodes int `json:"total_nodes"`
HealthyNodes int `json:"healthy_nodes"`
LastUpdate time.Time `json:"last_update"`
}

type NodeHistory struct {
NodeID string
Timestamp time.Time
IsHealthy bool
Services []ServiceStatus
}

type NodeHistoryPoint struct {
Timestamp time.Time `json:"timestamp"`
IsHealthy bool `json:"is_healthy"`
}

type APIServer struct {
mu sync.RWMutex
nodes map[string]*NodeStatus
router *mux.Router
nodeHistoryHandler func(nodeID string) ([]NodeHistoryPoint, error)
metricsManager metrics.MetricCollector
snmpManager snmp.SNMPManager
db db.Service
knownPollers []string
}

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.setupRoutes(s.apiKey)

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

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

//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
func WithDB(db db.Service) func(server *APIServer) {
return func(server *APIServer) {
server.db = db
}

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

func (s *APIServer) setupRoutes() {
func (s *APIServer) setupRoutes(apiKey string) {
// 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(next))
return srHttp.CommonMiddleware(srHttp.APIKeyMiddleware(apiKey)(next))
}

// Add middleware to router
Expand All @@ -142,10 +82,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 Expand Up @@ -296,6 +232,7 @@ func (s *APIServer) getNodes(w http.ResponseWriter, _ *http.Request) {
for _, known := range s.knownPollers {
if id == known {
nodes = append(nodes, node)

break
}
}
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.

61 changes: 61 additions & 0 deletions pkg/cloud/api/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package api

import (
"encoding/json"
"sync"
"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"
)

type ServiceStatus struct {
Name string `json:"name"`
Available bool `json:"available"`
Message string `json:"message"`
Type string `json:"type"` // e.g., "process", "port", "blockchain", etc.
Details json.RawMessage `json:"details"` // Flexible field for service-specific data
}

type NodeStatus struct {
NodeID string `json:"node_id"`
IsHealthy bool `json:"is_healthy"`
LastUpdate time.Time `json:"last_update"`
Services []ServiceStatus `json:"services"`
UpTime string `json:"uptime"`
FirstSeen time.Time `json:"first_seen"`
Metrics []models.MetricPoint `json:"metrics,omitempty"`
}

type SystemStatus struct {
TotalNodes int `json:"total_nodes"`
HealthyNodes int `json:"healthy_nodes"`
LastUpdate time.Time `json:"last_update"`
}

type NodeHistory struct {
NodeID string
Timestamp time.Time
IsHealthy bool
Services []ServiceStatus
}

type NodeHistoryPoint struct {
Timestamp time.Time `json:"timestamp"`
IsHealthy bool `json:"is_healthy"`
}

type APIServer struct {
mu sync.RWMutex
nodes map[string]*NodeStatus
router *mux.Router
nodeHistoryHandler func(nodeID string) ([]NodeHistoryPoint, error)
metricsManager metrics.MetricCollector
snmpManager snmp.SNMPManager
db db.Service
knownPollers []string
apiKey string
}
11 changes: 0 additions & 11 deletions pkg/cloud/api/web/container.go

This file was deleted.

21 changes: 0 additions & 21 deletions pkg/cloud/api/web/dist/index.html

This file was deleted.

11 changes: 0 additions & 11 deletions pkg/cloud/api/web/non-container.go

This file was deleted.

1 change: 1 addition & 0 deletions pkg/cloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ 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
Loading

0 comments on commit 58eb361

Please sign in to comment.