Files
workspace/apps/go-crm/main.go

164 lines
5.2 KiB
Go

package main
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() {
database, err := db.Init("/workspace/data/go-crm.db")
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
defer database.Close()
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()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
_, err := handlers.GetAccountID(r)
if err != nil {
http.Redirect(w, r, "/auth/login", http.StatusFound)
return
}
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<!DOCTYPE html><html><head><title>Dashboard</title></head><body><h1>Welcome to CRM</h1><nav><a href="/clients">Clients</a> | <a href="/customers">Customers</a> | <a href="/services">Services</a> | <a href="/scheduling">Scheduling</a> | <a href="/payments">Payments</a> | <a href="/questions">Questions</a> | <a href="/answers">Answers</a> | <a href="/auth/account">Account</a> | <form method="POST" action="/auth/logout" style="display:inline"><button type="submit">Logout</button></form></nav></body></html>`))
})
r.Route("/auth", func(r chi.Router) {
r.Get("/signup", handlers.SignupPage)
r.Post("/signup", handlers.Signup)
r.Get("/login", handlers.LoginPage)
r.Post("/login", handlers.Login)
r.Post("/logout", handlers.Logout)
r.Get("/account", handlers.AccountPage)
r.Post("/account", handlers.UpdateAccount)
})
r.Route("/clients", func(r chi.Router) {
r.Get("/", handlers.ListClients)
r.Post("/", handlers.CreateClient)
r.Get("/{id}", handlers.ViewClient)
r.Put("/{id}", handlers.UpdateClient)
r.Delete("/{id}", handlers.DeleteClient)
})
r.Route("/customers", func(r chi.Router) {
r.Get("/", handlers.ListCustomers)
r.Post("/", handlers.CreateCustomer)
r.Get("/{id}", handlers.ViewCustomer)
r.Put("/{id}", handlers.UpdateCustomer)
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)
r.Get("/{id}", handlers.ViewService)
r.Put("/{id}", handlers.UpdateService)
r.Delete("/{id}", handlers.DeleteService)
})
r.Route("/scheduling", func(r chi.Router) {
r.Get("/", handlers.ListSchedules)
r.Post("/", handlers.CreateSchedule)
r.Get("/{id}", handlers.ViewSchedule)
r.Put("/{id}", handlers.UpdateSchedule)
r.Delete("/{id}", handlers.DeleteSchedule)
})
r.Route("/payments", func(r chi.Router) {
r.Get("/", handlers.ListPayments)
r.Post("/", handlers.CreatePayment)
r.Get("/{id}", handlers.ViewPayment)
r.Put("/{id}", handlers.UpdatePayment)
r.Delete("/{id}", handlers.DeletePayment)
})
r.Route("/questions", func(r chi.Router) {
r.Get("/", handlers.ListQuestions)
r.Post("/", handlers.CreateQuestion)
r.Get("/{id}", handlers.ViewQuestion)
r.Put("/{id}", handlers.UpdateQuestion)
r.Delete("/{id}", handlers.DeleteQuestion)
})
r.Route("/answers", func(r chi.Router) {
r.Get("/", handlers.ListAnswers)
r.Post("/", handlers.CreateAnswer)
r.Get("/{id}", handlers.ViewAnswer)
r.Put("/{id}", handlers.UpdateAnswer)
r.Delete("/{id}", handlers.DeleteAnswer)
})
fmt.Println("CRM server running on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", r))
}