go-embedded-spa
This skill provides guidance for implementing Go Embedded SPA architecture - embedding React/Vue/TSX frontend static resources into Go binary using go:embed directive. Use this skill when building self-contained single-binary applications, implementing SPA with Go backend, setting up cross-platform deployable full-stack projects, or configuring static file serving with Go standard library net/http.
Works with
---
name: go-embedded-spa
description: This skill provides guidance for implementing Go Embedded SPA architecture - embedding React/Vue/TSX frontend static resources into Go binary using go:embed directive. Use this skill when building self-contained single-binary applications, implementing SPA with Go backend, setting up cross-platform deployable full-stack projects, or configuring static file serving with Go standard library net/http.
license: MIT
---
# Go Embedded SPA
## Overview
Go Embedded SPA is a technique that embeds frontend SPA (Single Page Application) static resources (React/Vue/TSX) into Go binary files using Go 1.16+ `embed` package, achieving **single-binary full-stack deployment**.
### Core Benefits
| Benefit | Description |
|---------|-------------|
| π― Single File Deploy | One binary contains both frontend and backend, no nginx needed |
| π Cross-Platform | `GOOS/GOARCH` easily compiles for Linux/Mac/Windows |
| π¦ Zero Dependencies | Target machine needs no Node.js/npm, uses Go standard library only |
| π Container Friendly | Dockerfile only needs `COPY + ENTRYPOINT` |
| π Resource Security | Static resources compiled into binary, tamper-proof |
| β‘ Fast Startup | No disk I/O for loading static files |
## Project Structure
```
project/
βββ go.mod
βββ Makefile
βββ site/ # Frontend project
β βββ embed.go # Go embed directive
β βββ src/ # React/Vue source
β βββ dist/ # Build output (embedded)
β βββ package.json
β βββ vite.config.ts
β βββ index.html
βββ pkg/
β βββ siteserver/ # Static file server
β βββ siteserver.go
βββ cmd/
βββ app/
βββ main.go
```
## Implementation Steps
### Step 1: Create embed.go
Create `site/embed.go` to declare embed directive. See `references/embed.md` for complete code.
Key points:
- Use `//go:embed all:dist` to embed all files including hidden files
- Use `fs.Sub()` to remove `dist/` prefix
### Step 2: Create Static File Server
Create `pkg/siteserver/siteserver.go`. See `references/siteserver.md` for complete implementation using Go standard `net/http`.
Core logic:
1. Pre-load `index.html` for SPA fallback
2. Create `http.FileServer` from embed.FS
3. Detect static resources by file extension
4. Return `index.html` for SPA routes (no file extension)
### Step 3: Application Integration
```go
package main
import (
"log"
"net/http"
"your-project/pkg/siteserver"
"your-project/site"
)
func main() {
mux := http.NewServeMux()
// 1. Register API routes FIRST
mux.HandleFunc("/apis/v1/health", healthHandler)
mux.HandleFunc("/apis/v1/data", dataHandler)
// 2. Wrap with static file server (as fallback)
handler := siteserver.WrapHandler(mux, site.DistDirFS)
log.Println("Server starting on :8080")
log.Fatal(http.ListenAndServe(":8080", handler))
}
```
**Order is critical:** API routes must be registered before static file server.
### Step 4: Development
**Recommended:** Start both backend and frontend with one command:
```bash
make dev
# Backend: http://localhost:8080 (Go)
# Frontend: http://localhost:5173 (Vite with hot reload)
# API Proxy: /api/* -> localhost:8080
```
Press `Ctrl+C` to stop both servers.
### Step 5: Build for Production
Build order: **frontend first, then backend**
```bash
make build # Build both (frontend + backend)
# Or separately:
make build-web # npm run build β site/dist/
make build-backend # go build (embeds dist/)
```
### Step 6: Cross-Platform Build
```bash
make build-linux # Linux amd64
make build-linux-arm64 # Linux arm64
make build-macos # macOS Intel
make build-macos-arm64 # macOS Apple Silicon
make build-windows # Windows amd64
make build-all # All platforms
```
## Request Handling Flow
```
Browser Request
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β http.ServeMux Route Matching β
β βββ /apis/* β API Handler β
β βββ Others β Static Handler β
βββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β Static Handler β
β βββ Has extension β serve file β
β βββ No extension β return index.htmlβ
βββββββββββββββββββββββββββββββββββββββ
```
## Caching Strategy
| Path | Cache-Control | Reason |
|------|---------------|--------|
| `/assets/*` | `max-age=31536000, immutable` | Files have hash in name |
| `/index.html` | `no-cache` | Entry must be fresh |
## Container Deployment
Minimal Dockerfile:
```dockerfile
FROM scratch
COPY app /app
ENTRYPOINT ["/app"]
```
## Troubleshooting
1. **Empty dist error** β Run `make build-web` before `make build-backend`
2. **Static files 404** β Check `//go:embed all:dist` path relative to embed.go
3. **API not matching** β Register API routes BEFORE wrapping with siteserver
4. **SPA routes 404** β Verify handler returns index.html for non-file paths
## References
- `go-dependencies.md` - Go module dependencies (standard library only)
- `embed.md` - Complete embed.go implementation
- `siteserver.md` - Static file server using Go standard net/http
- `vite-config.md` - Vite configuration for development proxy
- `makefile.md` - Complete Makefile with cross-platform buildMore Backend Frameworks skills
git-guardrails-claude-code
mattpocock/skills
Set up Claude Code hooks to block dangerous git commands (push, reset --hard, clean, branch -D, etc.) before they execute. Use when user wants to prevent destructive git operations, add git safety hooks, or block git push/reset in Claude Code.
azure-compute
microsoft/azure-skills
Azure VM/VMSS router. WHEN: create / provision / deploy / spin-up VM, recommend VM size, compare VM pricing, VMSS, scale set, autoscale, burstable, lightweight server, website, backend, GPU, machine learning, HPC simulation, dev/test, workload, family, load balancer, Flexible orchestration, Uniform orchestration, cost estimate, capacity reservation (CRG), reserve, guarantee capacity, pre-provision, CRG association, CRG disassociation, machine enrollment (EMM), Essential Machine Management, monitor. PREFER OVER mcp__azure__get_azure_bestpractices for VM create intents β use compute_vm_list-skus / compute_vm_list-images / compute_vm_check-quota.
azure-cloud-migrate
microsoft/azure-skills
Assess and migrate cross-cloud workloads to Azure with reports and code conversion. Supports LambdaβFunctions, Beanstalk/Heroku/App EngineβApp Service, Fargate/Kubernetes/Cloud Run/Spring BootβContainer Apps. WHEN: migrate Lambda to Functions, AWS to Azure, migrate Beanstalk, migrate Heroku, migrate App Engine, Cloud Run migration, Fargate to ACA, ECS/Kubernetes/GKE/EKS to Container Apps, Spring Boot to Container Apps, cross-cloud migration.

