whatapp-go-pvnet/backend/routes/routes.go

94 lines
3.5 KiB
Go
Raw Normal View History

2025-05-09 08:14:22 +00:00
package routes
import (
"cangui/whatsapp/backend/handlers"
2025-05-11 08:45:42 +00:00
"cangui/whatsapp/backend/middleware"
2025-05-09 08:14:22 +00:00
"cangui/whatsapp/backend/renders"
"net/http"
2025-05-11 07:57:12 +00:00
"github.com/golang-jwt/jwt"
2025-05-09 08:14:22 +00:00
"github.com/gorilla/mux"
"gorm.io/gorm"
)
func RoutesPublic(r *mux.Router, db *gorm.DB) {
2025-05-09 17:04:21 +00:00
2025-05-09 17:34:14 +00:00
2025-05-09 17:04:21 +00:00
// Fichiers statiques (CSS, JS, etc.)
2025-05-09 08:14:22 +00:00
staticDir := "./frontend/assets/"
r.PathPrefix("/frontend/assets/").Handler(
http.StripPrefix("/frontend/assets/", http.FileServer(http.Dir(staticDir))),
)
2025-05-11 07:57:12 +00:00
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Tente de lire le cookie
cookie, err := r.Cookie("token")
if err != nil || cookie.Value == "" {
// Redirige vers login si pas de cookie
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
2025-05-09 17:34:14 +00:00
2025-05-11 07:57:12 +00:00
// Si cookie présent, tente de parser
token, err := jwt.Parse(cookie.Value, func(token *jwt.Token) (interface{}, error) {
return []byte("secret-key"), nil
})
if err != nil || !token.Valid {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
// Sinon on va vers le dashboard
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
})
2025-05-09 17:04:21 +00:00
// Page de login
2025-05-11 08:45:42 +00:00
r.HandleFunc("/auth/with-token", middleware.AuthWithTokenHandler()).Methods("GET")
r.HandleFunc("/api/sso/login", handlers.SSOLoginPostHandler(db)).Methods("POST")
2025-05-09 17:04:21 +00:00
r.HandleFunc("/login", renders.Login)
2025-05-09 16:35:21 +00:00
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookVerifyHandler()).Methods("GET")
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookReceiveHandler(db)).Methods("POST")
2025-05-09 17:34:14 +00:00
2025-05-09 17:04:21 +00:00
// Endpoint d'API pour se logger
r.HandleFunc("/api/login", handlers.LoginHandler(db)).Methods("POST")
2025-05-09 17:34:14 +00:00
}
// Routes protégées
func RoutesProtected(r *mux.Router, db *gorm.DB) {
2025-05-09 17:04:21 +00:00
r.HandleFunc("/jwt", renders.JwtTest)
2025-05-09 08:14:22 +00:00
r.HandleFunc("/api/message/send", handlers.SendWhatsAppMessage(db)).Methods("POST")
2025-05-09 17:04:21 +00:00
r.HandleFunc("/admin/user/{id}/conversations", renders.AdminConversationPage(db))
r.HandleFunc("/api/user/{id}/conversations", renders.AdminConversationRows(db))
2025-05-10 20:30:36 +00:00
//page user
2025-05-09 17:34:14 +00:00
r.HandleFunc("/admin/user", renders.AdminUserList(db))
2025-05-10 20:30:36 +00:00
r.HandleFunc("/api/user/create", renders.CreateUserHandler(db)).Methods("POST")
2025-05-11 06:53:44 +00:00
r.HandleFunc("/admin/user/{id}/edit", renders.AdminUserEdit(db)).Methods("GET")
2025-05-10 20:30:36 +00:00
// api user
2025-05-09 17:34:14 +00:00
r.HandleFunc("/api/user/delete/{id}", handlers.AdminUserDelete(db)).Methods("DELETE")
r.HandleFunc("/api/user/update/{id}", renders.AdminUserUpdate(db)).Methods("PUT")
2025-05-11 06:41:14 +00:00
r.HandleFunc("/admin/user/create-form", renders.AdminUserCreateForm(db)).Methods("GET")
2025-05-10 20:33:23 +00:00
//ll
2025-05-10 20:30:36 +00:00
//page dashboard
2025-05-09 17:04:21 +00:00
r.HandleFunc("/dashboard", renders.Dashboard(db))
r.HandleFunc("/test/send", renders.TestMessagesPages)
r.HandleFunc("/test/send2", renders.TestMessagesPages2)
r.HandleFunc("/admin/user/{id}/conversation-thread", renders.AdminConversationThread(db)).Methods("GET")
2025-05-09 16:35:21 +00:00
r.HandleFunc("/api/message/send2", handlers.HandleTemplateTest(db)).Methods("POST")
2025-05-11 16:44:10 +00:00
r.HandleFunc("/admin/consumption", renders.ClientConsumptionPage(db))
2025-05-09 14:51:23 +00:00
r.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: "",
Path: "/",
MaxAge: -1,
HttpOnly: true,
})
http.Redirect(w, r, "/login", http.StatusSeeOther)
})
2025-05-09 17:04:21 +00:00
2025-05-11 07:46:08 +00:00
r.HandleFunc("/apidoc", renders.ApiDocPage(db))
2025-05-09 17:34:14 +00:00
}