Embed static assets into binary

This commit is contained in:
nemunaire 2025-07-17 16:04:48 +02:00
parent a044bb759e
commit 5f1d7adaa4

12
main.go
View file

@ -2,8 +2,10 @@ package main
import (
"bufio"
"embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"os"
@ -19,6 +21,9 @@ import (
"github.com/gorilla/websocket"
)
//go:embed all:static
var _assets embed.FS
// Structures de données
type WiFiNetwork struct {
SSID string `json:"ssid"`
@ -132,7 +137,12 @@ func main() {
r.HandleFunc("/ws/logs", websocketHandler)
// Servir les fichiers statiques
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
sub, err := fs.Sub(_assets, "static")
if err != nil {
log.Fatal("Unable to cd to static/ directory:", err)
}
Assets := http.FS(sub)
r.PathPrefix("/").Handler(http.FileServer(Assets))
addLog("Système", "Serveur API démarré sur le port 8080")
log.Fatal(http.ListenAndServe(":8080", r))