112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"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) {
|
|
_, 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.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))
|
|
} |