| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- package router
- import (
- "fmt"
- "github.com/go-chi/chi/v5"
- "github.com/porter-dev/porter/api/server/handlers/database"
- "github.com/porter-dev/porter/api/server/handlers/infra"
- "github.com/porter-dev/porter/api/server/shared"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/server/shared/router"
- "github.com/porter-dev/porter/api/types"
- )
- func NewInfraScopedRegisterer(children ...*router.Registerer) *router.Registerer {
- return &router.Registerer{
- GetRoutes: GetInfraScopedRoutes,
- Children: children,
- }
- }
- func GetInfraScopedRoutes(
- r chi.Router,
- config *config.Config,
- basePath *types.Path,
- factory shared.APIEndpointFactory,
- children ...*router.Registerer,
- ) []*router.Route {
- routes, projPath := getInfraRoutes(r, config, basePath, factory)
- if len(children) > 0 {
- r.Route(projPath.RelativePath, func(r chi.Router) {
- for _, child := range children {
- childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
- routes = append(routes, childRoutes...)
- }
- })
- }
- return routes
- }
- func getInfraRoutes(
- r chi.Router,
- config *config.Config,
- basePath *types.Path,
- factory shared.APIEndpointFactory,
- ) ([]*router.Route, *types.Path) {
- relPath := "/infras/{infra_id}"
- newPath := &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- }
- routes := make([]*router.Route, 0)
- // GET /api/projects/{project_id}/infra -> project.NewInfraListHandler
- listInfraEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: "/infra",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- },
- },
- )
- listInfraHandler := infra.NewInfraListHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: listInfraEndpoint,
- Handler: listInfraHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id} -> infra.NewInfraGetHandler
- getEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- getHandler := infra.NewInfraGetHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: getEndpoint,
- Handler: getHandler,
- Router: r,
- })
- // POST /api/projects/{project_id}/infras/{infra_id}/retry_create -> infra.NewInfraRetryHandler
- retryCreateEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbUpdate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/retry_create",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- retryCreateHandler := infra.NewInfraRetryCreateHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: retryCreateEndpoint,
- Handler: retryCreateHandler,
- Router: r,
- })
- // POST /api/projects/{project_id}/infras/{infra_id}/update -> infra.NewInfraUpdateHandler
- updateEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbUpdate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/update",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- updateHandler := infra.NewInfraUpdateHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: updateEndpoint,
- Handler: updateHandler,
- Router: r,
- })
- // POST /api/projects/{project_id}/infras/{infra_id}/retry_delete -> infra.NewInfraRetryDeleteHandler
- retryDeleteEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbUpdate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/retry_delete",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- retryDeleteHandler := infra.NewInfraRetryDeleteHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: retryDeleteEndpoint,
- Handler: retryDeleteHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id}/operations -> infra.NewInfraListOperationsHandler
- listOperationsEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbList,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/operations",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- listOperationsHandler := infra.NewInfraListOperationsHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: listOperationsEndpoint,
- Handler: listOperationsHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id}/operations/{operation_id} -> infra.NewInfraGetOperationHandler
- getOperationEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: fmt.Sprintf("%s/operations/{%s}", relPath, types.URLParamOperationID),
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- types.OperationScope,
- },
- },
- )
- getOperationHandler := infra.NewInfraGetOperationHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: getOperationEndpoint,
- Handler: getOperationHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id}/operations/{operation_id}/state -> infra.NewInfraStreamStateHandler
- streamStateEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: fmt.Sprintf("%s/operations/{%s}/state", relPath, types.URLParamOperationID),
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- types.OperationScope,
- },
- IsWebsocket: true,
- },
- )
- streamStateHandler := infra.NewInfraStreamStateHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: streamStateEndpoint,
- Handler: streamStateHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id}/operations/{operation_id}/log_stream -> infra.NewInfraStreamLogHandler
- streamLogEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: fmt.Sprintf("%s/operations/{%s}/log_stream", relPath, types.URLParamOperationID),
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- types.OperationScope,
- },
- IsWebsocket: true,
- },
- )
- streamLogHandler := infra.NewInfraStreamLogHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: streamLogEndpoint,
- Handler: streamLogHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id}/operations/{operation_id}/logs -> infra.NewInfraGetOperationLogsHandler
- getOperationLogsEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: fmt.Sprintf("%s/operations/{%s}/logs", relPath, types.URLParamOperationID),
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- types.OperationScope,
- },
- },
- )
- getOperationLogsHandler := infra.NewInfraGetOperationLogsHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: getOperationLogsEndpoint,
- Handler: getOperationLogsHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id}/state -> infra.NewInfraGetStateHandler
- getStateEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/state",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- getStateHandler := infra.NewInfraGetStateHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: getStateEndpoint,
- Handler: getStateHandler,
- Router: r,
- })
- // DELETE /api/projects/{project_id}/infras/{infra_id} -> infra.NewInfraDeleteHandler
- deleteEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbDelete,
- Method: types.HTTPVerbDelete,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- deleteHandler := infra.NewInfraDeleteHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: deleteEndpoint,
- Handler: deleteHandler,
- Router: r,
- })
- // POST /api/projects/{project_id}/infras/{infra_id}/database -> database.NewDatabaseUpdateStatusHandler
- updateDBStatusEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbUpdate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/database",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- updateDBStatusHandler := database.NewDatabaseUpdateStatusHandler(
- config,
- factory.GetDecoderValidator(),
- )
- routes = append(routes, &router.Route{
- Endpoint: updateDBStatusEndpoint,
- Handler: updateDBStatusHandler,
- Router: r,
- })
- return routes, newPath
- }
|