change route handling to ServeMux for better performance and readability
This commit is contained in:
parent
3a77eb1593
commit
2d0d0697fb
3 changed files with 17 additions and 14 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
||||||
|
|
||||||
dist/
|
dist/
|
||||||
|
test_data/
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ import (
|
||||||
// and a filename string as parameters.
|
// and a filename string as parameters.
|
||||||
// The function parses the template files from the content FS,
|
// The function parses the template files from the content FS,
|
||||||
// executes the template with the provided data, and renders it as a response.
|
// 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")
|
log.Print("FormHandler called")
|
||||||
|
|
||||||
// Lock the mutex to ensure exclusive access to the monsters slice.
|
// Lock the mutex to ensure exclusive access to the monsters slice.
|
||||||
|
|
|
||||||
28
main.go
28
main.go
|
|
@ -24,27 +24,29 @@ var (
|
||||||
func main() {
|
func main() {
|
||||||
filename := ""
|
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.
|
// Print the message indicating that 'static' has been included.
|
||||||
log.Printf("Eingebunden is %v\n", static)
|
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.
|
// Load the CSS file.
|
||||||
css, err := loadCSS(static)
|
css, err := loadCSS(static)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a route for the CSS file.
|
// Add a route for the CSS file
|
||||||
http.HandleFunc("/static/darkly_bulmawatch.css", func(w http.ResponseWriter, r *http.Request) {
|
routes.HandleFunc("/static/darkly_bulmawatch.css", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/css")
|
w.Header().Set("Content-Type", "text/css")
|
||||||
w.Write([]byte(css))
|
w.Write([]byte(css))
|
||||||
})
|
})
|
||||||
|
|
@ -53,7 +55,7 @@ func main() {
|
||||||
log.Print("Server gestartet, erreichbar unter http://localhost:8080")
|
log.Print("Server gestartet, erreichbar unter http://localhost:8080")
|
||||||
|
|
||||||
// Start the server and listen for incoming requests on port 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.
|
// loadCSS reads the CSS file from the embedded filesystem.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue