ddServer/main.go
Patryk Hegenberg 206b5bb506 add everything necessary to be able to calculate skills interactive.
added new route to main to handle skill field update
added new handler to work with htmx requests for skill calculation
made skill fields readonly
adapted templates for reloading of skills only
2023-12-12 20:56:55 +01:00

73 lines
2.2 KiB
Go

package main
import (
"ddServer/handlers"
"ddServer/model"
"embed"
"log"
"net/http"
"sync"
)
var (
mu sync.Mutex
chars []model.Character
//go:embed templates/*.html
//go:embed images/*
content embed.FS
//go:embed static/*
static embed.FS
Monsters []model.Monster
)
// main is the entry point of the program.
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))
routes.HandleFunc("/calculate-skills", handlers.SkillCalculationHandler(content))
// Print the message indicating that 'static' has been included.
log.Printf("Eingebunden is %v\n", static)
// Load the CSS file.
css, err := loadCSS(static)
if err != nil {
log.Fatal(err)
}
// 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))
})
// Print the message indicating that the server has started.
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", routes))
}
// loadCSS reads the CSS file from the embedded filesystem.
// It takes the content embed.FS as input.
// It returns the content of the CSS file as a string and an error if any.
func loadCSS(content embed.FS) (string, error) {
// Read the CSS file "static/darkly_bulmawatch.css" from the embedded filesystem
file, err := content.ReadFile("static/darkly_bulmawatch.css")
if err != nil {
return "", err
}
// Convert the file content to a string and return
return string(file), nil
}