hhhhh
This commit is contained in:
parent
2cac603952
commit
2f4d8ee851
@ -13,17 +13,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func Login(w http.ResponseWriter, r *http.Request,db *gorm.DB){
|
func Login(w http.ResponseWriter, r *http.Request){
|
||||||
renderTemplate(w,r,"login",nil,db)
|
renderTemplate(w,"login",nil)
|
||||||
}
|
}
|
||||||
func JwtTest(w http.ResponseWriter, r *http.Request,db *gorm.DB){
|
func JwtTest(w http.ResponseWriter, r *http.Request){
|
||||||
renderTemplate(w,r,"jwt",nil,db)
|
renderTemplate(w,"jwt",nil)
|
||||||
}
|
}
|
||||||
func TestMessagesPages(w http.ResponseWriter, r *http.Request,db *gorm.DB){
|
func TestMessagesPages(w http.ResponseWriter, r *http.Request){
|
||||||
renderTemplate(w,r,"test-send",nil,db)
|
renderTemplate(w,"test-send",nil)
|
||||||
}
|
}
|
||||||
func TestMessagesPages2(w http.ResponseWriter, r *http.Request,db *gorm.DB){
|
func TestMessagesPages2(w http.ResponseWriter, r *http.Request){
|
||||||
renderTemplate(w,r,"test",nil,db)
|
renderTemplate(w,"test",nil)
|
||||||
}
|
}
|
||||||
func AdminUserList(db *gorm.DB) http.HandlerFunc {
|
func AdminUserList(db *gorm.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -38,7 +38,7 @@ func AdminUserList(db *gorm.DB) http.HandlerFunc {
|
|||||||
"Users": users,
|
"Users": users,
|
||||||
}
|
}
|
||||||
|
|
||||||
renderTemplate(w,r, "admin_users", data,db)
|
renderTemplate(w, "admin_users", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func AdminUserEdit(db *gorm.DB) http.HandlerFunc {
|
func AdminUserEdit(db *gorm.DB) http.HandlerFunc {
|
||||||
@ -115,6 +115,43 @@ func AdminUserCreateForm() http.HandlerFunc {
|
|||||||
renderPartial(w, "admin_user_create", nil)
|
renderPartial(w, "admin_user_create", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func AdminUserCreate(db *gorm.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
|
http.Error(w, "Formulaire invalide", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
email := r.FormValue("email")
|
||||||
|
password := r.FormValue("password")
|
||||||
|
role := models.UserRole(r.FormValue("role"))
|
||||||
|
|
||||||
|
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Erreur hash", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ssoid := "sso_" + uuid.New().String()
|
||||||
|
user := models.User{
|
||||||
|
Email: email,
|
||||||
|
Password: string(hashed),
|
||||||
|
Role: role,
|
||||||
|
IsActive: true,
|
||||||
|
SSOID: ssoid,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Create(&user).Error; err != nil {
|
||||||
|
http.Error(w, "Erreur enregistrement", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("HX-Trigger", `{"userCreated":"Utilisateur créé avec succès"}`)
|
||||||
|
w.Header().Set("HX-Remove", "true") // cache le form
|
||||||
|
renderPartial(w, "admin_user_row", map[string]interface{}{"User": user})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func AdminUserEditForm(db *gorm.DB) http.HandlerFunc {
|
func AdminUserEditForm(db *gorm.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
id := mux.Vars(r)["id"]
|
id := mux.Vars(r)["id"]
|
||||||
@ -192,7 +229,7 @@ func AdminConversationPage(db *gorm.DB) http.HandlerFunc {
|
|||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"UserID": idStr,
|
"UserID": idStr,
|
||||||
}
|
}
|
||||||
renderTemplate(w,r, "adminconversations", data,db)
|
renderTemplate(w, "adminconversations", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func AdminConversationRows(db *gorm.DB) http.HandlerFunc {
|
func AdminConversationRows(db *gorm.DB) http.HandlerFunc {
|
||||||
@ -326,45 +363,32 @@ func Dashboard(db *gorm.DB) http.HandlerFunc {
|
|||||||
"Conversations": conversations,
|
"Conversations": conversations,
|
||||||
}
|
}
|
||||||
|
|
||||||
renderTemplate(w,r ,"dashboard", data,db)
|
renderTemplate(w, "dashboard", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func renderTemplate(w http.ResponseWriter, r *http.Request, templ string, data map[string]interface{}, db *gorm.DB) {
|
func renderTemplate(w http.ResponseWriter, templ string, data map[string]interface{}) {
|
||||||
// Initialiser la map si elle est nil
|
t, err := template.ParseFiles(
|
||||||
if data == nil {
|
|
||||||
data = make(map[string]interface{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Si "User" absent mais SSOID présent dans le contexte, on charge l'utilisateur
|
|
||||||
if _, exists := data["User"]; !exists {
|
|
||||||
if val := r.Context().Value("ssoid"); val != nil {
|
|
||||||
ssoid := val.(string)
|
|
||||||
var user models.User
|
|
||||||
if err := db.Where("sso_id = ?", ssoid).First(&user).Error; err == nil {
|
|
||||||
data["User"] = user
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
t, err := template.ParseFiles(
|
|
||||||
"./frontend/templates/head.pages.tmpl",
|
"./frontend/templates/head.pages.tmpl",
|
||||||
"./frontend/templates/sidebar.pages.tmpl",
|
"./frontend/templates/sidebar.pages.tmpl",
|
||||||
"./frontend/templates/" + templ + ".pages.tmpl",
|
"./frontend/templates/" + templ + ".pages.tmpl",
|
||||||
)
|
)
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := t.ExecuteTemplate(w, templ+".pages.tmpl", data); err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exécutez explicitement le template principal
|
||||||
|
err = t.ExecuteTemplate(w, templ+".pages.tmpl", data)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func renderPartial(w http.ResponseWriter, templ string, data map[string]interface{}) {
|
func renderPartial(w http.ResponseWriter, templ string, data map[string]interface{}) {
|
||||||
t, err := template.ParseFiles("./frontend/templates/" + templ + ".pages.tmpl")
|
t, err := template.ParseFiles("./frontend/templates/" + templ + ".pages.tmpl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -11,67 +11,37 @@ import (
|
|||||||
|
|
||||||
// Routes non protégées
|
// Routes non protégées
|
||||||
func RoutesPublic(r *mux.Router, db *gorm.DB) {
|
func RoutesPublic(r *mux.Router, db *gorm.DB) {
|
||||||
// Assets statiques
|
|
||||||
|
// Fichiers statiques (CSS, JS, etc.)
|
||||||
staticDir := "./frontend/assets/"
|
staticDir := "./frontend/assets/"
|
||||||
r.PathPrefix("/frontend/assets/").Handler(
|
r.PathPrefix("/frontend/assets/").Handler(
|
||||||
http.StripPrefix("/frontend/assets/", http.FileServer(http.Dir(staticDir))),
|
http.StripPrefix("/frontend/assets/", http.FileServer(http.Dir(staticDir))),
|
||||||
)
|
)
|
||||||
|
|
||||||
// Login
|
// Page de login
|
||||||
r.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/login", renders.Login)
|
||||||
renders.Login(w, r, db)
|
|
||||||
})
|
|
||||||
r.HandleFunc("/api/login", handlers.LoginHandler(db)).Methods("POST")
|
|
||||||
|
|
||||||
// Webhook WhatsApp (vérification + réception)
|
|
||||||
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookVerifyHandler()).Methods("GET")
|
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookVerifyHandler()).Methods("GET")
|
||||||
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookReceiveHandler(db)).Methods("POST")
|
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
|
// Routes protégées
|
||||||
func RoutesProtected(r *mux.Router, db *gorm.DB) {
|
func RoutesProtected(r *mux.Router, db *gorm.DB) {
|
||||||
// Dashboard (admin ou client)
|
r.HandleFunc("/jwt", renders.JwtTest)
|
||||||
r.HandleFunc("/dashboard", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
renders.Dashboard(db)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Test d'envoi de message (simples & templates)
|
|
||||||
r.HandleFunc("/test/send", func(w http.ResponseWriter, r *http.Request) {renders.TestMessagesPages(w, r, db)})
|
|
||||||
r.HandleFunc("/test/send2", func(w http.ResponseWriter, r *http.Request) {renders.TestMessagesPages2(w, r, db)})
|
|
||||||
r.HandleFunc("/api/message/send", handlers.SendWhatsAppMessage(db)).Methods("POST")
|
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))
|
||||||
|
r.HandleFunc("/admin/user/{id}/edit", renders.AdminUserEdit(db)).Methods("GET")
|
||||||
|
r.HandleFunc("/admin/user/new", renders.AdminUserCreate(db)).Methods("GET")
|
||||||
|
|
||||||
|
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("/api/message/send2", handlers.HandleTemplateTest(db)).Methods("POST")
|
||||||
|
|
||||||
// Conversations (admin/client)
|
|
||||||
r.HandleFunc("/admin/user/{id}/conversations", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
renders.AdminConversationPage(db)
|
|
||||||
})
|
|
||||||
r.HandleFunc("/api/user/{id}/conversations", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
renders.AdminConversationRows(db)
|
|
||||||
})
|
|
||||||
r.HandleFunc("/admin/user/{id}/conversation-thread", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
renders.AdminConversationThread(db)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Utilisateurs (admin)
|
|
||||||
r.HandleFunc("/admin/user", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
renders.AdminUserList(db)
|
|
||||||
})
|
|
||||||
r.HandleFunc("/admin/user/create-form", renders.AdminUserCreateForm()).Methods("GET")
|
|
||||||
r.HandleFunc("/admin/user/{id}/edit", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
renders.AdminUserEdit(db)
|
|
||||||
}).Methods("GET")
|
|
||||||
|
|
||||||
r.HandleFunc("/api/user/create", renders.CreateUserHandler(db)).Methods("POST")
|
|
||||||
r.HandleFunc("/api/user/update/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
renders.AdminUserUpdate(db)
|
|
||||||
}).Methods("PUT")
|
|
||||||
r.HandleFunc("/api/user/delete/{id}", handlers.AdminUserDelete(db)).Methods("DELETE")
|
|
||||||
|
|
||||||
// Test JWT
|
|
||||||
r.HandleFunc("/jwt", func(w http.ResponseWriter, r *http.Request){renders.JwtTest(w, r, db)})
|
|
||||||
|
|
||||||
// Déconnexion
|
|
||||||
r.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: "token",
|
Name: "token",
|
||||||
@ -82,4 +52,35 @@ func RoutesProtected(r *mux.Router, db *gorm.DB) {
|
|||||||
})
|
})
|
||||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
// // Ici on place les vues et API qui doivent être protégées
|
||||||
|
// r.HandleFunc("/stream", StreamHandler)
|
||||||
|
// r.HandleFunc("/dashboard", renders.Dashboard(bd))
|
||||||
|
// r.HandleFunc("/settings", renders.Settings)
|
||||||
|
// r.HandleFunc("/library", renders.Library)
|
||||||
|
// r.HandleFunc("/menuLibary", renders.Library)
|
||||||
|
// r.HandleFunc("/godownloader/downloads", renders.GoDownload)
|
||||||
|
// r.HandleFunc("/godownloader/linkcollectors", renders.GoDownloadLinkCollectors)
|
||||||
|
// r.HandleFunc("/godownloader/settings", renders.GoDownloadSetting)
|
||||||
|
// // API user
|
||||||
|
// r.HandleFunc("/api/user/create", users.CreateUser(bd)).Methods("POST")
|
||||||
|
// r.HandleFunc("/api/user/update/{id}", users.UpdateUser(bd)).Methods("PUT")
|
||||||
|
// r.HandleFunc("/api/user/delete/{id}", users.DeleteUser(bd)).Methods("DELETE")
|
||||||
|
// r.HandleFunc("/api/user/all/", users.ReadAllUser(bd)).Methods("GET")
|
||||||
|
// r.HandleFunc("/api/user/{id}", users.FindUserById(bd)).Methods("GET")
|
||||||
|
|
||||||
|
// // API download
|
||||||
|
// r.HandleFunc("/api/pathDownload/create", download.CreateSavePath(bd)).Methods("POST")
|
||||||
|
// r.HandleFunc("/api/pathDownload/update/{id}", download.UpdateSavePath(bd)).Methods("PUT")
|
||||||
|
// r.HandleFunc("/api/pathDownload/delete/{id}", download.DeleteSavePath(bd)).Methods("DELETE")
|
||||||
|
// r.HandleFunc("/api/pathDownload/all/", download.ReadAllSavePath(bd)).Methods("GET")
|
||||||
|
|
||||||
|
// //API Check path
|
||||||
|
// r.HandleFunc("/validate-path", download.PathValidationHandler)
|
||||||
|
|
||||||
|
//API Scan folder
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user