Skip to content

Commit

Permalink
Merge pull request #243 from carverauto/bug/api_server_embed_missing
Browse files Browse the repository at this point in the history
added back embed stuff
  • Loading branch information
mfreeman451 authored Feb 24, 2025
2 parents 9669ff1 + a84fafc commit 7ea340c
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 71 deletions.
40 changes: 40 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ clean: ## Clean up build artifacts
@echo "$(COLOR_BOLD)Cleaning up build artifacts$(COLOR_RESET)"
@rm -f cover.*.profile cover.html
@rm -rf bin/
@rm -rf serviceradar-*_* release-artifacts/

.PHONY: build
build: ## Build all binaries
Expand Down Expand Up @@ -134,6 +135,45 @@ container-push: kodata-prep ## Build and push container images with ko
./cmd/checkers/dusk \
./cmd/checkers/snmp

# Build Debian packages
.PHONY: deb-agent
deb-agent: build-web ## Build the agent Debian package
@echo "$(COLOR_BOLD)Building agent Debian package$(COLOR_RESET)"
@./scripts/setup-deb-agent.sh

.PHONY: deb-poller
deb-poller: build-web ## Build the poller Debian package
@echo "$(COLOR_BOLD)Building poller Debian package$(COLOR_RESET)"
@./scripts/setup-deb-poller.sh

.PHONY: deb-cloud
deb-cloud: build-web ## Build the cloud Debian package (standard)
@echo "$(COLOR_BOLD)Building cloud Debian package$(COLOR_RESET)"
@VERSION=$(VERSION) ./scripts/setup-deb-cloud.sh

.PHONY: deb-cloud-container
deb-cloud-container: build-web ## Build the cloud Debian package with container support
@echo "$(COLOR_BOLD)Building cloud Debian package with container support$(COLOR_RESET)"
@VERSION=$(VERSION) BUILD_TAGS=containers ./scripts/setup-deb-cloud.sh

.PHONY: deb-dusk
deb-dusk: ## Build the Dusk checker Debian package
@echo "$(COLOR_BOLD)Building Dusk checker Debian package$(COLOR_RESET)"
@./scripts/setup-deb-dusk-checker.sh

.PHONY: deb-snmp
deb-snmp: ## Build the SNMP checker Debian package
@echo "$(COLOR_BOLD)Building SNMP checker Debian package$(COLOR_RESET)"
@./scripts/setup-deb-snmp-checker.sh

.PHONY: deb-all
deb-all: deb-agent deb-poller deb-cloud deb-dusk deb-snmp ## Build all Debian packages
@echo "$(COLOR_BOLD)All Debian packages built$(COLOR_RESET)"

.PHONY: deb-all-container
deb-all-container: deb-agent deb-poller deb-cloud-container deb-dusk deb-snmp ## Build all Debian packages with container support for cloud
@echo "$(COLOR_BOLD)All Debian packages built (with container support for cloud)$(COLOR_RESET)"

# Docusaurus commands
.PHONY: docs-start
docs-start: ## Start Docusaurus development server
Expand Down
43 changes: 21 additions & 22 deletions pkg/cloud/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
package api

import (
"embed"
"encoding/json"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/gorilla/mux"
"github.com/mfreeman451/serviceradar/pkg/checker/snmp"
"github.com/mfreeman451/serviceradar/pkg/cloud/api/web"
"github.com/mfreeman451/serviceradar/pkg/db"
srHttp "github.com/mfreeman451/serviceradar/pkg/http"
"github.com/mfreeman451/serviceradar/pkg/metrics"
Expand Down Expand Up @@ -101,6 +99,22 @@ func WithDB(db db.Service) func(server *APIServer) {
}
}

