Responses
NoteIMPORTANT!
The response file should be placed in directoryinternal/http/response
How to response error, string, HTML, JSON, download, View data
// Simple
return errors.NotImplemented
// Structure JSON
return c.Error(response.Error{
Code: "BAD_REQUEST",
Message: "Invalid JWT token",
}, core.StatusUnauthorized)
// String
return c.String("Hello world")
// HTML
return c.HTML("<h2>Hello world</h2>")
return c.Status(gfly.StatusOK).JSON(response.SignIn{
Access: tokens.Access,
Refresh: tokens.Refresh,
})
return c.Download("./storage/logs/logs.log", "Log_file.log")
func (m *UploadFormPage) Handle(c *gfly.Ctx) error {
return c.View("upload", gfly.ViewData{})
}
View template resources/views/upload.tpl
The framework provides standard response structures located in internal/http/response:
Meta - Pagination metadata structure
// Meta struct to describe pagination metadata information.
// @Description Contains pagination metadata including current page, items per page, and total count
type Meta struct {
Page int `json:"page,omitempty" example:"1" doc:"Current page number"`
PerPage int `json:"per_page,omitempty" example:"10" doc:"Number of items per page"`
Total int `json:"total" example:"1354" doc:"Total number of records"`
}
List[T] - Generic list response with pagination
// List struct to describe a generic list response.
// @Description Generic list response structure
type List[T any] struct {
Meta Meta `json:"meta" example:"{\"page\":1,\"per_page\":10,\"total\":100}" doc:"Metadata information for pagination"`
Data []T `json:"data" example:"[]" doc:"List of category data"`
}
Success - Generic success response
// Success struct to describe a generic success response.
// @Description Generic success response structure
type Success struct {
Message string `json:"message" example:"Operation completed successfully"` // Success message description
Data core.Data `json:"data" doc:"Additional data related to the operation"` // Optional data related to the success operation
}
Error - Generic error response structure
// Error struct to describe login response.
// @Description Generic error response structure
type Error struct {
Code string `json:"code" example:"BAD_REQUEST"` // Error code
Message string `json:"message" example:"Bad request"` // Error message description
Data core.Data `json:"data"` // Useful for validation's errors
}