112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"go-crm/internal/db"
|
|
"go-crm/internal/handlers"
|
|
"go-crm/internal/templates"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
)
|
|
|
|
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()
|
|
|
|
handlers.SetupHandlers(database)
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(middleware.Logger)
|
|
r.Use(middleware.Recoverer)
|
|
r.Use(middleware.RequestID)
|
|
|
|
templates.Init()
|
|
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
homeTmpl := templates.Get("home.html")
|
|
layoutTmpl := templates.Layout("Home", "")
|
|
if homeTmpl != nil && layoutTmpl != nil {
|
|
buf := &strings.Builder{}
|
|
homeTmpl.Execute(buf, map[string]string{"Title": "Home"})
|
|
layoutTmpl.Execute(w, map[string]string{"Title": "Home", "Content": buf.String()})
|
|
} else {
|
|
w.Write([]byte(`<!DOCTYPE html><html><head><title>Home</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="/questions">Questions</a> | <a href="/answers">Answers</a></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.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.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))
|
|
} |