Back to

Package goyave

Latest Go to latest

The latest major version is v3.

Published: Nov 17, 2019 | License: MIT | Module:


func ClearStartupHooks

func ClearStartupHooks()

ClearStartupHooks removes all startup hooks.

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 (

 func main() {

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.

type Handler

type Handler func(*Response, *Request)

Handler is a controller or middleware function

type Middleware

type Middleware func(Handler) Handler

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)

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) ContentLength

func (r *Request) ContentLength() int64

ContentLength records the length of the associated content. The value -1 indicates that the length is unknown.

func (*Request) Cookies

func (r *Request) Cookies(name string) []*http.Cookie

Cookies returns the HTTP cookies sent with the request.

func (*Request) Header

func (r *Request) Header() http.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,

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) Method

func (r *Request) Method() string

Method specifies the HTTP method (GET, POST, PUT, etc.).

func (*Request) Protocol

func (r *Request) Protocol() string

Protocol the protocol used by this request, "HTTP/1.1" for example.

func (*Request) Referrer

func (r *Request) Referrer() string

Referrer returns the referring URL, if sent in the request.

func (*Request) RemoteAddress

func (r *Request) RemoteAddress() string

RemoteAddress allows to record the network address that sent the request, usually for logging.

func (*Request) URL

func (r *Request) URL() *url.URL

URL 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) UserAgent

func (r *Request) UserAgent() string

UserAgent returns the client's User-Agent, if sent in the request.

type Response

type Response struct {
	// 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)
result := writer.Result()
fmt.Println(result.StatusCode) // 204

func (*Response) Cookie

func (r *Response) Cookie(cookie *http.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

func (r *Response) Download(file string, fileName string)

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

func (r *Response) Error(err interface{})

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

func (r *Response) File(file string)

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) Header

func (r *Response) Header() http.Header

Header returns the header map that will be sent.

func (*Response) JSON

func (r *Response) JSON(responseCode int, data interface{})

JSON write json data as a response. Also sets the "Content-Type" header automatically

func (*Response) Redirect

func (r *Response) Redirect(url string)

Redirect send a permanent redirect response

func (*Response) Status

func (r *Response) Status(status int)

Status write the given status code

func (*Response) String

func (r *Response) String(responseCode int, message string)

String write a string as a response

func (*Response) TemporaryRedirect

func (r *Response) TemporaryRedirect(url string)

TemporaryRedirect send a temporary redirect response

func (*Response) Write

func (r *Response) Write(data []byte)

Write writes the data as a response.

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.


The validation rules set is optional. If you don't want your route to be validated, pass "nil".

func (*Router) Static

func (r *Router) Static(uri string, directory string, download bool)

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.

func (*Router) Subrouter

func (r *Router) Subrouter(prefix string) *Router

Subrouter create a new sub-router from this router. Use subrouters to create route groups and to apply middleware to multiple routes.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier