Skip to content
Snippets Groups Projects
rootmux.go 3.29 KiB
Newer Older
  • Learn to ignore specific revisions
  • Rémi PAILHAREY's avatar
    Rémi PAILHAREY committed
    package rootmux
    
    import (
    	"net/http"
    
    	"forge.grandlyon.com/web-et-numerique/factory/llle_project/backoffice-server/internal/auth"
    	"forge.grandlyon.com/web-et-numerique/factory/llle_project/backoffice-server/internal/common"
    	"forge.grandlyon.com/web-et-numerique/factory/llle_project/backoffice-server/internal/file"
    	"forge.grandlyon.com/web-et-numerique/factory/llle_project/backoffice-server/internal/models"
    
    	"github.com/go-chi/chi/v5"
    	"github.com/go-chi/chi/v5/middleware"
    
    Rémi PAILHAREY's avatar
    Rémi PAILHAREY committed
    )
    
    
    Rémi PAILHAREY's avatar
    Rémi PAILHAREY committed
    var imageFolder = common.StringValueFromEnv("IMAGE_FOLDER", "")
    
    
    type RootMux struct {
    
    	Router  chi.Router
    
    	Manager *auth.Manager
    }
    
    
    //	@title			Ecolyo-Agent API
    //	@version		1.0.0
    //	@description	This is backend of Ecolyo-Agent, a backoffice managing newsletters, SGE consents, fluid prices and custom alerts for Ecolyo
    //	@termsOfService	http://swagger.io/terms/
    //	@contact.name	API Support
    //	@contact.email	rpailharey@grandlyon.com
    
    func CreateRootMux() RootMux {
    
    	r := chi.NewRouter()
    
    	m := auth.NewManager()
    
    	dh := models.NewDataHandler()
    
    	// Middleware
    	r.Use(middleware.Logger)
    	r.Use(middleware.Recoverer)
    
    
    	r.HandleFunc("/OAuth2Login", m.HandleOAuth2Login)
    	r.Handle("/OAuth2Callback", m.HandleOAuth2Callback())
    	r.HandleFunc("/Logout", m.HandleLogout)
    
    	r.Method(http.MethodGet, "/api/common/WhoAmI", auth.ValidateAuthMiddleware(auth.WhoAmI(), []string{"*"}, false))
    
    	fs := http.FileServer(http.Dir("./" + imageFolder + "/"))
    	r.Handle("/assets/*", http.StripPrefix("/assets/", fs))
    
    	r.Route("/api/common", func(r chi.Router) {
    		r.Get("/monthlyReport", dh.GetMonthlyReport)
    		r.Get("/partnersInfo", dh.GetPartnersInfo)
    		r.Get("/customPopup", dh.GetCustomPopup)
    		r.Get("/prices", dh.GetAllPrices)
    		r.Get("/prices/{fluidtype}", dh.GetPricesByFluid)
    	})
    
    
    	r.Route("/api/animator", func(r chi.Router) {
    		r.Use(auth.AnimatorAuthMiddleware)
    
    		r.Get("/mailSubject/{year}/{month}", dh.GetSingleMailSubject)
    		r.Put("/mailSubject", dh.SaveMailSubject)
    		r.Delete("/mailSubject/{year}/{month}", dh.DeleteMailSubject)
    
    		r.Get("/monthlyNews", dh.GetAllMonthlyNews)
    		r.Get("/monthlyNews/{year}/{month}", dh.GetSingleMonthlyNews)
    		r.Put("/monthlyNews", dh.SaveMonthlyNews)
    		r.Delete("/monthlyNews/{year}/{month}", dh.DeleteMonthlyNews)
    
    		r.Get("/monthlyInfo", dh.GetAllMonthlyInfo)
    		r.Get("/monthlyInfo/{year}/{month}", dh.GetSingleMonthlyInfo)
    		r.Put("/monthlyInfo", dh.SaveMonthlyInfo)
    		r.Delete("/monthlyInfo/{year}/{month}", dh.DeleteMonthlyInfo)
    
    		r.Get("/poll", dh.GetAllPolls)
    		r.Get("/poll/{year}/{month}", dh.GetSinglePoll)
    		r.Put("/poll", dh.SavePoll)
    		r.Delete("/poll/{year}/{month}", dh.DeletePoll)
    
    		r.Put("/partnersInfo", dh.SavePartnersInfo)
    		r.Put("/customPopup", dh.SaveCustomPopup)
    		r.Get("/imageNames", file.GetEcogestureImages)
    		r.Put("/prices", dh.SavePrice)
    
    	})
    
    	r.Route("/api/admin", func(r chi.Router) {
    		r.Use(auth.AdminAuthMiddleware)
    
    		r.Get("/consent", dh.SearchConsent)
    	})
    
    	r.Route("/api/sge", func(r chi.Router) {
    		r.Use(auth.SGEAuthMiddleware)
    		r.Post("/consent", dh.PostConsent)
    		r.Get("/consent/{id}", dh.GetConsentById)
    		r.Put("/consent/{id}", dh.UpdateConsent)
    		r.Delete("/consent/{id}", dh.DeleteConsentById)
    	})
    
    	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})
    
    
    	swaggerFs := http.FileServer(http.Dir("./docs/"))
    	r.Handle("/doc/*", http.StripPrefix("/doc/", swaggerFs))
    
    
    	return RootMux{r, &m}
    
    Rémi PAILHAREY's avatar
    Rémi PAILHAREY committed
    }