docs/advanced-guide/overriding-default/page.md
GoFr allows overriding default behavior of its features.
GoFr by default wraps a handler's return value and assigns it to the data field in a response.
package main
import "gofr.dev/pkg/gofr"
type user struct {
ID int `json:"id"`
Name string `json:"name"`
}
func main() {
app := gofr.New()
app.GET("/users", func(ctx *gofr.Context) (any, error) {
users := []user{{ID: 1, Name: "Daria"}, {ID: 2, Name: "Ihor"}}
return users, nil
})
app.Run()
}
Response example:
{
"data": [
{
"id": 1,
"name": "Daria"
},
{
"id": 2,
"name": "Ihor"
}
]
}
If you want to have a raw response structure - wrap it in response.Raw:
app.GET("/users", func(ctx *gofr.Context) (any, error) {
users := []user{{ID: 1, Name: "Daria"}, {ID: 2, Name: "Ihor"}}
return response.Raw{Data: users}, nil
})
Response example:
[
{
"id": 1,
"name": "Daria"
},
{
"id": 2,
"name": "Ihor"
}
]
If you need to respond with XML without JSON encoding, return response.XML. It bypasses JSON encoding just like response.File or response.Template and writes the bytes directly to the client. The ContentType defaults to application/xml but can be overridden.
app.GET("/legacy/xml", func(ctx *gofr.Context) (any, error) {
payload := []byte(`<Response status="ok"><Message>Hello</Message></Response>`)
return response.XML{Content: payload}, nil
})
<Response status="ok"><Message>Hello</Message></Response>
GoFr makes it easy to render HTML and HTMX templates directly from your handlers using the response.Template type. By convention, all template files—whether HTML or HTMX—should be placed inside a templates directory located at the root of your project.
package main
import (
"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/http/response"
)
func main() {
app := gofr.New()
app.GET("/list", listHandler)
app.AddStaticFiles("/", "./static")
app.Run()
}
type Todo struct {
Title string
Done bool
}
type TodoPageData struct {
PageTitle string
Todos []Todo
}
func listHandler(ctx *gofr.Context) (any, error) {
// Get data from somewhere
data := TodoPageData{
PageTitle: "My TODO list",
Todos: []Todo{
{Title: "Expand on Gofr documentation ", Done: false},
{Title: "Add more examples", Done: true},
{Title: "Write some articles", Done: false},
},
}
return response.Template{Data: data, Name: "todo.html"}, nil
}
GoFr allows redirecting HTTP requests to other URLs using the response.Redirect type.
package main
import (
"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/http/response"
)
func main() {
app := gofr.New()
app.GET("/old-page", func(ctx *gofr.Context) (any, error) {
// Redirect to a new URL
return response.Redirect{URL: "https://example.com/new-page"}, nil
})
app.Run()
}
In GoFr, the following HTTP methods can be redirected, along with their corresponding status codes:
By default, GoFr loads its own favicon.ico present in root directory for an application. To override favicon.ico user
can place its custom icon in the static directory of its application.
[!NOTE] The custom favicon should also be named as
favicon.icoin the static directory of application.