diff --git a/.gitignore b/.gitignore index cde0123..9fdf7ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist/ +test_data/ diff --git a/handlers/form_handler.go b/handlers/form_handler.go index 20e0f05..f809eeb 100644 --- a/handlers/form_handler.go +++ b/handlers/form_handler.go @@ -13,7 +13,7 @@ import ( // and a filename string as parameters. // The function parses the template files from the content FS, // executes the template with the provided data, and renders it as a response. -func FormHandler(content embed.FS, monsters *[]model.Monster, filename string) http.HandlerFunc { +func FormHandler(content embed.FS, monsters *[]model.Monster) http.HandlerFunc { log.Print("FormHandler called") // Lock the mutex to ensure exclusive access to the monsters slice. diff --git a/main.go b/main.go index d52656d..546583c 100644 --- a/main.go +++ b/main.go @@ -24,27 +24,29 @@ var ( func main() { filename := "" + // Create a new ServeMux instance + routes := http.NewServeMux() + + // Register the handlers for different routes + routes.HandleFunc("/", handlers.FormHandler(content, &Monsters)) + routes.HandleFunc("/submit", handlers.SubmitHandler(content, &chars, &Monsters, filename)) + routes.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.FS(content)))) + routes.HandleFunc("/addMonster", handlers.AddMonster(&Monsters)) + routes.HandleFunc("/main", handlers.MainHandler(content, &Monsters)) + routes.HandleFunc("/about", handlers.AboutHandler(content)) + routes.HandleFunc("/contact", handlers.ContactHandler(content)) + routes.HandleFunc("/monsterTable", handlers.MonsterTableHandler(content, &Monsters)) // Print the message indicating that 'static' has been included. log.Printf("Eingebunden is %v\n", static) - // Set up the HTTP handlers for different routes. - http.HandleFunc("/", handlers.FormHandler(content, &Monsters, filename)) - http.HandleFunc("/submit", handlers.SubmitHandler(content, &chars, &Monsters, filename)) - http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.FS(content)))) - http.HandleFunc("/addMonster", handlers.AddMonster(&Monsters)) - http.HandleFunc("/main", handlers.MainHandler(content, &Monsters)) - http.HandleFunc("/about", handlers.AboutHandler(content)) - http.HandleFunc("/contact", handlers.ContactHandler(content)) - http.HandleFunc("/monsterTable", handlers.MonsterTableHandler(content, &Monsters)) - // Load the CSS file. css, err := loadCSS(static) if err != nil { log.Fatal(err) } - // Add a route for the CSS file. - http.HandleFunc("/static/darkly_bulmawatch.css", func(w http.ResponseWriter, r *http.Request) { + // Add a route for the CSS file + routes.HandleFunc("/static/darkly_bulmawatch.css", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/css") w.Write([]byte(css)) }) @@ -53,7 +55,7 @@ func main() { log.Print("Server gestartet, erreichbar unter http://localhost:8080") // Start the server and listen for incoming requests on port 8080. - log.Fatal(http.ListenAndServe(":8080", nil)) + log.Fatal(http.ListenAndServe(":8080", routes)) } // loadCSS reads the CSS file from the embedded filesystem.