//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() {
// Add CORS middleware
s.router.Use(srHttp.CommonMiddleware)
Expand All @@ -123,24 +137,9 @@ func (s *APIServer) setupRoutes() {
// SNMP endpoints
s.router.HandleFunc("/api/nodes/{id}/snmp", s.getSNMPData).Methods("GET")

// Configure static file serving
staticFilesPath := web.GetStaticFilesPath()
fileServer := http.FileServer(http.Dir(staticFilesPath))

s.router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if file exists
path := filepath.Join(staticFilesPath, r.URL.Path)
_, err := os.Stat(path)

// If file doesn't exist or is a directory (and not root), serve index.html
if os.IsNotExist(err) || (r.URL.Path != "/" && strings.HasSuffix(r.URL.Path, "/")) {
http.ServeFile(w, r, filepath.Join(staticFilesPath, "index.html"))
return
}

// Otherwise serve the requested file
fileServer.ServeHTTP(w, r)
})
// 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
10 changes: 10 additions & 0 deletions pkg/cloud/api/static_serve_container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build containers
// +build containers

package api

// configureStaticServing sets up static file serving for container mode
func (s *APIServer) configureStaticServing() {
// Use Ko's approach for containers
s.setupContainerStaticFileServing()
}
10 changes: 10 additions & 0 deletions pkg/cloud/api/static_serve_non_container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !containers
// +build !containers

package api

// configureStaticServing sets up static file serving for non-container mode.
func (s *APIServer) configureStaticServing() {
// Use embedded FS for regular builds
s.setupStaticFileServing()
}
94 changes: 94 additions & 0 deletions scripts/setup-build-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/bash
# setup-build-env.sh - Set up the build environment for serviceradar

set -e

echo "Setting up ServiceRadar build environment..."

# Check for required tools
echo "Checking for required tools..."

# Check for Go
if ! command -v go &> /dev/null; then
echo "Error: Go is not installed or not in PATH"
echo "Please install Go from https://golang.org/dl/"
exit 1
fi

GO_VERSION=$(go version | awk '{print $3}')
echo "Found Go $GO_VERSION"

# Check for Node.js and pnpm for web UI building
if ! command -v node &> /dev/null; then
echo "Error: Node.js is not installed or not in PATH"
echo "Please install Node.js from https://nodejs.org/"
exit 1
fi

NODE_VERSION=$(node -v)
echo "Found Node.js $NODE_VERSION"

if ! command -v pnpm &> /dev/null; then
echo "Error: pnpm is not installed or not in PATH"
echo "Please install pnpm (usually installed w/ npm)"
exit 1
fi

PNPM_VERSION=$(pnpm -v)
echo "Found pnpm $PNPM_VERSION"

# Check for ko if building containers
if [[ "$1" == "containers" ]]; then
if ! command -v ko &> /dev/null; then
echo "Ko not found. Installing ko..."
go install github.com/google/ko@latest
else
KO_VERSION=$(ko version)
echo "Found ko $KO_VERSION"
fi
fi

# Set up web dependencies
echo "Setting up web UI dependencies..."
cd web
pnpm install
cd ..

# Create necessary directories
echo "Creating necessary directories..."
mkdir -p release-artifacts
mkdir -p bin
mkdir -p pkg/cloud/api/web

# Build web UI
echo "Building web UI..."
cd web
pnpm run build
cd ..

# Copy web UI to embedded location
echo "Setting up web assets for embedding..."
cp -r web/dist pkg/cloud/api/web/

if [[ "$1" == "containers" ]]; then
# Set up kodata directory
echo "Setting up kodata directory for container builds..."
mkdir -p cmd/cloud/.kodata
cp -r web/dist cmd/cloud/.kodata/web

echo "Build environment for containerized builds is ready!"
else
echo "Build environment for standard builds is ready!"
fi

echo "You can now run:"
if [[ "$1" == "containers" ]]; then
echo " - 'make container-build' to build container images"
echo " - 'make container-push' to build and push container images"
echo " - 'make deb-cloud-container' to build the cloud Debian package with container support"
echo " - 'make deb-all-container' to build all Debian packages with container support"
else
echo " - 'make build' to build all binaries"
echo " - 'make deb-cloud' to build the cloud Debian package"
echo " - 'make deb-all' to build all Debian packages"
fi
Loading

0 comments on commit 7ea340c

Please sign in to comment.