Documentation ¶
Index ¶
- func ClearStartupHooks()
- func IsReady() bool
- func RegisterStartupHook(hook func())
- func Start(routeRegistrer func(*Router))
- func Stop()
- type Handler
- type Middleware
- type Request
- func (r *Request) Bool(field string) bool
- func (r *Request) ContentLength() int64
- func (r *Request) Cookies(name string) []*http.Cookie
- func (r *Request) Date(field string) time.Time
- func (r *Request) File(field string) []filesystem.File
- func (r *Request) Has(field string) bool
- func (r *Request) Header() http.Header
- func (r *Request) IP(field string) net.IP
- func (r *Request) Integer(field string) int
- func (r *Request) Method() string
- func (r *Request) Numeric(field string) float64
- func (r *Request) Protocol() string
- func (r *Request) Referrer() string
- func (r *Request) RemoteAddress() string
- func (r *Request) String(field string) string
- func (r *Request) Timezone(field string) *time.Location
- func (r *Request) URI() *url.URL
- func (r *Request) URL(field string) *url.URL
- func (r *Request) UUID(field string) uuid.UUID
- func (r *Request) UserAgent() string
- type Response
- func (r *Response) Cookie(cookie *http.Cookie)
- func (r *Response) Download(file string, fileName string) error
- func (r *Response) Error(err interface{}) error
- func (r *Response) File(file string) error
- func (r *Response) Header() http.Header
- func (r *Response) JSON(responseCode int, data interface{}) error
- func (r *Response) Redirect(url string)
- func (r *Response) Status(status int)
- func (r *Response) String(responseCode int, message string) error
- func (r *Response) TemporaryRedirect(url string)
- func (r *Response) Write(data []byte) (int, error)
- func (r *Response) WriteHeader(status int)
- type Router
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsReady ¶
func IsReady() bool
IsReady returns true if the server has finished initializing and is ready to serve incoming requests.
func RegisterStartupHook ¶
func RegisterStartupHook(hook func())
RegisterStartupHook to execute some code once the server is ready and running.
func Start ¶
func Start(routeRegistrer func(*Router))
Start starts the web server. The routeRegistrer parameter is a function aimed at registering all your routes and middlewares.
import ( "github.com/System-Glitch/goyave/v2" "my-project/route" ) func main() { goyave.start(route.Register) }
Panic if the server is already running.
func Stop ¶
func Stop()
Stop gracefully shuts down the server without interrupting any active connections.
Make sure the program doesn't exit and waits instead for Stop to return.
Stop does not attempt to close nor wait for hijacked connections such as WebSockets. The caller of Stop should separately notify such long-lived connections of shutdown and wait for them to close, if desired.
Types ¶
type Handler ¶
Handler is a controller or middleware function
func NativeHandler ¶
NativeHandler is an adapter function for "http.Handler". With this adapter, you can plug non-Goyave handlers to your application.
If the request is a JSON request, the native handler will not be able to read the body, as it has already been parsed by the framework and is stored in the "goyave.Request" object. However, form data can be accessed as usual. Just remember that it contains the raw data, which haven't been validated nor converted. This means that native handlers are not guaranteed to work and cannot modify the request data. Prefer implementing a Goyave handler.
This feature is a compatibility layer with the rest of the Golang web ecosystem. Prefer using Goyave handlers if possible.
type Middleware ¶
Middleware function generating middleware handler function.
Request data is available to middlewares, but bear in mind that it had not been validated yet. That means that you can modify or filter data. (Trim strings for example)
func NativeMiddleware ¶
func NativeMiddleware(middleware mux.MiddlewareFunc) Middleware
NativeMiddleware is an adapter function "mux.MiddlewareFunc". With this adapter, you can plug Gorilla Mux middleware to your application.
Native middleware work like native handlers. See "NativeHandler" for more details.
type Request ¶
type Request struct { Rules validation.RuleSet Data map[string]interface{} Params map[string]string Lang string // contains filtered or unexported fields }
Request struct represents an http request. Contains the validated body in the Data attribute if the route was defined with a request generator function
func (*Request) Bool ¶
Bool get a bool field from the request data. Panics if the field is not a bool.
func (*Request) ContentLength ¶
ContentLength records the length of the associated content. The value -1 indicates that the length is unknown.
func (*Request) Date ¶
Date get a date field from the request data. Panics if the field is not a date.
func (*Request) File ¶
func (r *Request) File(field string) []filesystem.File
File get a file field from the request data. Panics if the field is not numeric.
func (*Request) Header ¶
Header contains the request header fields either received by the server or to be sent by the client. Header names are case-insensitive.
If the raw request has the following header lines,
Host: example.com accept-encoding: gzip, deflate Accept-Language: en-us fOO: Bar foo: two
then the header map will look like this:
Header = map[string][]string{ "Accept-Encoding": {"gzip, deflate"}, "Accept-Language": {"en-us"}, "Foo": {"Bar", "two"}, }
func (*Request) Integer ¶
Integer get an integer field from the request data. Panics if the field is not an integer.
func (*Request) Numeric ¶
Numeric get a numeric field from the request data. Panics if the field is not numeric.
func (*Request) RemoteAddress ¶
RemoteAddress allows to record the network address that sent the request, usually for logging.
func (*Request) String ¶
String get a string field from the request data. Panics if the field is not a string.
func (*Request) Timezone ¶
Timezone get a timezone field from the request data. Panics if the field is not a timezone.
func (*Request) URI ¶
URI specifies the URI being requested. Use this if you absolutely need the raw query params, url, etc. Otherwise use the provided methods and fields of the "goyave.Request".
func (*Request) URL ¶
URL get an URL field from the request data. Panics if the field is not an URL.
type Response ¶
type Response struct { http.ResponseWriter // contains filtered or unexported fields }
Response represents a controller response.
func CreateTestResponse ¶
func CreateTestResponse(recorder http.ResponseWriter) *Response
CreateTestResponse create an empty response with the given response writer. This function is aimed at making it easier to unit test Responses.
writer := httptest.NewRecorder() response := goyave.CreateTestResponse(writer) response.Status(http.StatusNoContent) result := writer.Result() fmt.Println(result.StatusCode) // 204
func (*Response) Cookie ¶
Cookie add a Set-Cookie header to the response. The provided cookie must have a valid Name. Invalid cookies may be silently dropped.
func (*Response) Download ¶
Download write a file as an attachment element. Automatically detects the file MIME type and sets the "Content-Type" header accordingly. It is advised to call "filesystem.FileExists()" before sending a file to avoid a panic and return a 404 error if the file doesn't exist. The given path can be relative or absolute.
The "fileName" parameter defines the name the client will see. In other words, it sets the header "Content-Disposition" to "attachment; filename="${fileName}""
If you want the file to be sent as an inline element ("Content-Disposition: inline"), use the "File" function instead.
func (*Response) Error ¶
Error print the error in the console and return it with an error code 500. If debugging is enabled in the config, the error is also written in the response and the stacktrace is printed in the console.
func (*Response) File ¶
File write a file as an inline element. Automatically detects the file MIME type and sets the "Content-Type" header accordingly. It is advised to call "filesystem.FileExists()" before sending a file to avoid a panic and return a 404 error if the file doesn't exist. The given path can be relative or absolute.
If you want the file to be sent as a download ("Content-Disposition: attachment"), use the "Download" function instead.
func (*Response) JSON ¶
JSON write json data as a response. Also sets the "Content-Type" header automatically
func (*Response) TemporaryRedirect ¶
TemporaryRedirect send a temporary redirect response
func (*Response) WriteHeader ¶
WriteHeader sends an HTTP response header with the provided status code.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router registers routes to be matched and dispatches a handler.
func (*Router) Middleware ¶
func (r *Router) Middleware(middleware ...Middleware)
Middleware apply one or more middleware(s) to the route group.
func (*Router) Route ¶
func (r *Router) Route(methods string, uri string, handler Handler, validationRules validation.RuleSet)
Route register a new route.
Multiple methods can be passed using a pipe-separated string.
"PUT|PATCH"
The validation rules set is optional. If you don't want your route to be validated, pass "nil".
func (*Router) Static ¶
Static serve a directory and its subdirectories of static resources. Set the "download" parameter to true if you want the files to be sent as an attachment instead of an inline element.
If no file is given in the url, or if the given file is a directory, the handler will send the "index.html" file if it exists.