package main import ( "html/template" "log" "net/http" _ "github.com/go-sql-driver/mysql" "github.com/gorilla/mux" "go_selva/internal/api" "go_selva/internal/db" ) func main() { // Initialize database connection database, err := db.InitDB() if err != nil { log.Fatalf("Failed to initialize database: %v", err) } defer database.Close() // Initialize Gorilla Mux router router := mux.NewRouter() // Create a template registry and register the safeHTML function funcMap := template.FuncMap{ "safeHTML": func(s string) template.HTML { return template.HTML(s) }, } tmpl := template.New("").Funcs(funcMap) // Load HTML templates tmpl, err = tmpl.ParseGlob("templates/*") if err != nil { log.Fatalf("Failed to load HTML templates: %v", err) } // Serve static files (CSS) router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) // Initialize API handlers apiHandler := api.NewAPIHandler(database, tmpl) // Pass the loaded templates // Define API routes apiGroup := router.PathPrefix("/nombres").Subrouter() apiGroup.HandleFunc("/search", apiHandler.SearchNombres).Methods("GET") apiGroup.HandleFunc("", apiHandler.CreateNombre).Methods("POST") apiGroup.HandleFunc("", apiHandler.GetNombres).Methods("GET") apiGroup.HandleFunc("/{id}", apiHandler.GetNombreByID).Methods("GET") apiGroup.HandleFunc("/{id}", apiHandler.UpdateNombre).Methods("PUT") apiGroup.HandleFunc("/{id}", apiHandler.DeleteNombre).Methods("DELETE") apiGroup.HandleFunc("/html/edit/{id}", apiHandler.EditNombreHTML).Methods("GET") apiGroup.HandleFunc("/html/update/{id}", apiHandler.UpdateNombreHTML).Methods("POST") // HTML Routes router.HandleFunc("/", apiHandler.GetIndexPlantsHTML).Methods("GET") router.HandleFunc("/nombres/html", apiHandler.GetNombresHTML).Methods("GET") router.HandleFunc("/nombres/html/{id}", apiHandler.GetNombreByIDHTML).Methods("GET") // Start the server log.Println("Server started on :8080") if err := http.ListenAndServe(":8080", router); err != nil { log.Fatalf("Failed to start server: %v", err) } }