Skip to content

Commit

Permalink
Refactors API Service for API versioning
Browse files Browse the repository at this point in the history
Signed-off-by: Shivam Mukhade <[email protected]>
  • Loading branch information
SM43 committed Feb 10, 2021
1 parent f645eea commit f2a54cc
Show file tree
Hide file tree
Showing 102 changed files with 15,235 additions and 2,270 deletions.
44 changes: 36 additions & 8 deletions api/cmd/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ import (
rating "github.com/tektoncd/hub/api/gen/rating"
resource "github.com/tektoncd/hub/api/gen/resource"
status "github.com/tektoncd/hub/api/gen/status"
v1category "github.com/tektoncd/hub/api/v1/gen/category"
v1categorysvr "github.com/tektoncd/hub/api/v1/gen/http/category/server"
v1resourcesvr "github.com/tektoncd/hub/api/v1/gen/http/resource/server"
v1swaggersvr "github.com/tektoncd/hub/api/v1/gen/http/swagger/server"
v1resource "github.com/tektoncd/hub/api/v1/gen/resource"
)

// handleHTTPServer starts configures and starts a HTTP server on the given
Expand All @@ -52,8 +57,10 @@ func handleHTTPServer(
authEndpoints *auth.Endpoints,
catalogEndpoints *catalog.Endpoints,
categoryEndpoints *category.Endpoints,
v1categoryEndpoints *v1category.Endpoints,
ratingEndpoints *rating.Endpoints,
resourceEndpoints *resource.Endpoints,
v1resourceEndpoints *v1resource.Endpoints,
statusEndpoints *status.Endpoints,
wg *sync.WaitGroup, errc chan error, logger *log.Logger, debug bool) {

Expand Down Expand Up @@ -86,36 +93,45 @@ func handleHTTPServer(
// the service input and output data structures to HTTP requests and
// responses.
var (
adminServer *adminsvr.Server
authServer *authsvr.Server
catalogServer *catalogsvr.Server
categoryServer *categorysvr.Server
ratingServer *ratingsvr.Server
resourceServer *resourcesvr.Server
statusServer *statussvr.Server
swaggerServer *swaggersvr.Server
adminServer *adminsvr.Server
authServer *authsvr.Server
catalogServer *catalogsvr.Server
categoryServer *categorysvr.Server
v1categoryServer *v1categorysvr.Server
ratingServer *ratingsvr.Server
resourceServer *resourcesvr.Server
v1resourceServer *v1resourcesvr.Server
statusServer *statussvr.Server
swaggerServer *swaggersvr.Server
v1swaggerServer *v1swaggersvr.Server
)
{
eh := errorHandler(logger)
adminServer = adminsvr.New(adminEndpoints, mux, dec, enc, eh, nil)
authServer = authsvr.New(authEndpoints, mux, dec, enc, eh, nil)
catalogServer = catalogsvr.New(catalogEndpoints, mux, dec, enc, eh, nil)
categoryServer = categorysvr.New(categoryEndpoints, mux, dec, enc, eh, nil)
v1categoryServer = v1categorysvr.New(v1categoryEndpoints, mux, dec, enc, eh, nil)
ratingServer = ratingsvr.New(ratingEndpoints, mux, dec, enc, eh, nil)
resourceServer = resourcesvr.New(resourceEndpoints, mux, dec, enc, eh, nil)
v1resourceServer = v1resourcesvr.New(v1resourceEndpoints, mux, dec, enc, eh, nil)
statusServer = statussvr.New(statusEndpoints, mux, dec, enc, eh, nil)
swaggerServer = swaggersvr.New(nil, mux, dec, enc, eh, nil)
v1swaggerServer = v1swaggersvr.New(nil, mux, dec, enc, eh, nil)

if debug {
servers := goahttp.Servers{
adminServer,
authServer,
catalogServer,
categoryServer,
v1categoryServer,
ratingServer,
resourceServer,
v1resourceServer,
statusServer,
swaggerServer,
v1swaggerServer,
}
servers.Use(httpmdlwr.Debug(mux, os.Stdout))
}
Expand All @@ -125,10 +141,13 @@ func handleHTTPServer(
authsvr.Mount(mux, authServer)
catalogsvr.Mount(mux, catalogServer)
categorysvr.Mount(mux, categoryServer)
v1categorysvr.Mount(mux, v1categoryServer)
ratingsvr.Mount(mux, ratingServer)
resourcesvr.Mount(mux, resourceServer)
v1resourcesvr.Mount(mux, v1resourceServer)
statussvr.Mount(mux, statusServer)
swaggersvr.Mount(mux, swaggerServer)
v1swaggersvr.Mount(mux, v1swaggerServer)

// Wrap the multiplexer with additional middlewares. Middlewares mounted
// here apply to all the service endpoints.
Expand All @@ -153,18 +172,27 @@ func handleHTTPServer(
for _, m := range categoryServer.Mounts {
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
for _, m := range v1categoryServer.Mounts {
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
for _, m := range ratingServer.Mounts {
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
for _, m := range resourceServer.Mounts {
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
for _, m := range v1resourceServer.Mounts {
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
for _, m := range statusServer.Mounts {
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
for _, m := range swaggerServer.Mounts {
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
for _, m := range v1swaggerServer.Mounts {
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}

(*wg).Add(1)
go func() {
Expand Down
42 changes: 28 additions & 14 deletions api/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import (
ratingsvc "github.com/tektoncd/hub/api/pkg/service/rating"
resourcesvc "github.com/tektoncd/hub/api/pkg/service/resource"
statussvc "github.com/tektoncd/hub/api/pkg/service/status"
v1category "github.com/tektoncd/hub/api/v1/gen/category"
v1resource "github.com/tektoncd/hub/api/v1/gen/resource"
v1categorysvc "github.com/tektoncd/hub/api/v1/service/category"
v1resourcesvc "github.com/tektoncd/hub/api/v1/service/resource"
)

func main() {
Expand Down Expand Up @@ -79,42 +83,50 @@ func main() {

// Initialize the services.
var (
adminSvc admin.Service
authSvc auth.Service
catalogSvc catalog.Service
categorySvc category.Service
ratingSvc rating.Service
resourceSvc resource.Service
statusSvc status.Service
adminSvc admin.Service
authSvc auth.Service
catalogSvc catalog.Service
categorySvc category.Service
v1categorySvc v1category.Service
ratingSvc rating.Service
resourceSvc resource.Service
v1resourceSvc v1resource.Service
statusSvc status.Service
)
{
adminSvc = adminsvc.New(api)
authSvc = authsvc.New(api)
catalogSvc = catalogsvc.New(api)
categorySvc = categorysvc.New(api)
v1categorySvc = v1categorysvc.New(api)
ratingSvc = ratingsvc.New(api)
resourceSvc = resourcesvc.New(api)
v1resourceSvc = v1resourcesvc.New(api)
statusSvc = statussvc.New(api)
}

// Wrap the services in endpoints that can be invoked from other services
// potentially running in different processes.
var (
adminEndpoints *admin.Endpoints
authEndpoints *auth.Endpoints
catalogEndpoints *catalog.Endpoints
categoryEndpoints *category.Endpoints
ratingEndpoints *rating.Endpoints
resourceEndpoints *resource.Endpoints
statusEndpoints *status.Endpoints
adminEndpoints *admin.Endpoints
authEndpoints *auth.Endpoints
catalogEndpoints *catalog.Endpoints
categoryEndpoints *category.Endpoints
v1categoryEndpoints *v1category.Endpoints
ratingEndpoints *rating.Endpoints
resourceEndpoints *resource.Endpoints
v1resourceEndpoints *v1resource.Endpoints
statusEndpoints *status.Endpoints
)
{
adminEndpoints = admin.NewEndpoints(adminSvc)
authEndpoints = auth.NewEndpoints(authSvc)
catalogEndpoints = catalog.NewEndpoints(catalogSvc)
categoryEndpoints = category.NewEndpoints(categorySvc)
v1categoryEndpoints = v1category.NewEndpoints(v1categorySvc)
ratingEndpoints = rating.NewEndpoints(ratingSvc)
resourceEndpoints = resource.NewEndpoints(resourceSvc)
v1resourceEndpoints = v1resource.NewEndpoints(v1resourceSvc)
statusEndpoints = status.NewEndpoints(statusSvc)
}

Expand Down Expand Up @@ -161,8 +173,10 @@ func main() {
authEndpoints,
catalogEndpoints,
categoryEndpoints,
v1categoryEndpoints,
ratingEndpoints,
resourceEndpoints,
v1resourceEndpoints,
statusEndpoints,
&wg, errc, api.Logger("http"), *dbgF,
)
Expand Down
5 changes: 3 additions & 2 deletions api/design/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package design

import (
"github.com/tektoncd/hub/api/design/types"
. "goa.design/goa/v3/dsl"
)

Expand All @@ -28,7 +29,7 @@ var _ = Service("admin", func() {

Method("UpdateAgent", func() {
Description("Create or Update an agent user with required scopes")
Security(JWTAuth, func() {
Security(types.JWTAuth, func() {
Scope("agent:create")
})
Payload(func() {
Expand Down Expand Up @@ -56,7 +57,7 @@ var _ = Service("admin", func() {

Method("RefreshConfig", func() {
Description("Refresh the changes in config file")
Security(JWTAuth, func() {
Security(types.JWTAuth, func() {
Scope("config:refresh")
})
Payload(func() {
Expand Down
3 changes: 2 additions & 1 deletion api/design/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package design

import (
"github.com/tektoncd/hub/api/design/types"
. "goa.design/goa/v3/dsl"
)

Expand All @@ -35,7 +36,7 @@ var _ = Service("auth", func() {
Required("code")
})
Result(func() {
Attribute("data", AuthTokens, "User Tokens")
Attribute("data", types.AuthTokens, "User Tokens")
Required("data")
})

Expand Down
5 changes: 3 additions & 2 deletions api/design/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package design

import (
"github.com/tektoncd/hub/api/design/types"
. "goa.design/goa/v3/dsl"
)

Expand All @@ -26,14 +27,14 @@ var _ = Service("catalog", func() {

Method("Refresh", func() {
Description("Refreshes Tekton Catalog")
Security(JWTAuth, func() {
Security(types.JWTAuth, func() {
Scope("catalog:refresh")
})
Payload(func() {
Token("token", String, "JWT")
Required("token")
})
Result(Job)
Result(types.Job)

HTTP(func() {
POST("/catalog/refresh")
Expand Down
6 changes: 4 additions & 2 deletions api/design/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package design

import (
"github.com/tektoncd/hub/api/design/types"
. "goa.design/goa/v3/dsl"
)

// NOTE: APIs in the service are moved to v1. This API will be deprecated in the next release.

var _ = Service("category", func() {
Description("The category service provides details about category")

Expand All @@ -26,12 +29,11 @@ var _ = Service("category", func() {
Method("list", func() {
Description("List all categories along with their tags sorted by name")
Result(func() {
Attribute("data", ArrayOf(Category))
Attribute("data", ArrayOf(types.Category))
})

HTTP(func() {
GET("/categories")
GET("/v1/categories")

Response(StatusOK)
Response("internal-error", StatusInternalServerError)
Expand Down
5 changes: 3 additions & 2 deletions api/design/rating.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package design

import (
"github.com/tektoncd/hub/api/design/types"
. "goa.design/goa/v3/dsl"
)

Expand All @@ -28,7 +29,7 @@ var _ = Service("rating", func() {

Method("Get", func() {
Description("Find user's rating for a resource")
Security(JWTAuth, func() {
Security(types.JWTAuth, func() {
Scope("rating:read")
})
Payload(func() {
Expand Down Expand Up @@ -57,7 +58,7 @@ var _ = Service("rating", func() {

Method("Update", func() {
Description("Update user's rating for a resource")
Security(JWTAuth, func() {
Security(types.JWTAuth, func() {
Scope("rating:write")
})
Payload(func() {
Expand Down
Loading

0 comments on commit f2a54cc

Please sign in to comment.