Files
goodie/pkg/server/static.go
Marc 8741282322 Eliminate Python dependency: embed frontend assets in odoo-go
- Copy all OWL frontend assets (JS/CSS/XML/fonts/images) into frontend/
  directory (2925 files, 43MB) — no more runtime reads from Python Odoo
- Replace OdooAddonsPath config with FrontendDir pointing to local frontend/
- Rewire bundle.go, static.go, templates.go, webclient.go to read from
  frontend/ instead of external Python Odoo addons directory
- Auto-detect frontend/ and build/ dirs relative to binary in main.go
- Delete obsolete Python helper scripts (tools/*.py)

The Go server is now fully self-contained: single binary + frontend/ folder.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-31 23:09:12 +02:00

107 lines
3.2 KiB
Go

package server
import (
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
// handleStatic serves static files from Odoo addon directories.
// URL pattern: /<addon_name>/static/<path>
// Maps to: <addons_path>/<addon_name>/static/<path>
func (s *Server) handleStatic(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Serve the compiled XML templates bundle from memory (generated at
// startup by compileXMLTemplates) instead of reading the pre-compiled
// file from the build directory. This replaces the Python build step.
if r.URL.Path == "/web/static/src/xml_templates_bundle.js" {
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=3600")
w.Write([]byte(s.xmlTemplateBundle))
return
}
path := strings.TrimPrefix(r.URL.Path, "/")
parts := strings.SplitN(path, "/", 3)
if len(parts) < 3 || parts[1] != "static" {
http.NotFound(w, r)
return
}
addonName := parts[0]
filePath := parts[2]
// Security: prevent directory traversal
if strings.Contains(filePath, "..") {
http.NotFound(w, r)
return
}
// For CSS files: check build dir first (compiled SCSS -> CSS)
if s.config.BuildDir != "" && strings.HasSuffix(filePath, ".css") {
buildPath := filepath.Join(s.config.BuildDir, addonName, "static", filePath)
if _, err := os.Stat(buildPath); err == nil {
w.Header().Set("Cache-Control", "public, max-age=3600")
http.ServeFile(w, r, buildPath)
return
}
}
// Search in frontend directory
if s.config.FrontendDir != "" {
fullPath := filepath.Join(s.config.FrontendDir, addonName, "static", filePath)
if _, err := os.Stat(fullPath); err == nil {
w.Header().Set("Cache-Control", "public, max-age=3600")
// Serve SCSS as compiled CSS if available
if strings.HasSuffix(fullPath, ".scss") && s.config.BuildDir != "" {
buildCSS := filepath.Join(s.config.BuildDir, addonName, "static", strings.TrimSuffix(filePath, ".scss")+".css")
if _, err := os.Stat(buildCSS); err == nil {
fullPath = buildCSS
}
}
// Transpile ES module JS files on-the-fly when served
// individually (e.g. debug mode). The main bundle already
// contains transpiled versions, but individual file
// requests still need transpilation.
if strings.HasSuffix(fullPath, ".js") {
data, err := os.ReadFile(fullPath)
if err != nil {
http.NotFound(w, r)
return
}
content := string(data)
urlPath := "/" + addonName + "/static/" + filePath
if IsOdooModule(urlPath, content) {
content = TranspileJS(urlPath, content)
}
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
http.ServeContent(w, r, filePath, time.Time{}, strings.NewReader(content))
return
}
http.ServeFile(w, r, fullPath)
return
}
}
// Fallback: build dir for pre-compiled vendor assets
if s.config.BuildDir != "" {
fullPath := filepath.Join(s.config.BuildDir, addonName, "static", filePath)
if _, err := os.Stat(fullPath); err == nil {
w.Header().Set("Cache-Control", "public, max-age=3600")
http.ServeFile(w, r, fullPath)
return
}
}
http.NotFound(w, r)
}