rootmux.go 1.85 KB
Newer Older
Alexis POYEN's avatar
Alexis POYEN committed
1
2
3
4
5
6
package rootmux

import (
	"net/http"
	"os"

7
	"forge.grandlyon.com/apoyen/sdk-go/internal/auth"
8
	"forge.grandlyon.com/apoyen/sdk-go/internal/models"
Alexis POYEN's avatar
Alexis POYEN committed
9
	"forge.grandlyon.com/apoyen/sdk-go/pkg/middlewares"
Alexis POYEN's avatar
Alexis POYEN committed
10

Alexis POYEN's avatar
Alexis POYEN committed
11
	"forge.grandlyon.com/apoyen/sdk-go/pkg/common"
Alexis POYEN's avatar
Alexis POYEN committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
)

// 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)
32
33

	// Bank API endpoints
34
	dh := models.NewDataHandler()
Alexis POYEN's avatar
Alexis POYEN committed
35
	mainMux.HandleFunc("/api/", dh.ProcessAPI)
36
37

	// Common API endpoints
Alexis POYEN's avatar
Alexis POYEN committed
38
39
40
41
42
43
44
	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)
45
	mainMux.Handle("/api/admin/", http.StripPrefix("/api/admin", auth.ValidateAuthMiddleware(adminMux, []string{"ADMIN"}, true)))
Alexis POYEN's avatar
Alexis POYEN committed
46
47
48
49
50
51
52
	// 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+":*", false))
	return RootMux{mux, &m}
}