Skip to content

Commit

Permalink
Refactors API service for API versioning
Browse files Browse the repository at this point in the history
This refactors API service code to maintain seperate directory for each
user facing APIs.

Signed-off-by: Shivam Mukhade <[email protected]>
  • Loading branch information
SM43 committed Feb 11, 2021
1 parent 2aae805 commit 68f1ac2
Show file tree
Hide file tree
Showing 85 changed files with 14,300 additions and 2,417 deletions.
7 changes: 6 additions & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ RUN apk --no-cache add git ca-certificates && addgroup -S hub && adduser -S hub
USER hub

WORKDIR /app

COPY --from=builder /go/src/github.com/tektoncd/hub/api/api-server /app/api-server
COPY --from=builder /go/src/github.com/tektoncd/hub/api/gen/http/openapi3.yaml /app/gen/http/openapi3.yaml

# For each new version, doc has to be copied
COPY gen/http/openapi3.yaml /app/gen/http/openapi3.yaml
COPY v1/gen/http/openapi3.yaml /app/v1/gen/http/openapi3.yaml

EXPOSE 8000

CMD [ "/app/api-server" ]
34 changes: 26 additions & 8 deletions api/cmd/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ 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"
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 @@ -54,6 +57,7 @@ func handleHTTPServer(
categoryEndpoints *category.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,14 +90,16 @@ 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
ratingServer *ratingsvr.Server
resourceServer *resourcesvr.Server
v1resourceServer *v1resourcesvr.Server
statusServer *statussvr.Server
swaggerServer *swaggersvr.Server
v1swaggerServer *v1swaggersvr.Server
)
{
eh := errorHandler(logger)
Expand All @@ -103,8 +109,10 @@ func handleHTTPServer(
categoryServer = categorysvr.New(categoryEndpoints, 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{
Expand All @@ -114,8 +122,10 @@ func handleHTTPServer(
categoryServer,
ratingServer,
resourceServer,
v1resourceServer,
statusServer,
swaggerServer,
v1swaggerServer,
}
servers.Use(httpmdlwr.Debug(mux, os.Stdout))
}
Expand All @@ -127,8 +137,10 @@ func handleHTTPServer(
categorysvr.Mount(mux, categoryServer)
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 Down Expand Up @@ -159,12 +171,18 @@ func handleHTTPServer(
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
35 changes: 21 additions & 14 deletions api/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ 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"
v1resource "github.com/tektoncd/hub/api/v1/gen/resource"
v1resourcesvc "github.com/tektoncd/hub/api/v1/service/resource"
)

func main() {
Expand Down Expand Up @@ -79,13 +81,14 @@ 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
ratingSvc rating.Service
resourceSvc resource.Service
v1resourceSvc v1resource.Service
statusSvc status.Service
)
{
adminSvc = adminsvc.New(api)
Expand All @@ -94,19 +97,21 @@ func main() {
categorySvc = categorysvc.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
ratingEndpoints *rating.Endpoints
resourceEndpoints *resource.Endpoints
v1resourceEndpoints *v1resource.Endpoints
statusEndpoints *status.Endpoints
)
{
adminEndpoints = admin.NewEndpoints(adminSvc)
Expand All @@ -115,6 +120,7 @@ func main() {
categoryEndpoints = category.NewEndpoints(categorySvc)
ratingEndpoints = rating.NewEndpoints(ratingSvc)
resourceEndpoints = resource.NewEndpoints(resourceSvc)
v1resourceEndpoints = v1resource.NewEndpoints(v1resourceSvc)
statusEndpoints = status.NewEndpoints(statusSvc)
}

Expand Down Expand Up @@ -163,6 +169,7 @@ func main() {
categoryEndpoints,
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
4 changes: 2 additions & 2 deletions api/design/category.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,12 +27,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 68f1ac2

Please sign in to comment.