Documentation
¶
Index ¶
- Constants
- Variables
- func GetOptions(opt url.Values) (skip, limit, page int64)
- func Health(server *Server) http.HandlerFunc
- func RawRoutesList(server *Server) http.HandlerFunc
- func RequestScheme(r *http.Request) string
- func ResponseJsonWithError(w http.ResponseWriter, code int, errorResponse *ErrorResponse) error
- func ResponseWithError(w http.ResponseWriter, code int, msg string) error
- func ResponseWithJSON(w http.ResponseWriter, code int, payload interface{}) error
- func ResponseWithPagination(w http.ResponseWriter, code int, payload interface{}) error
- func ResponseWithPaginationTest(w http.ResponseWriter, code int, options *PaginationOptions) error
- func RoutesList(server *Server) http.HandlerFunc
- func Serve(servers []*Server)
- type Endpoint
- type EndpointOptions
- type ErrorResponse
- type GroupOptions
- type PaginationOptions
- type ResponsePagination
- type Server
- func (server *Server) ApplyMiddlewares(mux http.Handler) http.Handler
- func (server *Server) Endpoint(path string, handler http.HandlerFunc)
- func (server *Server) EndpointIsPublic(r *http.Request) bool
- func (server *Server) GetEndpoint(r *http.Request) (*Endpoint, bool)
- func (server *Server) GetEndpoints() map[string]*Endpoint
- func (server *Server) Group(group string, apiEndpoints []Endpoint)
- func (server *Server) GroupWithOptions(group string, apiEndpoints []Endpoint, groupOptions *GroupOptions)
- func (server *Server) LogRequest(next http.Handler) http.Handler
- func (server *Server) NoRequiresAuthentication(r *http.Request) bool
- func (server *Server) PrepareEndpoints() *http.ServeMux
- func (server *Server) Run()
- func (server *Server) Use(middlewares ...func(next http.Handler, server *Server) http.Handler)
- func (server *Server) ValidateSecret(next http.Handler) http.Handler
- type Settings
Constants ¶
const ( DefaultWriteTimeout = 15 * time.Second DefaultReadTimeout = 15 * time.Second )
Server timeout defaults, applied when Settings leaves them at zero. They are the values Run() hardcoded before they were configurable, so upgrading changes nothing on its own.
Variables ¶
var ServerEndpoints = []Endpoint{ {Path: "GET /_health", HandlerServerFunc: Health, Options: EndpointOptions{IsPublic: true, NoRequiresAuthentication: true, IgnorePrefix: true}}, {Path: "GET /_routes", HandlerServerFunc: RoutesList, Options: EndpointOptions{IsPublic: true, NoRequiresAuthentication: true, IgnorePrefix: true}}, {Path: "GET /_routes/raw", HandlerServerFunc: RawRoutesList, Options: EndpointOptions{IsPublic: true, NoRequiresAuthentication: true, IgnorePrefix: true}}, }
ServerEndpoints is the list of endpoints for the server
Functions ¶
func GetOptions ¶ added in v1.3.2
func RawRoutesList ¶ added in v1.3.0
func RawRoutesList(server *Server) http.HandlerFunc
func RequestScheme ¶ added in v1.3.3
func ResponseJsonWithError ¶ added in v1.3.2
func ResponseJsonWithError(w http.ResponseWriter, code int, errorResponse *ErrorResponse) error
func ResponseWithError ¶
func ResponseWithError(w http.ResponseWriter, code int, msg string) error
ResponseWithError return a json response with an error
func ResponseWithJSON ¶
func ResponseWithJSON(w http.ResponseWriter, code int, payload interface{}) error
ResponseWithJSON return a json response
func ResponseWithPagination ¶ added in v1.3.0
func ResponseWithPagination(w http.ResponseWriter, code int, payload interface{}) error
func ResponseWithPaginationTest ¶ added in v1.3.2
func ResponseWithPaginationTest(w http.ResponseWriter, code int, options *PaginationOptions) error
Types ¶
type Endpoint ¶ added in v1.2.0
type Endpoint struct {
Path string
HandlerFunc http.HandlerFunc
Handler http.Handler // Handler is a http.Handler and is used to create a new http.Handler with the server's middlewares and endpoints
HandlerServerFunc func(server *Server) http.HandlerFunc // HandlerServerFunc is a function that returns a http.HandlerFunc and is used to create a new http.HandlerFunc with the server's middlewares and endpoints
Options EndpointOptions
RegexPattern *regexp.Regexp
}
Endpoint is a struct that contains the endpoint's configuration and handlers
type EndpointOptions ¶ added in v1.2.0
EndpointOptions is a struct that contains the endpoint's options
type ErrorResponse ¶ added in v1.3.2
type GroupOptions ¶ added in v1.3.14
type PaginationOptions ¶ added in v1.3.2
type ResponsePagination ¶ added in v1.3.0
type ResponsePagination struct {
TotalPages int64 `json:"total_pages"`
Total int64 `json:"total"`
CurrentPage int64 `json:"current_page"`
From int64 `json:"from"`
To int64 `json:"to"`
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
PerPage int `json:"per_page"`
Path string `json:"path"`
FirstPageURL string `json:"first_page_url"`
NextPageURL string `json:"next_page_url"`
LastPageURL string `json:"last_page_url"`
PrevPageURL string `json:"prev_page_url"`
Data interface{} `json:"data"`
}
func (*ResponsePagination) Set ¶ added in v1.3.2
func (p *ResponsePagination) Set()
type Server ¶
type Server struct {
ServerName string
ServerNumber string
RunningServerMessage string
Secret string
Debug bool
Port string
Middlewares []func(next http.Handler, server *Server) http.Handler
Endpoints [][]Endpoint
EndpointsPaths map[string]*Endpoint
CorsOptions cors.Options
Settings *Settings
// contains filtered or unexported fields
}
Server is a struct that contains the server's configuration and endpoints
func (*Server) ApplyMiddlewares ¶ added in v1.2.0
ApplyMiddlewares apply all middlewares to the mux; if the server is in debug mode, the server will be register the LogRequest middleware that will log the request on the console if the server has a secret, the server will be register the ValidateSecret middleware that will check if the request has a secret
func (*Server) Endpoint ¶ added in v1.3.3
func (server *Server) Endpoint(path string, handler http.HandlerFunc)
func (*Server) EndpointIsPublic ¶ added in v1.2.0
EndpointIsPublic evalue if the endpoint is public
func (*Server) GetEndpoint ¶ added in v1.2.0
GetEndpoint evaluate if a path exists in the endpoints and return the endpoint and a bool if exists
func (*Server) GetEndpoints ¶ added in v1.2.0
GetEndpoints return all endpoints
func (*Server) GroupWithOptions ¶ added in v1.3.14
func (server *Server) GroupWithOptions(group string, apiEndpoints []Endpoint, groupOptions *GroupOptions)
func (*Server) LogRequest ¶ added in v1.2.0
LogRequest log the request on the console
func (*Server) NoRequiresAuthentication ¶ added in v1.3.13
func (*Server) PrepareEndpoints ¶ added in v1.4.3
PrepareEndpoints applies the path prefix, registers every endpoint on a mux and fills the route index that EndpointIsPublic and NoRequiresAuthentication read. It returns that mux. Run calls it and then serves the result.
It is exported so a caller can build the SAME wiring without listening. Until now the index was filled only inside Run, and that had a consequence worth spelling out: a test that assembled its own mux saw an EMPTY index, so matchRoute returned nil and every endpoint looked non-public. An assertion like "this group requires the API secret" then passed for a reason that had nothing to do with the group — it would have passed just the same with the group marked public. That is a green result that does not mean what it looks like, and the only way to avoid it without duplicating this route matching somewhere else is to let callers run this exact code.
It is idempotent: calling it twice neither prefixes the paths twice nor adds the library endpoints twice.
type Settings ¶ added in v1.3.3
type Settings struct {
IgnoreSecret bool
PathPrefix string
// WriteTimeout and ReadTimeout override the server defaults
// (DefaultWriteTimeout / DefaultReadTimeout). Zero means "not set" and keeps
// the default, so existing servers behave exactly as before.
//
// They were hardcoded at 15s until now. That is a sane default for a REST
// API and impossible for anything long-lived: an SSE stream, a chunked
// download, or an upload over a slow connection dies mid-flight, and the
// client sees a truncated body rather than an error.
//
// # Do NOT reach for a zero timeout to stream
//
// Zero here means "keep the default", not "no timeout" — deliberately.
// Disabling the write timeout for the WHOLE server so that one endpoint can
// stream lets a single stuck client hold a connection forever, and takes the
// protection away from every other route to fix one.
//
// The right tool is per-connection and lives in the standard library:
//
// // inside a streaming handler
// rc := http.NewResponseController(w)
// if err := rc.SetWriteDeadline(time.Time{}); err != nil {
// // the server does not support deadline control; fail loudly
// }
//
// That clears the deadline for THAT connection and leaves the server's
// default protecting everything else. These fields are for when the whole
// service needs a different number — a gateway that proxies slow upstreams,
// a service whose every route uploads.
WriteTimeout time.Duration
ReadTimeout time.Duration
}