tokers commented on a change in pull request #1102: URL: https://github.com/apache/apisix-dashboard/pull/1102#discussion_r557885773
########## File path: api/internal/handler/data_loader/import.go ########## @@ -0,0 +1,468 @@ +/* + * 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 ( + "bufio" + "encoding/json" + "fmt" + "net/http" + "path" + "regexp" + "strings" + + "github.com/getkin/kin-openapi/openapi3" + "github.com/gin-gonic/gin" + "github.com/shiningrush/droplet/data" + + "github.com/apisix/manager-api/internal/conf" + "github.com/apisix/manager-api/internal/core/entity" + "github.com/apisix/manager-api/internal/core/store" + "github.com/apisix/manager-api/internal/handler" + routeHandler "github.com/apisix/manager-api/internal/handler/route" + "github.com/apisix/manager-api/internal/utils" + "github.com/apisix/manager-api/internal/utils/consts" +) + +type Handler struct { + routeStore store.Interface + svcStore store.Interface + upstreamStore store.Interface + scriptStore store.Interface +} + +func NewHandler() (handler.RouteRegister, error) { + return &Handler{ + routeStore: store.GetStore(store.HubKeyRoute), + svcStore: store.GetStore(store.HubKeyService), + upstreamStore: store.GetStore(store.HubKeyUpstream), + scriptStore: store.GetStore(store.HubKeyScript), + }, nil +} + +func (h *Handler) ApplyRoute(r *gin.Engine) { + r.POST("/apisix/admin/import", consts.ErrorWrapper(Import)) +} + +func Import(c *gin.Context) (interface{}, error) { + file, err := c.FormFile("file") + if err != nil { + return nil, err + } + + // file check + suffix := path.Ext(file.Filename) + if suffix != ".json" && suffix != ".yaml" && suffix != ".yml" { + return nil, fmt.Errorf("the file type error: %s", suffix) + } + if file.Size > int64(conf.ImportSizeLimit) { + return nil, fmt.Errorf("the file size exceeds the limit; limit %d", conf.ImportSizeLimit) + } + + // read file and parse + handle, err := file.Open() + defer func() { + err = handle.Close() + }() + if err != nil { + return nil, err + } + + reader := bufio.NewReader(handle) + bytes := make([]byte, file.Size) + _, err = reader.Read(bytes) + if err != nil { + return nil, err + } + + swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(bytes) + if err != nil { + return nil, err + } + + routes, err := OpenAPI3ToRoute(swagger) + if err != nil { + return nil, err + } + + routeStore := store.GetStore(store.HubKeyRoute) + upstreamStore := store.GetStore(store.HubKeyUpstream) + scriptStore := store.GetStore(store.HubKeyScript) + + // check route + for _, route := range routes { + _, err := checkRouteName(route.Name) + if err != nil { + continue + } + if route.ServiceID != nil { + _, err := routeStore.Get(utils.InterfaceToString(route.ServiceID)) + if err != nil { + if err == data.ErrNotFound { + return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, + fmt.Errorf("service id: %s not found", route.ServiceID) + } + return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, err + } + } + if route.UpstreamID != nil { + _, err := upstreamStore.Get(utils.InterfaceToString(route.UpstreamID)) + if err != nil { + if err == data.ErrNotFound { + return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, + fmt.Errorf("upstream id: %s not found", route.UpstreamID) + } + return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, err + } + } + if route.Script != nil { + if route.ID == "" { + route.ID = utils.GetFlakeUidStr() + } + script := &entity.Script{} + script.ID = utils.InterfaceToString(route.ID) + script.Script = route.Script Review comment: ```suggestion script := &entity.Script{ ID: utils.InterfaceToString(route.ID), Script: route.Script, } ``` ########## File path: api/internal/handler/data_loader/import.go ########## @@ -0,0 +1,468 @@ +/* + * 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 ( + "bufio" + "encoding/json" + "fmt" + "net/http" + "path" + "regexp" + "strings" + + "github.com/getkin/kin-openapi/openapi3" + "github.com/gin-gonic/gin" + "github.com/shiningrush/droplet/data" + + "github.com/apisix/manager-api/internal/conf" + "github.com/apisix/manager-api/internal/core/entity" + "github.com/apisix/manager-api/internal/core/store" + "github.com/apisix/manager-api/internal/handler" + routeHandler "github.com/apisix/manager-api/internal/handler/route" + "github.com/apisix/manager-api/internal/utils" + "github.com/apisix/manager-api/internal/utils/consts" +) + +type Handler struct { + routeStore store.Interface + svcStore store.Interface + upstreamStore store.Interface + scriptStore store.Interface +} + +func NewHandler() (handler.RouteRegister, error) { + return &Handler{ + routeStore: store.GetStore(store.HubKeyRoute), + svcStore: store.GetStore(store.HubKeyService), + upstreamStore: store.GetStore(store.HubKeyUpstream), + scriptStore: store.GetStore(store.HubKeyScript), + }, nil +} + +func (h *Handler) ApplyRoute(r *gin.Engine) { + r.POST("/apisix/admin/import", consts.ErrorWrapper(Import)) +} + +func Import(c *gin.Context) (interface{}, error) { + file, err := c.FormFile("file") + if err != nil { + return nil, err + } + + // file check + suffix := path.Ext(file.Filename) + if suffix != ".json" && suffix != ".yaml" && suffix != ".yml" { + return nil, fmt.Errorf("the file type error: %s", suffix) + } + if file.Size > int64(conf.ImportSizeLimit) { + return nil, fmt.Errorf("the file size exceeds the limit; limit %d", conf.ImportSizeLimit) + } + + // read file and parse + handle, err := file.Open() + defer func() { Review comment: Should check `err` before the `defer` call. ########## File path: api/internal/core/store/validate.go ########## @@ -243,6 +245,8 @@ func (v *APISIXJsonSchemaValidator) Validate(obj interface{}) error { } errString.AppendString(vErr.String()) } + j, _ := json.Marshal(obj) + log.Errorf("schema validate failed:s: %v, obj: %s", v.schemaDef, string(j)) Review comment: Why not just using `%#v` to show the object? ########## File path: api/internal/handler/data_loader/import.go ########## @@ -0,0 +1,468 @@ +/* + * 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 ( + "bufio" + "encoding/json" + "fmt" + "net/http" + "path" + "regexp" + "strings" + + "github.com/getkin/kin-openapi/openapi3" + "github.com/gin-gonic/gin" + "github.com/shiningrush/droplet/data" + + "github.com/apisix/manager-api/internal/conf" + "github.com/apisix/manager-api/internal/core/entity" + "github.com/apisix/manager-api/internal/core/store" + "github.com/apisix/manager-api/internal/handler" + routeHandler "github.com/apisix/manager-api/internal/handler/route" + "github.com/apisix/manager-api/internal/utils" + "github.com/apisix/manager-api/internal/utils/consts" +) + +type Handler struct { + routeStore store.Interface + svcStore store.Interface + upstreamStore store.Interface + scriptStore store.Interface +} + +func NewHandler() (handler.RouteRegister, error) { + return &Handler{ + routeStore: store.GetStore(store.HubKeyRoute), + svcStore: store.GetStore(store.HubKeyService), + upstreamStore: store.GetStore(store.HubKeyUpstream), + scriptStore: store.GetStore(store.HubKeyScript), + }, nil +} + +func (h *Handler) ApplyRoute(r *gin.Engine) { + r.POST("/apisix/admin/import", consts.ErrorWrapper(Import)) +} + +func Import(c *gin.Context) (interface{}, error) { + file, err := c.FormFile("file") + if err != nil { + return nil, err + } + + // file check + suffix := path.Ext(file.Filename) + if suffix != ".json" && suffix != ".yaml" && suffix != ".yml" { + return nil, fmt.Errorf("the file type error: %s", suffix) + } + if file.Size > int64(conf.ImportSizeLimit) { + return nil, fmt.Errorf("the file size exceeds the limit; limit %d", conf.ImportSizeLimit) + } + + // read file and parse + handle, err := file.Open() + defer func() { + err = handle.Close() + }() + if err != nil { + return nil, err + } + + reader := bufio.NewReader(handle) + bytes := make([]byte, file.Size) + _, err = reader.Read(bytes) + if err != nil { + return nil, err + } + + swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(bytes) + if err != nil { + return nil, err + } + + routes, err := OpenAPI3ToRoute(swagger) + if err != nil { + return nil, err + } + + routeStore := store.GetStore(store.HubKeyRoute) + upstreamStore := store.GetStore(store.HubKeyUpstream) + scriptStore := store.GetStore(store.HubKeyScript) + + // check route + for _, route := range routes { + _, err := checkRouteName(route.Name) + if err != nil { + continue + } + if route.ServiceID != nil { + _, err := routeStore.Get(utils.InterfaceToString(route.ServiceID)) + if err != nil { + if err == data.ErrNotFound { + return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, + fmt.Errorf("service id: %s not found", route.ServiceID) + } + return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, err + } + } + if route.UpstreamID != nil { + _, err := upstreamStore.Get(utils.InterfaceToString(route.UpstreamID)) + if err != nil { + if err == data.ErrNotFound { + return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, + fmt.Errorf("upstream id: %s not found", route.UpstreamID) + } + return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, err + } + } + if route.Script != nil { + if route.ID == "" { + route.ID = utils.GetFlakeUidStr() + } + script := &entity.Script{} + script.ID = utils.InterfaceToString(route.ID) + script.Script = route.Script + // to lua + var err error + route.Script, err = routeHandler.GenerateLuaCode(route.Script.(map[string]interface{})) + if err != nil { + return nil, err + } + // save original conf + if err = scriptStore.Create(c, script); err != nil { + return nil, err + } + } + + if _, err := routeStore.CreateCheck(route); err != nil { + return handler.SpecCodeResponse(err), err + } + } + + // create route + for _, route := range routes { + if err := routeStore.Create(c, route); err != nil { + println(err.Error()) Review comment: Debugging code? ---------------------------------------------------------------- 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: us...@infra.apache.org