package frontend import ( "html/template" "net/http" "os" "path/filepath" "strings" "github.com/tdewolff/minify/v2" "github.com/tdewolff/minify/v2/css" "github.com/tdewolff/minify/v2/js" ) var dashboard = template.Must(template.ParseFiles( "frontend/htmx/contents/dash/base.html", "frontend/htmx/contents/dash/dashboard.html")) var inventory = template.Must(template.ParseFiles( "frontend/htmx/contents/dash/base.html", "frontend/htmx/contents/dash/inventory.html")) var item = template.Must(template.ParseFiles( "frontend/htmx/contents/dash/base.html", "frontend/htmx/contents/dash/itemlist.html")) var locations = template.Must(template.ParseFiles( "frontend/htmx/contents/dash/base.html", "frontend/htmx/contents/dash/locations.html")) var projects = template.Must(template.ParseFiles( "frontend/htmx/contents/dash/base.html", "frontend/htmx/contents/dash/projects.html")) var home = template.Must(template.ParseFiles("frontend/htmx/home.html")) func Home(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.ServeFile(w, r, "frontend/htmx/404.html") return } w.Header().Set("Content-Type", "text/html") err := home.Execute(w, struct { Name string }{ Name: "Home", }) if err != nil { return } } func Dashboard(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") err := dashboard.ExecuteTemplate(w, "base.html", struct { Title string Stats struct { Items int Projects int Locations int } }{ Title: "Dashboard", Stats: struct { Items int Projects int Locations int }{ Items: 1, Projects: 1, Locations: 3, }, }) if err != nil { return } } func Inventory(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") err := inventory.ExecuteTemplate(w, "base.html", struct { Title string }{ Title: "Inventory", }) if err != nil { return } } func Items(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") err := item.ExecuteTemplate(w, "base.html", struct { Title string }{ Title: "Items", }) if err != nil { return } } func Locations(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") err := locations.ExecuteTemplate(w, "base.html", struct { Title string }{ Title: "Locations", }) if err != nil { return } } func Projects(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") err := projects.ExecuteTemplate(w, "base.html", struct { Title string }{ Title: "Projects", }) if err != nil { return } } var minifier *minify.M func init() { minifier = minify.New() // Füge die Minifier für CSS und JS hinzu minifier.AddFunc("text/css", css.Minify) minifier.AddFunc("text/javascript", js.Minify) } func Assets(w http.ResponseWriter, r *http.Request) { path := strings.TrimPrefix(r.URL.Path, "/assets/") fullPath := filepath.Join("frontend/assets", path) w.Header().Set("Cache-Control", "public, max-age=3600") var mimeType string if strings.HasSuffix(path, ".min.js") { mimeType = "text/javascript" fullPath = strings.Replace(fullPath, ".min.js", ".js", 1) } else if strings.HasSuffix(path, ".min.css") { mimeType = "text/css" fullPath = strings.Replace(fullPath, ".min.css", ".css", 1) } info, err := os.Stat(fullPath) if err != nil || info.IsDir() { http.Error(w, "Asset not found", http.StatusNotFound) return } if mimeType != "" { content, err := os.ReadFile(fullPath) if err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) return } minifiedContent, err := minifier.Bytes(mimeType, content) if err == nil { w.Header().Set("Content-Type", mimeType) w.Write(minifiedContent) return } } http.ServeFile(w, r, fullPath) }