api

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2025 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StaticFileHandler added in v1.0.2

func StaticFileHandler(fileServer http.Handler, contentType string) http.HandlerFunc

StaticFileHandler creates a handler serving static files with the correct MIME type

Types

type API

type API struct {
	Config *config.Config
	Logger *log.Logger
	// WaitGroup tracks background tasks to wait for during server shutdown.
	WaitGroup        sync.WaitGroup
	ShortlinkService *services.ShortlinkService
	VisitService     *services.VisitService
}

API encapsulates all routes and middleware.

func (*API) BadRequest

func (api *API) BadRequest(err error, w http.ResponseWriter)

BadRequest responds with a 400.

func (*API) CreatedSuccesfully

func (api *API) CreatedSuccesfully(w http.ResponseWriter, payload interface{})

CreatedSuccesfully responds with a 201.

func (*API) HandleGetHealth

func (api *API) HandleGetHealth(w http.ResponseWriter, _ *http.Request)

HandleGetHealth returns the health status of the API.

func (*API) HandleGetMetrics

func (api *API) HandleGetMetrics(w http.ResponseWriter, r *http.Request)

HandleGetMetrics handles the /metrics endpoint exposing metrics for Prometheus.

func (*API) HandleGetProtectedSlug

func (api *API) HandleGetProtectedSlug(w http.ResponseWriter, r *http.Request, slug string, shortlink *entities.Shortlink)

HandleGetProtectedSlug handles a GET /p/{slug} request by resolving a password-protected shortlink.

func (*API) HandleGetSlug

func (api *API) HandleGetSlug(w http.ResponseWriter, r *http.Request)

HandleGetSlug handles a GET /{slug} request by resolving a regular shortlink.

func (api *API) HandlePostShortlink(w http.ResponseWriter, r *http.Request)

HandlePostShortlink handles a POST /shortlink request by creating a shortlink.

func (*API) InitMetrics

func (api *API) InitMetrics(commitSha string)

InitMetrics initializes server-level expvar metrics, exposed via GET /debug/vars. Request-level expvar metrics are collected in middleware.

Prometheus metrics are based on expvar metrics and exposed via GET /metrics. commit_sha, uptime_seconds, timestamp, goroutines are excluded from Prometheus metrics.

func (*API) InternalServerError

func (api *API) InternalServerError(err error, w http.ResponseWriter)

InternalServerError responds with a 500.

func (*API) NotFound

func (api *API) NotFound(w http.ResponseWriter)

NotFound responds with a 404.

func (*API) RateLimitExceeded

func (api *API) RateLimitExceeded(w http.ResponseWriter, ip string)

RateLimitExceeded responds with a 429.

func (*API) Routes

func (api *API) Routes() http.Handler

Routes sets up middleware and routes on the API.

func (*API) SetupMiddleware

func (api *API) SetupMiddleware() MiddlewareFn

SetupMiddleware sets up all middleware on the API.

func (*API) Unauthorized

func (api *API) Unauthorized(err error, w http.ResponseWriter)

Unauthorized responds with a 401.

type ErrorField

type ErrorField struct {
	Message string `json:"message"`
	Code    string `json:"code"`
	Doc     string `json:"doc"`
	Trace   string `json:"trace"`
}

ErrorField contains error details.

type ErrorResponse

type ErrorResponse struct {
	Error ErrorField `json:"error"`
}

ErrorResponse is a generic error response.

type MiddlewareFn added in v1.0.2

type MiddlewareFn func(http.Handler) http.Handler

MiddlewareFn is a function that wraps an http.Handler.

type SuccessResponse

type SuccessResponse struct {
	Data interface{} `json:"data"`
}

SuccessResponse is a generic success response.

Jump to

Keyboard shortcuts

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