Skip to content

Commit

Permalink
refactor how we register routes to make it easier to test
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed Feb 23, 2022
1 parent 1c17430 commit 1ae7192
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
21 changes: 2 additions & 19 deletions pkg/apiserver/server.go
Expand Up @@ -155,30 +155,13 @@ func Start(params *APIServerParams) {
* Unauthenticated routes
**********************************************************************/

debugRouter.HandleFunc("/healthz", handler.Healthz)
loggingRouter.HandleFunc("/api/v1/login", handler.Login)
loggingRouter.HandleFunc("/api/v1/login/info", handler.GetLoginInfo)
loggingRouter.HandleFunc("/api/v1/logout", handler.Logout) // this route uses its own auth
loggingRouter.Path("/api/v1/metadata").Methods("GET").HandlerFunc(handlers.GetMetadataHandler(handlers.GetMetaDataConfig))

loggingRouter.HandleFunc("/api/v1/oidc/login", handler.OIDCLogin)
loggingRouter.HandleFunc("/api/v1/oidc/login/callback", handler.OIDCLoginCallback)

loggingRouter.Path("/api/v1/troubleshoot/{appId}/{bundleId}").Methods("PUT").HandlerFunc(handler.UploadSupportBundle)
loggingRouter.Path("/api/v1/troubleshoot/supportbundle/{bundleId}/redactions").Methods("PUT").HandlerFunc(handler.SetSupportBundleRedactions)
loggingRouter.Path("/api/v1/preflight/app/{appSlug}/sequence/{sequence}").Methods("POST").HandlerFunc(handler.PostPreflightStatus)

// This the handler for license API and should be called by the application only.
loggingRouter.Path("/license/v1/license").Methods("GET").HandlerFunc(handler.GetPlatformLicenseCompatibility)
handlers.RegisterUnauthenticatedRoutes(handler, debugRouter, loggingRouter)

/**********************************************************************
* KOTS token auth routes
**********************************************************************/

debugRouter.Path("/api/v1/kots/ports").Methods("GET").HandlerFunc(handler.GetApplicationPorts)
loggingRouter.Path("/api/v1/upload").Methods("PUT").HandlerFunc(handler.UploadExistingApp)
loggingRouter.Path("/api/v1/download").Methods("GET").HandlerFunc(handler.DownloadApp)
loggingRouter.Path("/api/v1/airgap/install").Methods("POST").HandlerFunc(handler.UploadInitialAirgapApp)
handlers.RegisterTokenAuthRoutes(handler, debugRouter, loggingRouter)

/**********************************************************************
* Session auth routes
Expand Down
25 changes: 25 additions & 0 deletions pkg/handlers/handlers.go
Expand Up @@ -283,6 +283,31 @@ func JSON(w http.ResponseWriter, code int, payload interface{}) {
w.Write(response)
}

func RegisterTokenAuthRoutes(handler *Handler, debugRouter *mux.Router, loggingRouter *mux.Router) {
debugRouter.Path("/api/v1/kots/ports").Methods("GET").HandlerFunc(handler.GetApplicationPorts)
loggingRouter.Path("/api/v1/upload").Methods("PUT").HandlerFunc(handler.UploadExistingApp)
loggingRouter.Path("/api/v1/download").Methods("GET").HandlerFunc(handler.DownloadApp)
loggingRouter.Path("/api/v1/airgap/install").Methods("POST").HandlerFunc(handler.UploadInitialAirgapApp)
}

func RegisterUnauthenticatedRoutes(handler *Handler, debugRouter *mux.Router, loggingRouter *mux.Router) {
debugRouter.HandleFunc("/healthz", handler.Healthz)
loggingRouter.HandleFunc("/api/v1/login", handler.Login)
loggingRouter.HandleFunc("/api/v1/login/info", handler.GetLoginInfo)
loggingRouter.HandleFunc("/api/v1/logout", handler.Logout) // this route uses its own auth
loggingRouter.Path("/api/v1/metadata").Methods("GET").HandlerFunc(GetMetadataHandler(GetMetaDataConfig))

loggingRouter.HandleFunc("/api/v1/oidc/login", handler.OIDCLogin)
loggingRouter.HandleFunc("/api/v1/oidc/login/callback", handler.OIDCLoginCallback)

loggingRouter.Path("/api/v1/troubleshoot/{appId}/{bundleId}").Methods("PUT").HandlerFunc(handler.UploadSupportBundle)
loggingRouter.Path("/api/v1/troubleshoot/supportbundle/{bundleId}/redactions").Methods("PUT").HandlerFunc(handler.SetSupportBundleRedactions)
loggingRouter.Path("/api/v1/preflight/app/{appSlug}/sequence/{sequence}").Methods("POST").HandlerFunc(handler.PostPreflightStatus)

// This the handler for license API and should be called by the application only.
loggingRouter.Path("/license/v1/license").Methods("GET").HandlerFunc(handler.GetPlatformLicenseCompatibility)
}

func StreamJSON(c *websocket.Conn, payload interface{}) {
response, err := json.Marshal(payload)
if err != nil {
Expand Down

0 comments on commit 1ae7192

Please sign in to comment.