Skip to content

Commit

Permalink
[rest] Standalone admin api supports retrieve operations only in clus…
Browse files Browse the repository at this point in the history
…ter mode
  • Loading branch information
zhiyanliu authored and Jack47 committed Dec 21, 2017
1 parent 0c021c1 commit c3566fe
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
30 changes: 30 additions & 0 deletions doc/admin_api_ref.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ paths:
Plugin instance with the given name request parameter provided is already existing.
schema:
$ref: '#/definitions/Error'
503:
description: |
Non-retrieve operations are disallowed when running in cluster mode.
schema:
$ref: '#/definitions/Error'
200:
description: Plugin instance is created successfully.
get:
Expand Down Expand Up @@ -110,6 +115,11 @@ paths:
description: Plugin instance not found by given name.
schema:
$ref: '#/definitions/Error'
503:
description: |
Non-retrieve operations are disallowed when running in cluster mode.
schema:
$ref: '#/definitions/Error'
200:
description: Plugin instance is updated successfully.
/plugins/{pluginName}:
Expand Down Expand Up @@ -163,6 +173,11 @@ paths:
one or more pipelines.
schema:
$ref: '#/definitions/Error'
503:
description: |
Non-retrieve operations are disallowed when running in cluster mode.
schema:
$ref: '#/definitions/Error'
200:
description: Plugin instance is deleted successfully.
/pipelines:
Expand Down Expand Up @@ -191,6 +206,11 @@ paths:
Pipeline instance with the given name request parameter provided is already existing.
schema:
$ref: '#/definitions/Error'
503:
description: |
Non-retrieve operations are disallowed when running in cluster mode.
schema:
$ref: '#/definitions/Error'
200:
description: Pipeline instance is created successfully.
get:
Expand Down Expand Up @@ -238,6 +258,11 @@ paths:
pipeline are not found.
schema:
$ref: '#/definitions/Error'
503:
description: |
Non-retrieve operations are disallowed when running in cluster mode.
schema:
$ref: '#/definitions/Error'
200:
description: Pipeline instance is updated successfully.
/pipelines/{pipelineName}:
Expand Down Expand Up @@ -285,6 +310,11 @@ paths:
description: Pipeline instance not found by given name.
schema:
$ref: '#/definitions/Error'
503:
description: |
Non-retrieve operations are disallowed when running in cluster mode.
schema:
$ref: '#/definitions/Error'
200:
description: Pipeline instance is deleted successfully.
definitions:
Expand Down
7 changes: 5 additions & 2 deletions src/rest/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ant0ine/go-json-rest/rest"

"cluster/gateway"
"common"
"config"
"engine"
Expand All @@ -20,11 +21,13 @@ import (

type adminServer struct {
gateway *engine.Gateway
gc *gateway.GatewayCluster
}

func newAdminServer(gateway *engine.Gateway) (*adminServer, error) {
func newAdminServer(gateway *engine.Gateway, gc *gateway.GatewayCluster) (*adminServer, error) {
return &adminServer{
gateway: gateway,
gc: gc,
}, nil
}

Expand Down Expand Up @@ -53,7 +56,7 @@ func (s *adminServer) Api() (*rest.Api, error) {
}

api := rest.NewApi()
api.Use(RestStack...)
api.Use(append(RestStack, &standaloneAvailabilityMiddleware{gc: s.gc})...)
api.SetApp(router)

return api, nil
Expand Down
21 changes: 19 additions & 2 deletions src/rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,24 @@ type clusterAvailabilityMiddleware struct {

func (cam *clusterAvailabilityMiddleware) MiddlewareFunc(h rest.HandlerFunc) rest.HandlerFunc {
return func(w rest.ResponseWriter, r *rest.Request) {
if cam.gc == nil {
if cam.gc == nil || cam.gc.Stopped() {
rest.Error(w, "service unavailable", http.StatusServiceUnavailable)
return
}

// call the handler
h(w, r)
}
}

type standaloneAvailabilityMiddleware struct {
gc *gateway.GatewayCluster
}

func (sam *standaloneAvailabilityMiddleware) MiddlewareFunc(h rest.HandlerFunc) rest.HandlerFunc {
return func(w rest.ResponseWriter, r *rest.Request) {
if sam.gc != nil && !sam.gc.Stopped() && r.Method != "GET" {
// standalone admin api only supports retrieve operations when running in cluster mode
rest.Error(w, "service unavailable", http.StatusServiceUnavailable)
return
}
Expand Down Expand Up @@ -75,7 +92,7 @@ func (s *Rest) Start() (<-chan error, string, error) {

listenAddr := fmt.Sprintf("%s:9090", option.RestHost)

adminServer, err := newAdminServer(s.gateway)
adminServer, err := newAdminServer(s.gateway, s.gc)
if err != nil {
logger.Errorf("[create admin rest server failed: %v]", err)
return nil, listenAddr, err
Expand Down

0 comments on commit c3566fe

Please sign in to comment.