hscontrol: consolidate assets into single package

Move favicon.png, style.css, and headscale.svg to hscontrol/assets/
and create a single assets.go file with all embed directives.

Update hscontrol/handlers.go and hscontrol/templates/general.go to
use the centralized assets package.
This commit is contained in:
Kristoffer Dalby
2025-11-12 14:23:15 +01:00
committed by Kristoffer Dalby
parent 09c9762fe0
commit e3ced80278
5 changed files with 38 additions and 22 deletions

View File

@@ -0,0 +1,24 @@
// Package assets provides embedded static assets for Headscale.
// All static files (favicon, CSS, SVG) are embedded here for
// centralized asset management.
package assets
import (
_ "embed"
)
// Favicon is the embedded favicon.png file served at /favicon.ico
//
//go:embed favicon.png
var Favicon []byte
// CSS is the embedded style.css stylesheet used in HTML templates.
// Contains Material for MkDocs design system styles.
//
//go:embed style.css
var CSS string
// SVG is the embedded headscale.svg logo used in HTML templates.
//
//go:embed headscale.svg
var SVG string

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

@@ -9,10 +9,12 @@
--md-primary-fg-color: #4051b5; --md-primary-fg-color: #4051b5;
--md-accent-fg-color: #526cfe; --md-accent-fg-color: #526cfe;
--md-typeset-a-color: var(--md-primary-fg-color); --md-typeset-a-color: var(--md-primary-fg-color);
--md-text-font: "Roboto", -apple-system, BlinkMacSystemFont, "Segoe UI", --md-text-font:
"Helvetica Neue", Arial, sans-serif; "Roboto", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue",
--md-code-font: "Roboto Mono", "SF Mono", Monaco, "Cascadia Code", Consolas, Arial, sans-serif;
"Courier New", monospace; --md-code-font:
"Roboto Mono", "SF Mono", Monaco, "Cascadia Code", Consolas, "Courier New",
monospace;
} }
/* Base Typography */ /* Base Typography */

View File

@@ -2,7 +2,6 @@ package hscontrol
import ( import (
"bytes" "bytes"
_ "embed"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -14,6 +13,7 @@ import (
"github.com/chasefleming/elem-go/styles" "github.com/chasefleming/elem-go/styles"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/juanfont/headscale/hscontrol/assets"
"github.com/juanfont/headscale/hscontrol/templates" "github.com/juanfont/headscale/hscontrol/templates"
"github.com/juanfont/headscale/hscontrol/types" "github.com/juanfont/headscale/hscontrol/types"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@@ -278,12 +278,9 @@ func (a *AuthProviderWeb) RegisterHandler(
writer.Write([]byte(templates.RegisterWeb(registrationId).Render())) writer.Write([]byte(templates.RegisterWeb(registrationId).Render()))
} }
//go:embed assets/favicon.png
var favicon []byte
func FaviconHandler(writer http.ResponseWriter, req *http.Request) { func FaviconHandler(writer http.ResponseWriter, req *http.Request) {
writer.Header().Set("Content-Type", "image/png") writer.Header().Set("Content-Type", "image/png")
http.ServeContent(writer, req, "favicon.ico", time.Unix(0, 0), bytes.NewReader(favicon)) http.ServeContent(writer, req, "favicon.ico", time.Unix(0, 0), bytes.NewReader(assets.Favicon))
} }
// BlankHandler returns a blank page with favicon linked. // BlankHandler returns a blank page with favicon linked.

View File

@@ -1,22 +1,15 @@
package templates package templates
import ( import (
_ "embed"
"github.com/chasefleming/elem-go" "github.com/chasefleming/elem-go"
"github.com/chasefleming/elem-go/attrs" "github.com/chasefleming/elem-go/attrs"
"github.com/chasefleming/elem-go/styles" "github.com/chasefleming/elem-go/styles"
"github.com/juanfont/headscale/hscontrol/assets"
) )
//go:embed style.css
var headscaleCSS string
//go:embed headscale.svg
var headscaleSVG string
// mdTypesetBody creates a body element with md-typeset styling // mdTypesetBody creates a body element with md-typeset styling
// that matches the official Headscale documentation design. // that matches the official Headscale documentation design.
// Uses CSS classes with styles defined in headscaleCSS. // Uses CSS classes with styles defined in assets.CSS.
func mdTypesetBody(children ...elem.Node) *elem.Element { func mdTypesetBody(children ...elem.Node) *elem.Element {
return elem.Body(attrs.Props{ return elem.Body(attrs.Props{
attrs.Style: styles.Props{ attrs.Style: styles.Props{
@@ -41,7 +34,7 @@ func mdTypesetBody(children ...elem.Node) *elem.Element {
// Styled Element Wrappers // Styled Element Wrappers
// These functions wrap elem-go elements using CSS classes. // These functions wrap elem-go elements using CSS classes.
// Styling is handled by the CSS in headscaleCSS. // Styling is handled by the CSS in assets.CSS.
// H1 creates a H1 element styled by .md-typeset h1 // H1 creates a H1 element styled by .md-typeset h1
func H1(children ...elem.Node) *elem.Element { func H1(children ...elem.Node) *elem.Element {
@@ -126,7 +119,7 @@ func contentContainer(children ...elem.Node) *elem.Element {
// The logo is styled by the .headscale-logo CSS class. // The logo is styled by the .headscale-logo CSS class.
func headscaleLogo() elem.Node { func headscaleLogo() elem.Node {
// Return the embedded SVG as-is // Return the embedded SVG as-is
return elem.Raw(headscaleSVG) return elem.Raw(assets.SVG)
} }
// pageFooter creates a consistent footer for all pages. // pageFooter creates a consistent footer for all pages.
@@ -198,7 +191,7 @@ func HtmlStructure(head, body *elem.Element) *elem.Element {
attrs.Href: "https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&family=Roboto+Mono:wght@400;700&display=swap", attrs.Href: "https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&family=Roboto+Mono:wght@400;700&display=swap",
}), }),
// Material for MkDocs CSS styles // Material for MkDocs CSS styles
elem.Style(attrs.Props{attrs.Type: "text/css"}, elem.Raw(headscaleCSS)), elem.Style(attrs.Props{attrs.Type: "text/css"}, elem.Raw(assets.CSS)),
head, head,
), ),
body, body,