v1

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: MIT Imports: 38 Imported by: 0

Documentation

Overview

Package v1 provides HTTP handlers for the blog API version 1 endpoints.

Index

Constants

View Source
const (
	TokenTypeAccess  = "access"  // Access token for API authentication
	TokenTypeRefresh = "refresh" // Refresh token for obtaining new access tokens
)

Token type constants define the types of JWT tokens used in the application.

Variables

View Source
var (
	// ErrUnauthorized is returned when a user attempts to access a protected resource without proper credentials.
	ErrUnauthorized = "Unauthorized access"

	// ErrTokenInvalidOrExpired is returned when the provided token is either malformed or has expired.
	ErrTokenInvalidOrExpired = "Your session has expired"

	// ErrMissingHeader is returned when the Authorization header is not present in the request.
	ErrMissingHeader = "missing Authorization header"

	// ErrInvalidFormat is returned when the Authorization header format is incorrect.
	ErrInvalidFormat = "invalid Authorization header"

	// ErrEmptyToken is returned when the token value is empty or blank.
	ErrEmptyToken = "token is empty"

	// ErrUserNotFound is returned when the authenticated user cannot be found in the database.
	ErrUserNotFound = "user not found"

	// ErrTokenExpired is returned when the token has passed its expiration time.
	ErrTokenExpired = "token expired"

	// ErrInvalidAuthFormat is returned when the Authorization header doesn't follow the 'Bearer <token>' format.
	ErrInvalidAuthFormat = "Invalid Authorization format. Expected 'Bearer <token>'"

	// ErrNotEnoughPerm is returned when a user lacks the required permissions for a resource.
	ErrNotEnoughPerm = "You don't have enough permission to access this resource."
)

Authentication error messages returned by the auth middleware and handlers.

Functions

func MarkdownToHTML

func MarkdownToHTML(content string) string

MarkdownToHTML converts a Markdown string to HTML with syntax highlighting.

Types

type APIV1Service

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

APIV1Service handles all API v1 endpoints and dependencies.

func NewAPIV1Service

func NewAPIV1Service(cfg *config.Config, logger *slog.Logger, db database.Models) *APIV1Service

NewAPIV1Service creates a new API v1 service instance.

func (*APIV1Service) CheckIP added in v1.0.14

func (s *APIV1Service) CheckIP() gin.HandlerFunc

CheckIP returns a Gin middleware handler that checks if the client's IP address is banned. If banned, it aborts the request with a forbidden error response using AbortWithError. If an internal error occurs during the check, it aborts with an internal server error. Otherwise, it calls c.Next() to proceed with the handler chain. This middleware should be used to protect routes from banned IPs.

func (*APIV1Service) CheckJWT added in v1.0.14

func (s *APIV1Service) CheckJWT() gin.HandlerFunc

CheckJWT validates a JWT token from the "Authorization" header. It performs the following checks: Ensures the "Authorization" header exists and is in "Bearer <token>" format. Verifies the token signature, expiration, and required claims (user_id). Cross-checks the token's user_id against the database for validity.

func (*APIV1Service) RateLimiter added in v1.0.14

func (s *APIV1Service) RateLimiter() gin.HandlerFunc

RateLimiter returns a Gin middleware function for rate limiting requests. It uses a token bucket algorithm to limit the number of requests allowed per client.

func (*APIV1Service) RegisterRoutes

func (s *APIV1Service) RegisterRoutes() http.Handler

RegisterRoutes configures and returns an HTTP handler with all API v1 routes.

Jump to

Keyboard shortcuts

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