chore: import upstream snapshot with attribution
CI / Check and lint (push) Failing after 1s
Lint / Lint (ubuntu-latest) (push) Failing after 1s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:22:32 +08:00
commit 39dbe3a57d
1131 changed files with 272709 additions and 0 deletions
+92
View File
@@ -0,0 +1,92 @@
/*
* 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 handler
import (
"errors"
"net/http"
"github.com/apache/answer/internal/base/reason"
"github.com/apache/answer/internal/base/validator"
"github.com/gin-gonic/gin"
myErrors "github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
)
// HandleResponse Handle response body
func HandleResponse(ctx *gin.Context, err error, data any) {
lang := GetLangByCtx(ctx)
// no error
if err == nil {
ctx.JSON(http.StatusOK, NewRespBodyData(http.StatusOK, reason.Success, data).TrMsg(lang))
return
}
var myErr *myErrors.Error
// unknown error
if !errors.As(err, &myErr) {
log.Error(err, "\n", myErrors.LogStack(2, 5))
ctx.JSON(http.StatusInternalServerError, NewRespBody(
http.StatusInternalServerError, reason.UnknownError).TrMsg(lang))
return
}
// log internal server error
if myErrors.IsInternalServer(myErr) {
log.Error(myErr)
}
respBody := NewRespBodyFromError(myErr).TrMsg(lang)
if data != nil {
respBody.Data = data
}
ctx.JSON(myErr.Code, respBody)
}
// BindAndCheck bind request and check
func BindAndCheck(ctx *gin.Context, data any) bool {
lang := GetLangByCtx(ctx)
if err := ctx.ShouldBind(data); err != nil {
log.Errorf("http_handle BindAndCheck fail, %s", err.Error())
HandleResponse(ctx, myErrors.New(http.StatusBadRequest, reason.RequestFormatError), nil)
return true
}
errField, err := validator.GetValidatorByLang(lang).Check(data)
if err != nil {
HandleResponse(ctx, err, errField)
return true
}
return false
}
// BindAndCheckReturnErr bind request and check
func BindAndCheckReturnErr(ctx *gin.Context, data any) (errFields []*validator.FormErrorField) {
lang := GetLangByCtx(ctx)
if err := ctx.ShouldBind(data); err != nil {
log.Errorf("http_handle BindAndCheck fail, %s", err.Error())
HandleResponse(ctx, myErrors.New(http.StatusBadRequest, reason.RequestFormatError), nil)
ctx.Abort()
return nil
}
errFields, _ = validator.GetValidatorByLang(lang).Check(data)
return errFields
}
+47
View File
@@ -0,0 +1,47 @@
/*
* 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 handler
import (
"context"
"github.com/apache/answer/internal/base/constant"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/i18n"
)
// GetLangByCtx get language from header
func GetLangByCtx(ctx context.Context) i18n.Language {
if ginCtx, ok := ctx.(*gin.Context); ok {
acceptLanguage, ok := ginCtx.Get(constant.AcceptLanguageFlag)
if ok {
if acceptLanguage, ok := acceptLanguage.(i18n.Language); ok {
return acceptLanguage
}
return i18n.DefaultLanguage
}
}
acceptLanguage, ok := ctx.Value(constant.AcceptLanguageContextKey).(i18n.Language)
if ok {
return acceptLanguage
}
return i18n.DefaultLanguage
}
+72
View File
@@ -0,0 +1,72 @@
/*
* 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 handler
import (
"github.com/apache/answer/internal/base/translator"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/i18n"
)
// RespBody response body.
type RespBody struct {
// http code
Code int `json:"code"`
// reason key
Reason string `json:"reason"`
// response message
Message string `json:"msg"`
// response data
Data any `json:"data"`
}
// TrMsg translate the reason cause as a message
func (r *RespBody) TrMsg(lang i18n.Language) *RespBody {
if len(r.Message) == 0 {
r.Message = translator.Tr(lang, r.Reason)
}
return r
}
// NewRespBody new response body
func NewRespBody(code int, reason string) *RespBody {
return &RespBody{
Code: code,
Reason: reason,
}
}
// NewRespBodyFromError new response body from error
func NewRespBodyFromError(e *errors.Error) *RespBody {
return &RespBody{
Code: e.Code,
Reason: e.Reason,
Message: e.Message,
}
}
// NewRespBodyData new response body with data
func NewRespBodyData(code int, reason string, data any) *RespBody {
return &RespBody{
Code: code,
Reason: reason,
Data: data,
}
}
+47
View File
@@ -0,0 +1,47 @@
/*
* 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 handler
import (
"context"
"github.com/apache/answer/internal/base/constant"
"github.com/gin-gonic/gin"
)
// GetEnableShortID get short id flag from context
func GetEnableShortID(ctx context.Context) bool {
// Check gin context first (set by ShortIDMiddleware via ctx.Set)
if ginCtx, ok := ctx.(*gin.Context); ok {
flag, ok := ginCtx.Get(constant.ShortIDFlag)
if ok {
if flag, ok := flag.(bool); ok {
return flag
}
return false
}
}
// Fallback for non-gin contexts (e.g., SitemapCron uses context.WithValue)
flag, ok := ctx.Value(constant.ShortIDContextKey).(bool)
if ok {
return flag
}
return false
}