whatapp-go-pvnet/backend/main.go

35 lines
976 B
Go
Raw Permalink Normal View History

2025-05-09 08:14:22 +00:00
package main
import (
"cangui/whatsapp/backend/db"
"cangui/whatsapp/backend/middleware"
"cangui/whatsapp/backend/routes"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
// 1. Démarrer le routeur principal
r := mux.NewRouter()
// 2. Initialiser la DB
bd := db.InitDB()
// 3. Routes non protégées : on les monte sur le routeur principal
routes.RoutesPublic(r, bd)
// 4. Créer un sous-routeur pour les routes protégées
protected := r.PathPrefix("/").Subrouter()
// 5. Appliquer le middleware JWT à ce sous-routeur
protected.Use(middleware.AuthMiddleware)
// 6. Enregistrer les routes protégées sur ce sous-routeur
routes.RoutesProtected(protected, bd)
2025-05-11 07:57:12 +00:00
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/login", http.StatusSeeOther)
})
2025-05-09 08:14:22 +00:00
// 7. Lancer le serveur sur le port 4000
log.Fatal(http.ListenAndServe(":3003", r))
}