api

package
v0.0.0-...-c63aa1e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderAuthenticate    = "WWW-Authenticate"
	HeaderAuthorization   = "Authorization"
	HeaderOrigin          = "Origin"
	HeaderContentLength   = "Content-Length"
	HeaderContentType     = "Content-Type"
	HeaderContentLanguage = "Content-Language"
	HeaderAcceptLanguage  = "Accept-Language"
	HeaderTraceID         = "Trace-ID"
)
View Source
const (
	KeyParams = "params"

	LocaleEN      = "en"
	DefaultLocale = LocaleEN
)
View Source
const (
	ErrorCodeInvalidRoute   = "invalid_route"
	ErrorCodeInvalidRequest = "invalid_request"
	ErrorCodeInvalidQuery   = "invalid_query"
	ErrorCodeInvalidBody    = "invalid_body"
	ErrorCodeInvalidServer  = "invalid_server"
)
View Source
const (
	KeySkipLog = "skip_log"
)

Variables

View Source
var (
	PathNotFound          = errors.New("path not found")
	NoAuthorizationHeader = errors.New("no authorization header")
	InvalidTokenSignature = errors.New("invalid token signature")
	FailedToParseToken    = errors.New("failed to parse token")
	InvalidToken          = errors.New("invalid token")
)
View Source
var DefaultCorsConfig = cors.Config{
	AllowAllOrigins: false,
	AllowMethods: []string{
		http.MethodGet,
		http.MethodPost,
		http.MethodPut,
		http.MethodPatch,
		http.MethodDelete,
		http.MethodHead,
		http.MethodOptions,
	},
	AllowHeaders: []string{
		HeaderAuthorization,
		HeaderOrigin,
		HeaderContentLength,
		HeaderContentType,
		HeaderAcceptLanguage,
		HeaderContentLanguage,
		HeaderTraceID,
	},
	ExposeHeaders:    []string{"Content-Length"},
	AllowCredentials: true,
	AllowWildcard:    true,
	MaxAge:           12 * time.Hour,
}
View Source
var PaginationPerPage int64 = 10

Functions

func ExtractParams

func ExtractParams(c *gin.Context)

func ExtractTraceID

func ExtractTraceID(c *gin.Context)

func MiddlewareLogger

func MiddlewareLogger(c *gin.Context)

func MiddlewareSkipLogger

func MiddlewareSkipLogger(c *gin.Context)

func NewRouter

func NewRouter(c config.API) *gin.Engine

func NewServer

func NewServer(c config.API, router *gin.Engine) *http.Server

func ParseRequest

func ParseRequest(c *gin.Context, obj any) error

func ParseRequestURI

func ParseRequestURI(c *gin.Context, obj any) error

func Recovery

func Recovery(c *gin.Context)

func RegisterSwagger

func RegisterSwagger(r *gin.Engine, docsFs embed.FS) error

func RespondBadRequest

func RespondBadRequest(c *gin.Context, code string, desc error)

func RespondConflict

func RespondConflict(c *gin.Context, code string, desc error)

func RespondData

func RespondData(c *gin.Context, data any)

func RespondDataPages

func RespondDataPages(c *gin.Context, data any, pages int64)

func RespondForbidden

func RespondForbidden(c *gin.Context, code string, desc error)

func RespondInternalError

func RespondInternalError(c *gin.Context)

func RespondNotFound

func RespondNotFound(c *gin.Context, code string, desc error)

func RespondOK

func RespondOK(c *gin.Context)

func RespondUnauthorized

func RespondUnauthorized(c *gin.Context, code string, desc error)

Types

type ClaimsWithRoles

type ClaimsWithRoles struct {
	jwt.RegisteredClaims
	Roles []string `json:"roles"`
}

type Error

type Error struct {
	Code        string `json:"code,omitempty"`
	Description string `json:"description,omitempty"`
}

type JWTGenerator

type JWTGenerator struct {
	// contains filtered or unexported fields
}

func NewJWTGenerator

func NewJWTGenerator(issuer, privateKeyString string, ttl time.Duration) (*JWTGenerator, error)

func (JWTGenerator) GenerateToken

func (g JWTGenerator) GenerateToken(userID string) (string, error)

func (JWTGenerator) GenerateTokenWthRoles

func (g JWTGenerator) GenerateTokenWthRoles(userID string, roles []string) (string, error)

type JWTMiddleware

type JWTMiddleware struct {
	// contains filtered or unexported fields
}

func NewJWTMiddleware

func NewJWTMiddleware(publicKeyString string, enabled bool) (*JWTMiddleware, error)

func (JWTMiddleware) JWTAuth

func (m JWTMiddleware) JWTAuth(c *gin.Context)

JWTAuth parse and verifying JWT token

func (JWTMiddleware) JWTAuthRole

func (m JWTMiddleware) JWTAuthRole(role string) gin.HandlerFunc

JWTAuthRole parse and verifying JWT token and check role

type Pagination

type Pagination struct {
	Page    int64 `form:"page" json:"page"`
	PerPage int64 `form:"per_page" json:"per_page"`
}

func (Pagination) LimitOffset

func (p Pagination) LimitOffset() (int64, int64)

func (Pagination) Pages

func (p Pagination) Pages(count int64) int64

type Params

type Params struct {
	Subject string
	Roles   []string
	Locale  string
	IPAddr  string
}

func GetParams

func GetParams(c *gin.Context) Params

type Response

type Response struct {
	Data  any    `json:"data,omitempty"`
	Error *Error `json:"error,omitempty"`
	Pages int64  `json:"pages,omitempty"`
}

func ErrorResponse

func ErrorResponse(code string, err error) Response

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL