Documentation
¶
Index ¶
- func StaticFileHandler(fileServer http.Handler, contentType string) http.HandlerFunc
- type API
- func (api *API) BadRequest(err error, w http.ResponseWriter)
- func (api *API) CreatedSuccesfully(w http.ResponseWriter, payload interface{})
- func (api *API) HandleGetHealth(w http.ResponseWriter, _ *http.Request)
- func (api *API) HandleGetMetrics(w http.ResponseWriter, r *http.Request)
- func (api *API) HandleGetProtectedSlug(w http.ResponseWriter, r *http.Request, slug string, ...)
- func (api *API) HandleGetSlug(w http.ResponseWriter, r *http.Request)
- func (api *API) HandlePostShortlink(w http.ResponseWriter, r *http.Request)
- func (api *API) InitMetrics(commitSha string)
- func (api *API) InternalServerError(err error, w http.ResponseWriter)
- func (api *API) NotFound(w http.ResponseWriter)
- func (api *API) RateLimitExceeded(w http.ResponseWriter, ip string)
- func (api *API) Routes() http.Handler
- func (api *API) SetupMiddleware() MiddlewareFn
- func (api *API) Unauthorized(err error, w http.ResponseWriter)
- type ErrorField
- type ErrorResponse
- type MiddlewareFn
- type SuccessResponse
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) HandlePostShortlink ¶
func (api *API) HandlePostShortlink(w http.ResponseWriter, r *http.Request)
HandlePostShortlink handles a POST /shortlink request by creating a shortlink.
func (*API) InitMetrics ¶
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) 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
MiddlewareFn is a function that wraps an http.Handler.
type SuccessResponse ¶
type SuccessResponse struct {
Data interface{} `json:"data"`
}
SuccessResponse is a generic success response.