Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563494858



##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package data_loader
+
+import (
+       "context"
+       "encoding/json"
+       "fmt"
+       "net/http"
+       "reflect"
+       "strings"
+
+       "github.com/apisix/manager-api/internal/core/entity"
+       "github.com/apisix/manager-api/internal/core/store"
+       "github.com/apisix/manager-api/internal/handler"
+       "github.com/apisix/manager-api/internal/log"
+       "github.com/apisix/manager-api/internal/utils"
+       "github.com/getkin/kin-openapi/openapi3"
+       "github.com/gin-gonic/gin"
+       "github.com/shiningrush/droplet"
+       "github.com/shiningrush/droplet/data"
+       "github.com/shiningrush/droplet/wrapper"
+       wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+       routeStore    store.Interface
+       upstreamStore store.Interface
+       serviceStore  store.Interface
+       consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+       return &Handler{
+               routeStore:    store.GetStore(store.HubKeyRoute),
+               upstreamStore: store.GetStore(store.HubKeyUpstream),
+               serviceStore:  store.GetStore(store.HubKeyService),
+               consumerStore: store.GetStore(store.HubKeyConsumer),
+       }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+       r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+               wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+       r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+       IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+       input := c.Input().(*ExportInput)
+       ids := strings.Split(input.IDs, ",")
+       routes := []*entity.Route{}
+
+       for _, id := range ids {
+               route, err := h.routeStore.Get(context.Background(), id)
+               if err != nil {
+                       return nil, err
+               }
+               routes = append(routes, route.(*entity.Route))
+       }
+
+       swagger, err := h.routeToOpenApi3(routes)
+       if err != nil {
+               return nil, err
+       }
+       return swagger, nil
+}
+
+type AuthType string
+
+const (
+       BasicAuth AuthType = "basic-auth"
+       KeyAuth   AuthType = "key-auth"
+       JWTAuth   AuthType = "jwt-auth"
+)
+
+var (
+       openApi = "3.0.0"
+       title   = "RoutesExport"
+       service interface{}
+       err     error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+       routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+       if err != nil {
+               return nil, err
+       }
+
+       routes := []*entity.Route{}
+
+       for _, route := range routelist.Rows {
+               routes = append(routes, route.(*entity.Route))
+       }
+
+       swagger, err := h.routeToOpenApi3(routes)
+       if err != nil {
+               return nil, err
+       }
+       return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, 
error) {
+       paths := openapi3.Paths{}
+       paramsRefs := []*openapi3.ParameterRef{}
+       requestBody := &openapi3.RequestBody{}
+       components := &openapi3.Components{}
+       secSchemas := openapi3.SecuritySchemes{}
+       servicePlugins := make(map[string]interface{})
+       plugins := make(map[string]interface{})
+       serviceLabels := make(map[string]string)
+       labels := make(map[string]string)
+
+       for _, route := range routes {
+               extensions := make(map[string]interface{})
+               pathItem := &openapi3.PathItem{}
+               path := openapi3.Operation{}
+               path.Summary = route.Desc
+               path.OperationID = route.Name
+
+               if route.ServiceID != nil {
+                       serviceID := utils.InterfaceToString(route.ServiceID)
+                       service, err = h.serviceStore.Get(context.Background(), 
serviceID)
+                       if err != nil {
+                               if err == data.ErrNotFound {
+                                       return nil, fmt.Errorf("service id: %s 
not found", route.ServiceID)
+                               }
+                               return nil, err
+                       }
+
+                       _service := service.(*entity.Service)
+                       servicePlugins = _service.Plugins
+                       serviceLabels = _service.Labels
+               }
+
+               if route.Upstream != nil {
+                       extensions["x-apisix-upstream"] = route.Upstream
+               } else if route.UpstreamID != nil && route.Upstream == nil {
+                       upstreamID := utils.InterfaceToString(route.UpstreamID)
+                       upstream, err := 
h.upstreamStore.Get(context.Background(), upstreamID)
+                       if err != nil {
+                               if err == data.ErrNotFound {
+                                       return nil, fmt.Errorf("upstream id: %s 
not found", route.UpstreamID)
+                               }
+                               return nil, err
+                       }
+                       extensions["x-apisix-upstream"] = upstream
+               } else if route.UpstreamID == nil && route.Upstream == nil && 
route.ServiceID != nil {
+                       _service := service.(*entity.Service)
+                       if _service.Upstream != nil {
+                               extensions["x-apisix-upstream"] = 
_service.Upstream
+                       } else if _service.Upstream == nil && 
_service.UpstreamID != nil {
+                               upstreamID := 
utils.InterfaceToString(_service.UpstreamID)
+                               upstream, err := 
h.upstreamStore.Get(context.Background(), upstreamID)
+                               if err != nil {
+                                       if err == data.ErrNotFound {
+                                               return nil, 
fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+                                       }
+                                       return nil, err
+                               }
+                               extensions["x-apisix-upstream"] = upstream
+                       }
+               }
+
+               if route.Host != "" {
+                       extensions["x-apisix-host"] = route.Host
+               }
+
+               if route.Hosts != nil {
+                       extensions["x-apisix-hosts"] = route.Hosts
+               }
+
+               //Parse Labels
+               labels, err = parseLabels(route, serviceLabels, labels)
+               if err != nil {
+                       log.Errorf("parseLabels err: ", err)
+                       return nil, err
+               }
+
+               if labels != nil {
+                       extensions["x-apisix-labels"] = labels
+               }
+
+               if route.RemoteAddr != "" {
+                       extensions["x-apisix-remote_addr"] = route.RemoteAddr
+               }
+
+               if route.RemoteAddrs != nil {
+                       extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+               }
+
+               if route.FilterFunc != "" {
+                       extensions["x-apisix-filter_func"] = route.FilterFunc
+               }
+
+               if route.Script != nil {
+                       extensions["x-apisix-script"] = route.Script
+               }
+
+               if route.ServiceProtocol != "" {
+                       extensions["x-apisix-service_protocol"] = 
route.ServiceProtocol
+               }
+               if route.Vars != nil {
+                       extensions["x-apisix-vars"] = route.Vars
+               }
+
+               // analysis route.URIs
+               routeURIs := []string{}
+               if route.URI != "" {
+                       routeURIs = append(routeURIs, route.URI)
+               }
+
+               if route.Uris != nil {
+                       routeURIs = route.Uris
+               }
+
+               for _, uri := range routeURIs {
+                       if strings.Contains(uri, "*") {
+                               paths[strings.Split(uri, "*")[0]+"{params}"] = 
pathItem
+                               // add params introduce
+                               paramsRefs = append(paramsRefs, 
&openapi3.ParameterRef{
+                                       Value: &openapi3.Parameter{
+                                               In:          "path",
+                                               Name:        "params",
+                                               Required:    true,
+                                               Description: "params in path",
+                                               Schema:      
&openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+                       } else {
+                               paths[uri] = pathItem
+                       }
+               }

Review comment:
       No problem, but there are some problems that need to be solved by 
voting. I'll deal with them after the results come out




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to