cmc-sales/go-app/cmd/server/main.go

291 lines
14 KiB
Go
Raw Normal View History

2025-06-24 03:32:28 -07:00
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"code.springupsoftware.com/cmc/cmc-sales/internal/cmc/db"
"code.springupsoftware.com/cmc/cmc-sales/internal/cmc/handlers"
"code.springupsoftware.com/cmc/cmc-sales/internal/cmc/templates"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
)
func main() {
// Load environment variables
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
// Database configuration
dbHost := getEnv("DB_HOST", "localhost")
dbPort := getEnv("DB_PORT", "3306")
dbUser := getEnv("DB_USER", "cmc")
dbPass := getEnv("DB_PASSWORD", "xVRQI&cA?7AU=hqJ!%au")
dbName := getEnv("DB_NAME", "cmc")
// Connect to database
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", dbUser, dbPass, dbHost, dbPort, dbName)
database, err := sql.Open("mysql", dsn)
if err != nil {
log.Fatal("Failed to connect to database:", err)
}
defer database.Close()
// Test database connection
if err := database.Ping(); err != nil {
log.Fatal("Failed to ping database:", err)
}
log.Println("Connected to database successfully")
// Create queries instance
queries := db.New(database)
// Initialize template manager
tmpl, err := templates.NewTemplateManager("templates")
if err != nil {
log.Fatal("Failed to initialize templates:", err)
}
// Create handlers
customerHandler := handlers.NewCustomerHandler(queries)
productHandler := handlers.NewProductHandler(queries)
purchaseOrderHandler := handlers.NewPurchaseOrderHandler(queries)
enquiryHandler := handlers.NewEnquiryHandler(queries)
2025-07-02 05:04:36 -07:00
documentHandler := handlers.NewDocumentHandler(queries)
2025-06-24 03:32:28 -07:00
pageHandler := handlers.NewPageHandler(queries, tmpl)
2025-07-02 05:04:36 -07:00
addressHandler := handlers.NewAddressHandler(queries)
attachmentHandler := handlers.NewAttachmentHandler(queries)
countryHandler := handlers.NewCountryHandler(queries)
statusHandler := handlers.NewStatusHandler(queries)
lineItemHandler := handlers.NewLineItemHandler(queries)
2025-06-24 03:32:28 -07:00
// Setup routes
r := mux.NewRouter()
2025-07-02 05:04:36 -07:00
2025-06-24 03:32:28 -07:00
// Static files
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
2025-07-02 05:04:36 -07:00
// PDF files (matching CakePHP structure)
r.PathPrefix("/pdf/").Handler(http.StripPrefix("/pdf/", http.FileServer(http.Dir("webroot/pdf"))))
2025-06-24 03:32:28 -07:00
// API routes
api := r.PathPrefix("/api/v1").Subrouter()
// Customer routes
api.HandleFunc("/customers", customerHandler.List).Methods("GET")
api.HandleFunc("/customers", customerHandler.Create).Methods("POST")
api.HandleFunc("/customers/{id}", customerHandler.Get).Methods("GET")
api.HandleFunc("/customers/{id}", customerHandler.Update).Methods("PUT")
api.HandleFunc("/customers/{id}", customerHandler.Delete).Methods("DELETE")
api.HandleFunc("/customers/search", customerHandler.Search).Methods("GET")
// Product routes
api.HandleFunc("/products", productHandler.List).Methods("GET")
api.HandleFunc("/products", productHandler.Create).Methods("POST")
api.HandleFunc("/products/{id}", productHandler.Get).Methods("GET")
api.HandleFunc("/products/{id}", productHandler.Update).Methods("PUT")
api.HandleFunc("/products/{id}", productHandler.Delete).Methods("DELETE")
api.HandleFunc("/products/search", productHandler.Search).Methods("GET")
// Purchase Order routes
api.HandleFunc("/purchase-orders", purchaseOrderHandler.List).Methods("GET")
api.HandleFunc("/purchase-orders", purchaseOrderHandler.Create).Methods("POST")
api.HandleFunc("/purchase-orders/{id}", purchaseOrderHandler.Get).Methods("GET")
api.HandleFunc("/purchase-orders/{id}", purchaseOrderHandler.Update).Methods("PUT")
api.HandleFunc("/purchase-orders/{id}", purchaseOrderHandler.Delete).Methods("DELETE")
api.HandleFunc("/purchase-orders/search", purchaseOrderHandler.Search).Methods("GET")
// Enquiry routes
api.HandleFunc("/enquiries", enquiryHandler.List).Methods("GET")
api.HandleFunc("/enquiries", enquiryHandler.Create).Methods("POST")
api.HandleFunc("/enquiries/{id}", enquiryHandler.Get).Methods("GET")
api.HandleFunc("/enquiries/{id}", enquiryHandler.Update).Methods("PUT")
api.HandleFunc("/enquiries/{id}", enquiryHandler.Delete).Methods("DELETE")
api.HandleFunc("/enquiries/{id}/undelete", enquiryHandler.Undelete).Methods("PUT")
api.HandleFunc("/enquiries/{id}/status", enquiryHandler.UpdateStatus).Methods("PUT")
api.HandleFunc("/enquiries/{id}/mark-submitted", enquiryHandler.MarkSubmitted).Methods("PUT")
api.HandleFunc("/enquiries/search", enquiryHandler.Search).Methods("GET")
2025-07-02 05:04:36 -07:00
// Document routes
api.HandleFunc("/documents", documentHandler.List).Methods("GET")
api.HandleFunc("/documents", documentHandler.Create).Methods("POST")
api.HandleFunc("/documents/{id}", documentHandler.Get).Methods("GET")
api.HandleFunc("/documents/{id}", documentHandler.Update).Methods("PUT")
api.HandleFunc("/documents/{id}/archive", documentHandler.Archive).Methods("PUT")
api.HandleFunc("/documents/{id}/unarchive", documentHandler.Unarchive).Methods("PUT")
api.HandleFunc("/documents/search", documentHandler.Search).Methods("GET")
// Address routes
api.HandleFunc("/addresses", addressHandler.List).Methods("GET")
api.HandleFunc("/addresses", addressHandler.Create).Methods("POST")
api.HandleFunc("/addresses/{id}", addressHandler.Get).Methods("GET")
api.HandleFunc("/addresses/{id}", addressHandler.Update).Methods("PUT")
api.HandleFunc("/addresses/{id}", addressHandler.Delete).Methods("DELETE")
api.HandleFunc("/addresses/customer/{customerID}", addressHandler.CustomerAddresses).Methods("GET")
// Attachment routes
api.HandleFunc("/attachments", attachmentHandler.List).Methods("GET")
api.HandleFunc("/attachments/archived", attachmentHandler.Archived).Methods("GET")
api.HandleFunc("/attachments", attachmentHandler.Create).Methods("POST")
api.HandleFunc("/attachments/{id}", attachmentHandler.Get).Methods("GET")
api.HandleFunc("/attachments/{id}", attachmentHandler.Update).Methods("PUT")
api.HandleFunc("/attachments/{id}", attachmentHandler.Delete).Methods("DELETE")
// Country routes
api.HandleFunc("/countries", countryHandler.List).Methods("GET")
api.HandleFunc("/countries", countryHandler.Create).Methods("POST")
api.HandleFunc("/countries/{id}", countryHandler.Get).Methods("GET")
api.HandleFunc("/countries/{id}", countryHandler.Update).Methods("PUT")
api.HandleFunc("/countries/{id}", countryHandler.Delete).Methods("DELETE")
api.HandleFunc("/countries/complete", countryHandler.CompleteCountry).Methods("GET")
// Status routes
api.HandleFunc("/statuses", statusHandler.List).Methods("GET")
api.HandleFunc("/statuses", statusHandler.Create).Methods("POST")
api.HandleFunc("/statuses/{id}", statusHandler.Get).Methods("GET")
api.HandleFunc("/statuses/{id}", statusHandler.Update).Methods("PUT")
api.HandleFunc("/statuses/{id}", statusHandler.Delete).Methods("DELETE")
api.HandleFunc("/statuses/json/{selectedId}", statusHandler.JsonList).Methods("GET")
// Line Item routes
api.HandleFunc("/line-items", lineItemHandler.List).Methods("GET")
api.HandleFunc("/line-items", lineItemHandler.Create).Methods("POST")
api.HandleFunc("/line-items/{id}", lineItemHandler.Get).Methods("GET")
api.HandleFunc("/line-items/{id}", lineItemHandler.Update).Methods("PUT")
api.HandleFunc("/line-items/{id}", lineItemHandler.Delete).Methods("DELETE")
api.HandleFunc("/line-items/document/{documentID}/table", lineItemHandler.GetTable).Methods("GET")
2025-06-24 03:32:28 -07:00
// Health check
api.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"ok"}`))
}).Methods("GET")
2025-07-06 14:34:12 -07:00
// Recent activity endpoint
r.HandleFunc("/api/recent-activity", documentHandler.GetRecentActivity).Methods("GET")
2025-06-24 03:32:28 -07:00
// Page routes
r.HandleFunc("/", pageHandler.Home).Methods("GET")
2025-07-02 05:04:36 -07:00
2025-06-24 03:32:28 -07:00
// Customer pages
r.HandleFunc("/customers", pageHandler.CustomersIndex).Methods("GET")
r.HandleFunc("/customers/new", pageHandler.CustomersNew).Methods("GET")
r.HandleFunc("/customers/search", pageHandler.CustomersSearch).Methods("GET")
r.HandleFunc("/customers/{id}", pageHandler.CustomersShow).Methods("GET")
r.HandleFunc("/customers/{id}/edit", pageHandler.CustomersEdit).Methods("GET")
2025-07-02 05:04:36 -07:00
2025-06-24 03:32:28 -07:00
// Product pages
r.HandleFunc("/products", pageHandler.ProductsIndex).Methods("GET")
r.HandleFunc("/products/new", pageHandler.ProductsNew).Methods("GET")
r.HandleFunc("/products/search", pageHandler.ProductsSearch).Methods("GET")
r.HandleFunc("/products/{id}", pageHandler.ProductsShow).Methods("GET")
r.HandleFunc("/products/{id}/edit", pageHandler.ProductsEdit).Methods("GET")
2025-07-02 05:04:36 -07:00
2025-06-24 03:32:28 -07:00
// Purchase Order pages
r.HandleFunc("/purchase-orders", pageHandler.PurchaseOrdersIndex).Methods("GET")
r.HandleFunc("/purchase-orders/new", pageHandler.PurchaseOrdersNew).Methods("GET")
r.HandleFunc("/purchase-orders/search", pageHandler.PurchaseOrdersSearch).Methods("GET")
r.HandleFunc("/purchase-orders/{id}", pageHandler.PurchaseOrdersShow).Methods("GET")
r.HandleFunc("/purchase-orders/{id}/edit", pageHandler.PurchaseOrdersEdit).Methods("GET")
2025-07-02 05:04:36 -07:00
2025-06-24 03:32:28 -07:00
// Enquiry pages
r.HandleFunc("/enquiries", pageHandler.EnquiriesIndex).Methods("GET")
r.HandleFunc("/enquiries/new", pageHandler.EnquiriesNew).Methods("GET")
r.HandleFunc("/enquiries/search", pageHandler.EnquiriesSearch).Methods("GET")
r.HandleFunc("/enquiries/{id}", pageHandler.EnquiriesShow).Methods("GET")
r.HandleFunc("/enquiries/{id}/edit", pageHandler.EnquiriesEdit).Methods("GET")
2025-07-02 05:04:36 -07:00
// Document pages
r.HandleFunc("/documents", pageHandler.DocumentsIndex).Methods("GET")
r.HandleFunc("/documents/search", pageHandler.DocumentsSearch).Methods("GET")
r.HandleFunc("/documents/view/{id}", pageHandler.DocumentsView).Methods("GET")
r.HandleFunc("/documents/{id}", pageHandler.DocumentsShow).Methods("GET")
r.HandleFunc("/documents/pdf/{id}", documentHandler.GeneratePDF).Methods("GET")
// Address routes (matching CakePHP)
r.HandleFunc("/addresses", addressHandler.List).Methods("GET")
r.HandleFunc("/addresses/view/{id}", addressHandler.Get).Methods("GET")
r.HandleFunc("/addresses/add/{customerid}", addressHandler.Create).Methods("GET", "POST")
r.HandleFunc("/addresses/add_another/{increment}", addressHandler.AddAnother).Methods("GET")
r.HandleFunc("/addresses/remove_another/{increment}", addressHandler.RemoveAnother).Methods("GET")
r.HandleFunc("/addresses/edit/{id}", addressHandler.Update).Methods("GET", "POST")
r.HandleFunc("/addresses/customer_addresses/{customerID}", addressHandler.CustomerAddresses).Methods("GET")
// Attachment routes (matching CakePHP)
r.HandleFunc("/attachments", attachmentHandler.List).Methods("GET")
r.HandleFunc("/attachments/view/{id}", attachmentHandler.Get).Methods("GET")
r.HandleFunc("/attachments/archived", attachmentHandler.Archived).Methods("GET")
r.HandleFunc("/attachments/add", attachmentHandler.Create).Methods("GET", "POST")
r.HandleFunc("/attachments/edit/{id}", attachmentHandler.Update).Methods("GET", "POST")
r.HandleFunc("/attachments/delete/{id}", attachmentHandler.Delete).Methods("POST")
// Country routes (matching CakePHP)
r.HandleFunc("/countries", countryHandler.List).Methods("GET")
r.HandleFunc("/countries/view/{id}", countryHandler.Get).Methods("GET")
r.HandleFunc("/countries/add", countryHandler.Create).Methods("GET", "POST")
r.HandleFunc("/countries/edit/{id}", countryHandler.Update).Methods("GET", "POST")
r.HandleFunc("/countries/delete/{id}", countryHandler.Delete).Methods("POST")
r.HandleFunc("/countries/complete_country", countryHandler.CompleteCountry).Methods("GET")
// Status routes (matching CakePHP)
r.HandleFunc("/statuses", statusHandler.List).Methods("GET")
r.HandleFunc("/statuses/view/{id}", statusHandler.Get).Methods("GET")
r.HandleFunc("/statuses/add", statusHandler.Create).Methods("GET", "POST")
r.HandleFunc("/statuses/edit/{id}", statusHandler.Update).Methods("GET", "POST")
r.HandleFunc("/statuses/delete/{id}", statusHandler.Delete).Methods("POST")
r.HandleFunc("/statuses/json_list/{selectedId}", statusHandler.JsonList).Methods("GET")
// Line Item routes (matching CakePHP)
r.HandleFunc("/line_items/ajax_add", lineItemHandler.AjaxAdd).Methods("POST")
r.HandleFunc("/line_items/ajax_edit", lineItemHandler.AjaxEdit).Methods("POST")
r.HandleFunc("/line_items/ajax_delete/{id}", lineItemHandler.AjaxDelete).Methods("POST")
r.HandleFunc("/line_items/get_table/{documentID}", lineItemHandler.GetTable).Methods("GET")
r.HandleFunc("/line_items/edit/{id}", lineItemHandler.Update).Methods("GET", "POST")
r.HandleFunc("/line_items/add/{documentID}", lineItemHandler.Create).Methods("GET", "POST")
2025-06-24 03:32:28 -07:00
// HTMX endpoints
r.HandleFunc("/customers", customerHandler.Create).Methods("POST")
r.HandleFunc("/customers/{id}", customerHandler.Update).Methods("PUT")
r.HandleFunc("/customers/{id}", customerHandler.Delete).Methods("DELETE")
2025-07-02 05:04:36 -07:00
2025-06-24 03:32:28 -07:00
r.HandleFunc("/products", productHandler.Create).Methods("POST")
r.HandleFunc("/products/{id}", productHandler.Update).Methods("PUT")
r.HandleFunc("/products/{id}", productHandler.Delete).Methods("DELETE")
2025-07-02 05:04:36 -07:00
2025-06-24 03:32:28 -07:00
r.HandleFunc("/purchase-orders", purchaseOrderHandler.Create).Methods("POST")
r.HandleFunc("/purchase-orders/{id}", purchaseOrderHandler.Update).Methods("PUT")
r.HandleFunc("/purchase-orders/{id}", purchaseOrderHandler.Delete).Methods("DELETE")
2025-07-02 05:04:36 -07:00
2025-06-24 03:32:28 -07:00
r.HandleFunc("/enquiries", enquiryHandler.Create).Methods("POST")
r.HandleFunc("/enquiries/{id}", enquiryHandler.Update).Methods("PUT")
r.HandleFunc("/enquiries/{id}", enquiryHandler.Delete).Methods("DELETE")
r.HandleFunc("/enquiries/{id}/undelete", enquiryHandler.Undelete).Methods("PUT")
r.HandleFunc("/enquiries/{id}/status", enquiryHandler.UpdateStatus).Methods("PUT")
r.HandleFunc("/enquiries/{id}/mark-submitted", enquiryHandler.MarkSubmitted).Methods("PUT")
2025-07-02 05:04:36 -07:00
r.HandleFunc("/documents", documentHandler.Create).Methods("POST")
r.HandleFunc("/documents/{id}", documentHandler.Update).Methods("PUT")
r.HandleFunc("/documents/{id}/archive", documentHandler.Archive).Methods("PUT")
r.HandleFunc("/documents/{id}/unarchive", documentHandler.Unarchive).Methods("PUT")
2025-06-24 03:32:28 -07:00
// Start server
port := getEnv("PORT", "8080")
log.Printf("Starting server on port %s", port)
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Fatal("Failed to start server:", err)
}
}
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
2025-07-02 05:04:36 -07:00
}