/
handlers.go
75 lines (65 loc) · 2.85 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package api
import (
"database/sql"
"fmt"
"net/http"
// Blank import needed for swagger doc
_ "github.com/cloudfoundry/go-cf-api/internal/api/docs"
"github.com/cloudfoundry/go-cf-api/internal/api/health"
v3 "github.com/cloudfoundry/go-cf-api/internal/api/v3"
"github.com/cloudfoundry/go-cf-api/internal/api/v3/buildpacks"
"github.com/cloudfoundry/go-cf-api/internal/api/v3/info"
"github.com/cloudfoundry/go-cf-api/internal/api/v3/securitygroups"
"github.com/cloudfoundry/go-cf-api/internal/apicommon/v3/auth"
"github.com/cloudfoundry/go-cf-api/internal/apicommon/v3/metadata"
"github.com/cloudfoundry/go-cf-api/internal/apicommon/v3/permissions"
"github.com/cloudfoundry/go-cf-api/internal/config"
"github.com/labstack/echo/v4"
echoSwagger "github.com/swaggo/echo-swagger"
)
func RegisterHandlers(
echoInstance *echo.Echo,
database *sql.DB,
jwtMiddleware echo.MiddlewareFunc,
rateLimitMiddleware echo.MiddlewareFunc,
conf *config.CfAPIConfig,
) {
// Health Endpoint
echoInstance.GET("healthz", health.GetHealth)
// Info Endpoint
echoInstance.GET("/", NewRootEndpoint(conf))
// V3 API
echoInstance.GET("/v3", v3.NewV3RootEndpoint(conf))
registerV3Handlers(echoInstance, database, jwtMiddleware, rateLimitMiddleware, conf)
// Serve V3 Swagger-UI API Docs
echoInstance.GET("docs/v3", func(c echo.Context) error {
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/%s/index.html", "docs/v3"))
})
echoInstance.GET(fmt.Sprintf("%s/", "docs/v3"), func(c echo.Context) error {
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/%s/index.html", "docs/v3"))
})
echoInstance.GET(fmt.Sprintf("%s/*", "docs/v3"), echoSwagger.WrapHandler)
}
func registerV3Handlers(echoInstance *echo.Echo,
database *sql.DB,
jwtMiddleware echo.MiddlewareFunc,
rateLimitMiddleware echo.MiddlewareFunc,
conf *config.CfAPIConfig) {
v3Root := echoInstance.Group("v3/", jwtMiddleware)
if conf.RateLimit.Enabled {
v3Root.Use(rateLimitMiddleware)
}
requiresRead := v3Root.Group("", auth.NewRequiresReadMiddleware())
requiresWrite := v3Root.Group("", auth.NewRequiresWriteMiddleware())
// Info
echoInstance.GET("/v3/info", info.NewV3InfoEndpoint(conf))
// Buildpacks
buildpacksController := buildpacks.Controller{DB: database, Presenter: buildpacks.NewPresenter(), LabelSelectorParser: metadata.NewLabelSelectorParser()}
requiresRead.GET("buildpacks", buildpacksController.List)
requiresRead.GET(fmt.Sprintf("buildpacks/:%s", buildpacks.GUIDParam), buildpacksController.Get)
requiresWrite.POST("buildpacks", buildpacksController.Post)
// Security Groups
securityGroupsController := securitygroups.Controller{DB: database, Presenter: securitygroups.NewPresenter(), Permissions: permissions.NewQuerier()}
requiresRead.GET(fmt.Sprintf("security_groups/:%s", securitygroups.GUIDParam), securityGroupsController.Get)
requiresRead.GET("security_groups", securityGroupsController.List)
}