diff --git a/internal/onie/onie.go b/internal/onie/onie.go new file mode 100644 index 0000000..f46d38c --- /dev/null +++ b/internal/onie/onie.go @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +package onie + +import "net/http" + +// Register a handler which serves the given directory over HTTP. See the ONIE +// documentation for which file names are tried: +// https://opencomputeproject.github.io/onie/design-spec/discovery.html +func Register(mux *http.ServeMux, installerDir string) { + mux.Handle("GET /", http.FileServer(http.Dir(installerDir))) +} diff --git a/internal/ztp/ztp.go b/internal/ztp/ztp.go index 1cf0fd8..92d02a0 100644 --- a/internal/ztp/ztp.go +++ b/internal/ztp/ztp.go @@ -64,12 +64,12 @@ type SwitchParameters struct { ASNumber int `json:"as_number"` } -type Handler struct { +type handler struct { t *template.Template m map[netip.Addr]SwitchParameters } -func NewZTPHandler(c Config) *Handler { +func Register(mux *http.ServeMux, c Config) { t := template.New("ztp-scripts") t = t.Funcs(template.FuncMap{ "add": func(a, b int) int { return a + b }, @@ -79,10 +79,7 @@ func NewZTPHandler(c Config) *Handler { }) t = template.Must(t.ParseFS(templateFS, "templates/*.gotmpl")) - return &Handler{ - t: t, - m: c.SwitchParams, - } + mux.Handle("GET /ztp", &handler{t: t, m: c.SwitchParams}) } // interfacePrefix takes the interface ID and the /64 prefix of the switch and @@ -101,7 +98,7 @@ func interfacePrefix(prefix netip.Prefix, interfaceID int) (string, error) { return prefix.String(), nil } -func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ap, err := netip.ParseAddrPort(r.RemoteAddr) if err != nil { handleErr(w, err)