This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new 5b0280c  add Schema Disable Config #674 (#675)
5b0280c is described below

commit 5b0280cdbf672cfc872616850c30992fce3d65d6
Author: yankooo <[email protected]>
AuthorDate: Fri Aug 7 11:32:00 2020 +0800

    add Schema Disable Config #674 (#675)
    
    * add schema disable config
    
    * only forbid modify schema
---
 frontend/schema/schemahandler.go               | 13 ++++++++++++-
 frontend/server.go                             |  3 ++-
 server/core/config.go                          |  2 ++
 server/core/proto/types.go                     |  3 +++
 server/rest/controller/v4/schema_controller.go | 16 ++++++++++++++--
 5 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/frontend/schema/schemahandler.go b/frontend/schema/schemahandler.go
index 1a9a557..5a8f321 100644
--- a/frontend/schema/schemahandler.go
+++ b/frontend/schema/schemahandler.go
@@ -27,7 +27,18 @@ import (
        "github.com/labstack/echo"
 )
 
-func SchemaHandleFunc(c echo.Context) (err error) {
+type Mux struct {
+       // Disable represents frontend proxy service api or not
+       Disable bool
+}
+
+func (m *Mux) SchemaHandleFunc(c echo.Context) (err error) {
+       if m.Disable {
+               c.Response().WriteHeader(http.StatusForbidden)
+               _, _ = c.Response().Write([]byte("schema is disabled"))
+               return
+       }
+
        r := c.Request()
 
        //      protocol:= r.Header.Get("X-InstanceProtocol")
diff --git a/frontend/server.go b/frontend/server.go
index e527537..00c7623 100644
--- a/frontend/server.go
+++ b/frontend/server.go
@@ -38,7 +38,8 @@ func Serve(c Config) {
        staticPath := filepath.Join(dir, "app")
        e.Static("/", staticPath)
 
-       e.Any("/testSchema/*", schema.SchemaHandleFunc)
+       m := schema.Mux{Disable: os.Getenv("SCHEMA_DISABLE") == "true"}
+       e.Any("/testSchema/*", m.SchemaHandleFunc)
 
        scProxy(c, e)
 
diff --git a/server/core/config.go b/server/core/config.go
index 89d569c..b1ffa29 100644
--- a/server/core/config.go
+++ b/server/core/config.go
@@ -139,6 +139,8 @@ func newInfo() proto.ServerInformation {
                        ServiceClearEnabled:  
os.Getenv("SERVICE_CLEAR_ENABLED") == "true",
                        ServiceClearInterval: serviceClearInterval,
                        ServiceTTL:           serviceTTL,
+
+                       SchemaDisable: os.Getenv("SCHEMA_DISABLE") == "true",
                },
        }
 }
diff --git a/server/core/proto/types.go b/server/core/proto/types.go
index c301032..a2dc148 100644
--- a/server/core/proto/types.go
+++ b/server/core/proto/types.go
@@ -67,6 +67,9 @@ type ServerConfig struct {
        ServiceTTL time.Duration `json:"serviceTTL"`
        //CacheTTL is the ttl of cache
        CacheTTL time.Duration `json:"cacheTTL"`
+
+       // if want disable Test Schema, SchemaDisable set true
+       SchemaDisable bool `json:"schemaDisable"`
 }
 
 type ServerInformation struct {
diff --git a/server/rest/controller/v4/schema_controller.go 
b/server/rest/controller/v4/schema_controller.go
index e059794..cefc20d 100644
--- a/server/rest/controller/v4/schema_controller.go
+++ b/server/rest/controller/v4/schema_controller.go
@@ -36,13 +36,25 @@ type SchemaService struct {
 }
 
 func (s *SchemaService) URLPatterns() []rest.Route {
-       return []rest.Route{
+       var r = []rest.Route{
                {Method: rest.HTTPMethodGet, Path: 
"/v4/:project/registry/microservices/:serviceId/schemas/:schemaId", Func: 
s.GetSchemas},
-               {Method: rest.HTTPMethodPut, Path: 
"/v4/:project/registry/microservices/:serviceId/schemas/:schemaId", Func: 
s.ModifySchema},
                {Method: rest.HTTPMethodDelete, Path: 
"/v4/:project/registry/microservices/:serviceId/schemas/:schemaId", Func: 
s.DeleteSchemas},
                {Method: rest.HTTPMethodPost, Path: 
"/v4/:project/registry/microservices/:serviceId/schemas", Func: 
s.ModifySchemas},
                {Method: rest.HTTPMethodGet, Path: 
"/v4/:project/registry/microservices/:serviceId/schemas", Func: 
s.GetAllSchemas},
        }
+
+       if !core.ServerInfo.Config.SchemaDisable {
+               r = append(r, rest.Route{Method: rest.HTTPMethodPut, Path: 
"/v4/:project/registry/microservices/:serviceId/schemas/:schemaId", Func: 
s.ModifySchema})
+       } else {
+               r = append(r, rest.Route{Method: rest.HTTPMethodPut, Path: 
"/v4/:project/registry/microservices/:serviceId/schemas/:schemaId", Func: 
s.DisableSchema})
+       }
+
+       return r
+}
+
+func (s *SchemaService) DisableSchema(w http.ResponseWriter, r *http.Request) {
+       w.WriteHeader(http.StatusForbidden)
+       _, _ = w.Write([]byte("schema modify is disabled"))
 }
 
 func (s *SchemaService) GetSchemas(w http.ResponseWriter, r *http.Request) {

Reply via email to