| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- package router
- import (
- "github.com/go-chi/chi"
- "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/types"
- )
- func NewInfraScopedRegisterer(children ...*Registerer) *Registerer {
- return &Registerer{
- GetRoutes: GetInfraScopedRoutes,
- Children: children,
- }
- }
- func GetInfraScopedRoutes(
- r chi.Router,
- config *config.Config,
- basePath *types.Path,
- factory shared.APIEndpointFactory,
- children ...*Registerer,
- ) []*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,
- ) ([]*Route, *types.Path) {
- relPath := "/infras/{infra_id}"
- newPath := &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- }
- routes := make([]*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.GetResultWriter(),
- )
- routes = append(routes, &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, &Route{
- Endpoint: getEndpoint,
- Handler: getHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id}/logs -> infra.NewInfraStreamLogsHandler
- streamLogsEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/logs",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- IsWebsocket: true,
- },
- )
- streamLogsHandler := infra.NewInfraStreamLogsHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &Route{
- Endpoint: streamLogsEndpoint,
- Handler: streamLogsHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id}/current -> infra.NewInfraGetHandler
- getCurrentEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/current",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- getCurrentHandler := infra.NewInfraGetCurrentHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &Route{
- Endpoint: getCurrentEndpoint,
- Handler: getCurrentHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/infras/{infra_id}/desired -> infra.NewInfraGetHandler
- getDesiredEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/desired",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.InfraScope,
- },
- },
- )
- getDesiredHandler := infra.NewInfraGetDesiredHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &Route{
- Endpoint: getDesiredEndpoint,
- Handler: getDesiredHandler,
- 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, &Route{
- Endpoint: deleteEndpoint,
- Handler: deleteHandler,
- Router: r,
- })
- return routes, newPath
- }
|