add embed package and set it up

This commit is contained in:
ghostersk
2026-03-08 06:26:49 +00:00
parent 75904809dc
commit 97e07689a9
2 changed files with 24 additions and 2 deletions
+16 -2
View File
@@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"io/fs"
"log" "log"
"net/http" "net/http"
"os" "os"
@@ -10,6 +11,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/ghostersk/gowebmail"
"github.com/ghostersk/gowebmail/config" "github.com/ghostersk/gowebmail/config"
"github.com/ghostersk/gowebmail/internal/db" "github.com/ghostersk/gowebmail/internal/db"
"github.com/ghostersk/gowebmail/internal/handlers" "github.com/ghostersk/gowebmail/internal/handlers"
@@ -52,6 +54,10 @@ func main() {
} }
// ── Normal server startup ────────────────────────────────────────────── // ── Normal server startup ──────────────────────────────────────────────
staticFS, err := fs.Sub(gowebmail.WebFS, "web/static")
if err != nil {
log.Fatalf("embed static fs: %v", err)
}
cfg, err := config.Load() cfg, err := config.Load()
if err != nil { if err != nil {
log.Fatalf("config load: %v", err) log.Fatalf("config load: %v", err)
@@ -81,10 +87,18 @@ func main() {
// Static files // Static files
r.PathPrefix("/static/").Handler( r.PathPrefix("/static/").Handler(
http.StripPrefix("/static/", http.FileServer(http.Dir("./web/static/"))), http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))),
) )
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./web/static/img/favicon.png") data, err := gowebmail.WebFS.ReadFile("web/static/img/favicon.png")
if err != nil {
log.Printf("favicon error: %v", err)
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "image/png")
w.Write(data)
}) })
// Public auth routes // Public auth routes
auth := r.PathPrefix("/auth").Subrouter() auth := r.PathPrefix("/auth").Subrouter()
+8
View File
@@ -0,0 +1,8 @@
package gowebmail
import "embed"
// Global access to the web assets
//
//go:embed web/static/** web/templates/**
var WebFS embed.FS