Skip to content
Snippets Groups Projects
rootmux.go 1.97 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis Poyen's avatar
    Alexis Poyen committed
    package rootmux
    
    import (
    	"net/http"
    	"os"
    
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	"forge.grandlyon.com/gestion-des-assemblees/elections/internal/auth"
    	"forge.grandlyon.com/gestion-des-assemblees/elections/internal/models"
    
    	"forge.grandlyon.com/gestion-des-assemblees/elections/pkg/middlewares"
    
    Alexis Poyen's avatar
    Alexis Poyen committed
    
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	"forge.grandlyon.com/systemes-dinformation/project-template/sdk-go/pkg/common"
    
    Alexis Poyen's avatar
    Alexis Poyen committed
    )
    
    // RootMux represents the main controller of the application
    type RootMux struct {
    	Mux     http.Handler
    	Manager *auth.Manager
    }
    
    // CreateRootMux creates a RootMux
    func CreateRootMux(port int, staticDir string) RootMux {
    	hostname := os.Getenv("HOSTNAME")
    	// Create the main handler
    	mainMux := http.NewServeMux()
    	// ALL USERS API ENDPOINTS
    	// Authentication endpoints
    	m := auth.NewManager()
    	mainMux.HandleFunc("/OAuth2Login", m.HandleOAuth2Login)
    	mainMux.Handle("/OAuth2Callback", m.HandleOAuth2Callback())
    	mainMux.HandleFunc("/Logout", m.HandleLogout)
    	mainMux.HandleFunc("/Login", m.HandleInMemoryLogin)
    
    	// Bank API endpoints
    	dh := models.NewDataHandler()
    	mainMux.HandleFunc("/api/", dh.ProcessAPI)
    
    	// Common API endpoints
    	commonMux := http.NewServeMux()
    	mainMux.Handle("/api/common/WhoAmI", auth.ValidateAuthMiddleware(auth.WhoAmI(), []string{"*"}, false))
    	commonMux.HandleFunc("/Share", auth.GetShareToken)
    	mainMux.Handle("/api/common/", http.StripPrefix("/api/common", auth.ValidateAuthMiddleware(commonMux, []string{"*"}, true)))
    	// ADMIN API ENDPOINTS
    	adminMux := http.NewServeMux()
    	adminMux.HandleFunc("/users/", auth.ProcessUsers)
    	mainMux.Handle("/api/admin/", http.StripPrefix("/api/admin", auth.ValidateAuthMiddleware(adminMux, []string{"ADMIN"}, true)))
    	// Serve static files falling back to serving index.html
    	mainMux.Handle("/", middlewares.NoCache(http.FileServer(&common.FallBackWrapper{Assets: http.Dir(staticDir)})))
    	// Put it together into the main handler
    	mux := http.NewServeMux()
    
    	mux.Handle(hostname+"/", middlewares.WebSecurity(mainMux, "*."+hostname+":* *.mapbox.com *.grandlyon.com ", false))
    
    Alexis Poyen's avatar
    Alexis Poyen committed
    	return RootMux{mux, &m}
    }