95 lines
3.6 KiB
Go
95 lines
3.6 KiB
Go
package routes
|
|
|
|
import (
|
|
"cangui/whatsapp/backend/handlers"
|
|
"cangui/whatsapp/backend/middleware"
|
|
"cangui/whatsapp/backend/renders"
|
|
"net/http"
|
|
|
|
"github.com/golang-jwt/jwt"
|
|
"github.com/gorilla/mux"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func RoutesPublic(r *mux.Router, db *gorm.DB) {
|
|
|
|
|
|
// Fichiers statiques (CSS, JS, etc.)
|
|
staticDir := "./frontend/assets/"
|
|
r.PathPrefix("/frontend/assets/").Handler(
|
|
http.StripPrefix("/frontend/assets/", http.FileServer(http.Dir(staticDir))),
|
|
)
|
|
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
|
|
}
|
|
|
|
// 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)
|
|
})
|
|
// Page de login
|
|
r.HandleFunc("/auth/with-token", middleware.AuthWithTokenHandler()).Methods("GET")
|
|
r.HandleFunc("/api/sso/login", handlers.SSOLoginPostHandler(db)).Methods("POST")
|
|
r.HandleFunc("/login", renders.Login)
|
|
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookVerifyHandler()).Methods("GET")
|
|
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookReceiveHandler(db)).Methods("POST")
|
|
|
|
// Endpoint d'API pour se logger
|
|
r.HandleFunc("/api/login", handlers.LoginHandler(db)).Methods("POST")
|
|
|
|
}
|
|
|
|
// Routes protégées
|
|
func RoutesProtected(r *mux.Router, db *gorm.DB) {
|
|
r.HandleFunc("/jwt", renders.JwtTest)
|
|
r.HandleFunc("/api/message/send", handlers.SendWhatsAppMessage(db)).Methods("POST")
|
|
r.HandleFunc("/admin/user/{id}/conversations", renders.AdminConversationPage(db))
|
|
r.HandleFunc("/api/user/{id}/conversations", renders.AdminConversationRows(db))
|
|
|
|
//page user
|
|
r.HandleFunc("/admin/user", renders.AdminUserList(db))
|
|
r.HandleFunc("/api/user/create", renders.CreateUserHandler(db)).Methods("POST")
|
|
r.HandleFunc("/admin/user/{id}/edit", renders.AdminUserEdit(db)).Methods("GET")
|
|
|
|
|
|
// api user
|
|
r.HandleFunc("/api/user/delete/{id}", handlers.AdminUserDelete(db)).Methods("DELETE")
|
|
r.HandleFunc("/api/user/update/{id}", renders.AdminUserUpdate(db)).Methods("PUT")
|
|
r.HandleFunc("/admin/user/create-form", renders.AdminUserCreateForm(db)).Methods("GET")
|
|
//ll
|
|
//page dashboard
|
|
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")
|
|
r.HandleFunc("/api/message/send2", handlers.HandleTemplateTest(db)).Methods("POST")
|
|
// r.HandleFunc("/admin/consumption", renders.ClientConsumptionPage(db))
|
|
r.HandleFunc("/admin/consumption", renders.ConsumptionPageGrouped(db))
|
|
|
|
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)
|
|
})
|
|
|
|
r.HandleFunc("/apidoc", renders.ApiDocPage(db))
|
|
}
|
|
|