Browse Source

embed web/static into binary via go:embed

Static files are now compiled into the binary using //go:embed.
- Add web/embed.go with //go:embed static directive
- Update server.New() to accept fs.FS parameter
- Switch file server from http.Dir to http.FS

Deployment is now: roadamp.exe + config.yaml + winamp/

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
master
Jan Svabenik 1 month ago
parent
commit
fb3e7e2224
2 changed files with 17 additions and 8 deletions
  1. +10
    -8
      internal/server/server.go
  2. +7
    -0
      web/embed.go

+ 10
- 8
internal/server/server.go View File

@@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"os"
@@ -53,14 +54,15 @@ func loadConfig(path string) (Config, error) {
// ── Server ────────────────────────────────────────────────────────────────────

type Server struct {
cfg Config
wa *winamp.Controller
kl *killist.KillList
hub *hub
mux *http.ServeMux
cfg Config
wa *winamp.Controller
kl *killist.KillList
hub *hub
mux *http.ServeMux
staticFS fs.FS
}

func New(configPath string) (*Server, error) {
func New(configPath string, staticFS fs.FS) (*Server, error) {
cfg, err := loadConfig(configPath)
if err != nil {
return nil, fmt.Errorf("config: %w", err)
@@ -69,7 +71,7 @@ func New(configPath string) (*Server, error) {
if err != nil {
return nil, fmt.Errorf("killist: %w", err)
}
s := &Server{cfg: cfg, wa: winamp.New(), kl: kl, mux: http.NewServeMux()}
s := &Server{cfg: cfg, wa: winamp.New(), kl: kl, mux: http.NewServeMux(), staticFS: staticFS}
s.hub = newHub(s.handleCommand)
s.routes()
return s, nil
@@ -90,7 +92,7 @@ func (s *Server) Run() error {
// ── Routes ────────────────────────────────────────────────────────────────────

func (s *Server) routes() {
s.mux.Handle("/", http.FileServer(http.Dir("web/static")))
s.mux.Handle("/", http.FileServer(http.FS(s.staticFS)))

// WebSocket (primary interface)
s.mux.HandleFunc("/ws", s.handleWS)


+ 7
- 0
web/embed.go View File

@@ -0,0 +1,7 @@
// Package web embeds the frontend static files into the binary.
package web

import "embed"

//go:embed static
var FS embed.FS

Loading…
Cancel
Save