|
- /* ******************************************************************************
- * 2019 - present Contributed by Apulis Technology (Shenzhen) Co. LTD
- *
- * This program and the accompanying materials are made available under the
- * terms of the MIT License, which is available at
- * https://www.opensource.org/licenses/MIT
- *
- * See the NOTICE file distributed with this work for additional
- * information regarding copyright ownership.
- * 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.
- *
- * SPDX-License-Identifier: MIT
- ******************************************************************************/
- package controllers
-
- import (
- "errors"
- "fmt"
- "github.com/apulis/app/ai-arts-backend/pkg/exports"
- "net/http"
- "net/url"
- "strings"
-
- "github.com/apulis/app/ai-arts-backend/internal/middlewares"
- "github.com/apulis/app/ai-arts-backend/internal/services"
- "github.com/apulis/sdk/go-utils/logging"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- )
-
- type ControllerBase struct {
- }
-
- func (controllerBase *ControllerBase) Forward(c *gin.Context, path string) {
-
- director := func(req *http.Request) {
- urlpath := path //configs.GetAppConfig().Resources.Dataset + "/cv_datasets"
- u, _ := url.Parse(urlpath)
- req.URL.Path = u.Path
- req.URL.Host = u.Host
- req.URL.Scheme = u.Scheme
-
- req.Host = u.Host
- //req.Header.Set("User-Agent", "")
- req.Header.Del("From")
- fmt.Println(req.URL.String())
- }
- services.GetProxy(director).ServeHTTP(c.Writer, c.Request)
- }
-
- func (controllerBase *ControllerBase) ForwardWithQuery(c *gin.Context, path string, query string) {
-
- director := func(req *http.Request) {
- urlpath := path //configs.GetAppConfig().Resources.Dataset + "/cv_datasets"
- u, _ := url.Parse(urlpath)
- req.URL.Path = u.Path
- req.URL.Host = u.Host
- req.URL.Scheme = u.Scheme
-
- req.URL.RawQuery += query
-
- req.Host = u.Host
- //req.Header.Set("User-Agent", "")
- req.Header.Del("From")
- fmt.Println(req.URL.String())
- }
- services.GetProxy(director).ServeHTTP(c.Writer, c.Request)
- }
-
- type UserInfo struct {
- userName string
- userId int64
- groupName string
- groupId int64
- orgId int64
- orgName string
- }
-
- func (i *UserInfo) InitUserInfo(c *gin.Context) {
- i.userName = middlewares.UserName(c)
- i.userId, _ = middlewares.UserId(c)
- i.groupName = middlewares.GroupName(c)
- i.groupId, _ = middlewares.GroupId(c)
- i.orgId, _ = middlewares.OrgId(c)
- i.orgName = middlewares.OrgName(c)
- }
-
- type HeaderInfo struct {
- header http.Header
- }
-
- func (h *HeaderInfo) InitHeader(c *gin.Context) {
- header := http.Header{}
- for key, value := range c.Keys {
- header[key] = append(header[key], fmt.Sprintf("%v", value))
- }
- header["Authorization"] = append(header["Authorization"], c.Request.Header.Get("Authorization"))
- h.header = header
- }
-
- var (
- ValidateErrorMessage = map[string]string{
- "entityname":"无效的名称",
- }
- )
-
- func bindAndValidate(c *gin.Context, params interface{}) bool {
- if err := bind(c, params); err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
- "code": 400,
- "msg": err.Error(),
- "data": struct{}{},
- })
- return false
- }
-
- return true
- }
-
- func bind(c *gin.Context, params interface{}) error {
- _ = c.ShouldBindUri(params)
- if err := c.ShouldBind(params); err != nil {
- if fieldErr, ok := err.(validator.ValidationErrors); ok {
- var tagErrorMsg []string
- for _, v := range fieldErr {
- if _, has := ValidateErrorMessage[v.Tag()]; has {
- tagErrorMsg = append(tagErrorMsg, fmt.Sprintf("%s [%s]",ValidateErrorMessage[v.Tag()], v.Field()))
- } else {
- tagErrorMsg = append(tagErrorMsg, v.Error())
- }
- }
-
- logging.Error(err).Msgf("validate param error: %v", err)
- return errors.New(strings.Join(tagErrorMsg, ","))
- }
- }
- if c.Request.Method == "POST" {//@mark: bind querys into params also !!!
- _ = c.ShouldBindQuery(params)
- }
-
- return nil
- }
-
- func ok(c *gin.Context) {
- c.JSON(http.StatusOK, gin.H{
- "code": 0,
- "message": "success",
- "data": struct{}{},
- })
- }
-
- func respWithData(c *gin.Context, data interface{}) {
- c.JSON(http.StatusOK, gin.H{
- "code": 0,
- "msg": "success",
- "data": data,
- })
- }
-
- func fail(c *gin.Context, err error) {
- if h,ok := err.(*exports.APIException);ok {
- c.JSON(h.StatusCode, gin.H{
- "code": h.Code,
- "msg": h.Msg,
- "data": struct{}{},
- })
- }else {
- c.JSON(http.StatusInternalServerError, gin.H{
- "code": 400,
- "msg": err.Error(),
- "data": struct{}{},
- })
- }
- }
-
- func failWithData(c *gin.Context, code int, msg string, data interface{}) {
- c.JSON(http.StatusInternalServerError, gin.H{
- "code": code,
- "msg": msg,
- "data": data,
- })
- }
-
- /***
- func failValidate(c *gin.Context, msg string) {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
- "code": constants.ErrorValidation.Code,
- "msg": msg,
- "data": struct{}{},
- })
- }
-
- func failValidateWithErr(c *gin.Context, err *constants.CodeMessage) {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
- "code": err.Code,
- "msg": err.Message,
- "data": struct{}{},
- })
- }
-
- func ok(c *gin.Context) {
- c.JSON(http.StatusOK, gin.H{
- "code": constants.ActionSuccess.Code,
- "message": constants.ActionSuccess.Message,
- "data": struct{}{},
- })
- }
-
- func respWithData(c *gin.Context, data interface{}) {
- c.JSON(http.StatusOK, gin.H{
- "code": constants.ActionSuccess.Code,
- "msg": constants.ActionSuccess.Message,
- "data": data,
- })
- }
-
- func respWithDataAndErrCode(c *gin.Context, data interface{}, err *constants.CodeMessage) {
- c.JSON(http.StatusOK, gin.H{
- "code": err.Code,
- "msg": err.Message,
- "data": data,
- })
- }
-
- //StatusUnauthorized
- func failUnauthorized(c *gin.Context, err *constants.CodeMessage) {
- c.JSON(http.StatusUnauthorized, gin.H{
- "code": err.Code,
- "msg": err.Message,
- "data": struct{}{},
- })
- }
-
- func failWithHttpCode(c *gin.Context, httpCode int, err *constants.CodeMessage) {
- c.JSON(httpCode, gin.H{
- "code": err.Code,
- "msg": err.Message,
- "data": struct{}{},
- })
- }
-
- func fail(c *gin.Context, err *constants.CodeMessage) {
- c.JSON(http.StatusInternalServerError, gin.H{
- "code": err.Code,
- "msg": err.Message,
- "data": struct{}{},
- })
- }
-
- func failWithDBError(c *gin.Context, err error) {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- constants.ErrorRecordNotExist.Message = err.Error()
- c.JSON(http.StatusInternalServerError, gin.H{
- "code": constants.ErrorRecordNotExist.Code,
- "msg": constants.ErrorRecordNotExist.Message,
- "data": struct{}{},
- })
- } else {
- constants.ErrorDatabaseOp.Message = err.Error()
- c.JSON(http.StatusInternalServerError, gin.H{
- "code": constants.ErrorDatabaseOp.Code,
- "msg": constants.ErrorDatabaseOp.Message,
- "data": struct{}{},
- })
- }
- }
- ***/
|