Add leads handlers, WhatsApp integration, and test files
This commit is contained in:
@@ -4,13 +4,16 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"go-crm/internal/db"
|
||||
"go-crm/internal/handlers"
|
||||
"go-crm/internal/templates"
|
||||
wa "go-crm/internal/whatsapp"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -20,13 +23,29 @@ func main() {
|
||||
}
|
||||
defer database.Close()
|
||||
|
||||
handlers.SetupHandlers(database)
|
||||
waConnector, err := wa.NewWhatsmeowAdapter("/workspace/data/whatsapp.db", "http://localhost:8080", "internal-secret")
|
||||
if err != nil {
|
||||
log.Printf("Warning: failed to initialize WhatsApp connector: %v", err)
|
||||
waConnector = nil
|
||||
} else {
|
||||
waConnector.SetClientDB(database)
|
||||
}
|
||||
|
||||
handlers.SetupHandlers(database, waConnector)
|
||||
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(middleware.Recoverer)
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(cors.Handler(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
|
||||
AllowedHeaders: []string{"Accept", "Content-Type", "X-Internal-Secret", "Origin"},
|
||||
ExposedHeaders: []string{"Content-Length", "Content-Type"},
|
||||
AllowCredentials: false,
|
||||
MaxAge: 86400,
|
||||
}))
|
||||
|
||||
templates.Init()
|
||||
|
||||
@@ -67,6 +86,39 @@ func main() {
|
||||
r.Delete("/{id}", handlers.DeleteCustomer)
|
||||
})
|
||||
|
||||
r.Get("/debug/cors-test", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"status":"ok","origin":"`+r.Header.Get("Origin")+`"}`))
|
||||
})
|
||||
|
||||
r.Get("/debug/whatsapp", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if waConnector == nil {
|
||||
w.Write([]byte(`{"status":"error","error":"WhatsApp connector not initialized"}`))
|
||||
return
|
||||
}
|
||||
w.Write([]byte(`{"status":"ok","message":"WhatsApp connector initialized"}`))
|
||||
})
|
||||
|
||||
r.Get("/debug/net-test", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
resp, err := http.Get("https://www.google.com")
|
||||
if err != nil {
|
||||
w.Write([]byte(`{"status":"error","error":` + err.Error() + `}`))
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
w.Write([]byte(`{"status":"ok","code":` + strconv.Itoa(resp.StatusCode) + `}`))
|
||||
})
|
||||
|
||||
r.Route("/leads", func(r chi.Router) {
|
||||
r.Get("/", handlers.ListLeads)
|
||||
r.Get("/connect", handlers.LeadsConnectPage)
|
||||
r.Get("/qr", handlers.LeadsQR)
|
||||
r.Put("/{id}", handlers.UpdateLead)
|
||||
r.Delete("/{id}", handlers.DeleteLead)
|
||||
})
|
||||
|
||||
r.Route("/services", func(r chi.Router) {
|
||||
r.Get("/", handlers.ListServices)
|
||||
r.Post("/", handlers.CreateService)
|
||||
|
||||
Reference in New Issue
Block a user