settings section

This commit is contained in:
2026-07-04 18:34:30 +05:00
parent bbe5d219ad
commit 0c717101da
2867 changed files with 1118596 additions and 1 deletions

41
internal/appresult/error.go Executable file
View File

@@ -0,0 +1,41 @@
package appresult
import "encoding/json"
var (
ErrMissingParam = NewAppError(nil, "Required parameter is missing or invalid.", "SE-00001")
ErrUnauthorized = NewAppError(nil, "Invalid credentials or unauthorized access.", "SE-00002")
ErrNotFound = NewAppError(nil, "The requested resource was not found.", "SE-00003")
ErrInternalServer = NewAppError(nil, "An internal server error occurred. Please try again later.", "SE-00004")
ErrNotAcceptable = NewAppError(nil, "The request could not be processed.", "SE-00007")
)
type AppError struct {
Status bool `json:"status"`
Err error `json:"-"`
Message string `json:"message,omitempty"`
Code string `json:"code,omitempty"`
}
func NewAppError(err error, message, code string) *AppError {
return &AppError{
Status: false,
Err: err,
Message: message,
Code: code,
}
}
func (e *AppError) Error() string {
return e.Message
}
func (e *AppError) Unwrap() error { return e.Err }
func (e *AppError) Marshal() []byte {
marshal, err := json.Marshal(e)
if err != nil {
return nil
}
return marshal
}

View File

@@ -0,0 +1,68 @@
package appresult
import (
"fmt"
"net/http"
"strings"
"online-back/internal/config"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
)
func JwtTokenCheck() gin.HandlerFunc {
return func(c *gin.Context) {
token := extractBearerToken(c.GetHeader("Authorization"))
if token == "" {
respondWithError(c, http.StatusForbidden, "authorization header is missing")
return
}
claims, err := TokenClaims(token, config.GetConfig().JwtKey)
if err != nil {
respondWithError(c, http.StatusUnauthorized, "invalid or expired token")
return
}
isAdmin, ok := claims["admin"].(bool)
if !ok || !isAdmin {
respondWithError(c, http.StatusForbidden, "unauthorized")
return
}
c.Set("id", fmt.Sprint(claims["id"]))
c.Next()
}
}
func TokenClaims(token, secretKey string) (jwt.MapClaims, error) {
decoded, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(secretKey), nil
})
if err != nil {
return nil, err
}
claims, ok := decoded.Claims.(jwt.MapClaims)
if !ok || !decoded.Valid {
return nil, fmt.Errorf("invalid token claims")
}
return claims, nil
}
func extractBearerToken(header string) string {
const prefix = "Bearer "
if strings.HasPrefix(header, prefix) {
return strings.TrimSpace(header[len(prefix):])
}
return strings.TrimSpace(header)
}
func respondWithError(c *gin.Context, code int, message interface{}) {
c.AbortWithStatusJSON(code, gin.H{"error": message})
}

36
internal/appresult/success.go Executable file
View File

@@ -0,0 +1,36 @@
package appresult
import "encoding/json"
const (
successMessage = "Request completed successfully."
successCode = "SS-10000"
)
type AppSuccess struct {
Status bool `json:"status"`
Message string `json:"message,omitempty"`
Code string `json:"code,omitempty"`
Data interface{} `json:"data"`
}
func NewAppSuccess(message, code string, data interface{}) *AppSuccess {
return &AppSuccess{
Status: true,
Message: message,
Code: code,
Data: data,
}
}
func SuccessWithData(data interface{}) *AppSuccess {
return NewAppSuccess(successMessage, successCode, data)
}
func (s *AppSuccess) Marshal() []byte {
marshal, err := json.Marshal(s)
if err != nil {
return nil
}
return marshal
}