Documentation
¶
Index ¶
- Constants
- Variables
- func CookieRead(r *http.Request, name string) (string, error)
- func CookieReadEncrypted(r *http.Request, name string, secretKey []byte) (string, error)
- func CookieWrite(w http.ResponseWriter, cookie http.Cookie) error
- func CookieWriteEncrypted(w http.ResponseWriter, cookie http.Cookie, secretKey []byte) error
- func CreateAuthTokenHashedCacheKey(userID uint, userAgent string) string
- func DisableEvents()
- func EnableEvents()
- func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
- func NewGorm() *gorm.DB
- func NewTemplates(components embed.FS, templates embed.FS)
- func RenderNamedTemplate(name string, data interface{}) (template.HTML, error)
- func ResolveGorm() *gorm.DB
- func SetCookie(w http.ResponseWriter, email string, token string) error
- func UseHook(mw Hook)
- type App
- func (app *App) Bootstrap()
- func (app *App) MakeDirs(dirs ...string)
- func (app *App) Next(c *Context)
- func (app *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httprouter.Router
- func (app *App) RegisterTemplates(templates_resources embed.FS)
- func (app *App) Run(router *httprouter.Router)
- func (app *App) SetBasePath(path string)
- func (app *App) SetCacheConfig(c CacheConfig)
- func (app *App) SetGormConfig(g GormConfig)
- func (app *App) SetLogsDriver(d logger.LogsDriver)
- func (app *App) SetRequestConfig(r RequestConfig)
- func (app *App) SetRunMode(runmode string)
- type Asynqtask
- type Cache
- type CacheConfig
- type Context
- func (c *Context) CastToFloat(value interface{}) float64
- func (c *Context) CastToInt(value interface{}) int
- func (c *Context) CastToString(value interface{}) string
- func (c *Context) CopyFile(sourceFilePath string, destFolderPath string) error
- func (c *Context) DebugAny(variable interface{})
- func (c *Context) GetBaseDirPath() string
- func (c *Context) GetCookie() (UserCookie, error)
- func (c *Context) GetHeader(key string) string
- func (c *Context) GetPathParam(key string) interface{}
- func (c *Context) GetQueueClient() *asynq.Client
- func (c *Context) GetRequesBodyMap() map[string]interface{}
- func (c *Context) GetRequesBodyStruct(dest interface{}) error
- func (c *Context) GetRequesForm(key string) interface{}
- func (c *Context) GetRequestParam(key string) interface{}
- func (c *Context) GetUploadedFile(name string) (*UploadedFileInfo, error)
- func (c Context) GetUserAgent() string
- func (c *Context) MapToJson(v any) string
- func (c *Context) MoveFile(sourceFilePath string, destFolderPath string) error
- func (c *Context) Next()
- func (c *Context) RequestParamExists(key string) bool
- type Controller
- type EmailAddress
- type EmailAttachment
- type EnvFileConfig
- type Event
- type EventJob
- type EventsManager
- type GormConfig
- type Hashing
- type Hook
- type Hooks
- type JWT
- type JWTConfig
- type JWTOptions
- type Mailer
- func (m *Mailer) Send() error
- func (m *Mailer) SetAttachments(attachments []EmailAttachment) *Mailer
- func (m *Mailer) SetBCC(emailAddresses []EmailAddress) *Mailer
- func (m *Mailer) SetCC(emailAddresses []EmailAddress) *Mailer
- func (m *Mailer) SetFrom(emailAddresses EmailAddress) *Mailer
- func (m *Mailer) SetHTMLBody(body string) *Mailer
- func (m *Mailer) SetPlainTextBody(body string) *Mailer
- func (m *Mailer) SetSubject(subject string) *Mailer
- func (m *Mailer) SetTo(emailAddresses []EmailAddress) *Mailer
- type QueueConfig
- type Queuemux
- type Request
- type RequestConfig
- type Response
- func (rs *Response) Any(body any) *Response
- func (rs *Response) BufferFile(name string, filetype string, b bytes.Buffer) *Response
- func (rs *Response) BufferInline(name string, filetype string, b bytes.Buffer) *Response
- func (rs *Response) ForceSendResponse()
- func (rs *Response) HTML(body string) *Response
- func (rs *Response) Json(body string) *Response
- func (rs *Response) Redirect(url string, use303 ...bool) *Response
- func (rs *Response) SetContentType(c string) *Response
- func (rs *Response) SetHeader(key string, val string) *Response
- func (rs *Response) SetStatusCode(code int) *Response
- func (rs *Response) Template(name string, data interface{}) *Response
- func (rs *Response) Text(body string) *Response
- type Route
- type Router
- func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Router
- func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router
- func (r *Router) GetRoutes() []Route
- func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router
- func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Router
- func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Router
- func (r *Router) Post(path string, controller Controller, hooks ...Hook) *Router
- func (r *Router) Put(path string, controller Controller, hooks ...Hook) *Router
- type SessionUser
- func (s *SessionUser) Delete(key string) interface{}
- func (s *SessionUser) Flush() error
- func (s *SessionUser) Get(key string) (interface{}, bool)
- func (s *SessionUser) GetUserID() uint
- func (s *SessionUser) Init(c *Context) bool
- func (s *SessionUser) IsAuthenticated() bool
- func (s *SessionUser) Save() error
- func (s *SessionUser) Set(key string, value interface{}) error
- type UploadedFileInfo
- type UserCookie
- type Validator
Constants ¶
const CONTENT_TYPE string = "content-Type"
const CONTENT_TYPE_HTML string = "text/html; charset=utf-8"
const CONTENT_TYPE_IMAGEJPG string = "image/jpeg"
const CONTENT_TYPE_IMAGEPNG string = "image/png"
const CONTENT_TYPE_IMAGESVGXML string = "image/svg+xml"
const CONTENT_TYPE_JSON string = "application/json"
const CONTENT_TYPE_MULTIPART_FORM_DATA string = "multipart/form-data;"
const CONTENT_TYPE_TEXT string = "text/plain"
const DELETE string = "delete"
const GET string = "get"
const HEAD string = "head"
const LOCALHOST string = "http://localhost"
const OPTIONS string = "options"
const PATCH string = "patch"
const POST string = "post"
const PRODUCTION string = "production"
const PUT string = "put"
const TEST_STR string = "Testing!"
Variables ¶
var ( ErrValueTooLong = errors.New("cookie value too long") ErrInvalidValue = errors.New("invalid cookie value") )
ErrValueTooLong indicates that the cookie value exceeds the allowed length limit. ErrInvalidValue signifies that the cookie value is in an invalid format.
Functions ¶
func CookieRead ¶ added in v1.8.0
CookieRead retrieves a base64-encoded cookie value by name from the HTTP request and decodes it. Returns the decoded value as a string or an error if the cookie is not found or the value is invalid.
func CookieReadEncrypted ¶ added in v1.8.0
CookieReadEncrypted reads an encrypted cookie, decrypts its value using AES-GCM, and validates its name before returning. Returns the plaintext cookie value or an error if decryption or validation fails.
func CookieWrite ¶ added in v1.8.0
func CookieWrite(w http.ResponseWriter, cookie http.Cookie) error
CookieWrite writes a secure HTTP cookie to the response writer after base64 encoding its value. Returns ErrValueTooLong if the cookie string exceeds the 4096-byte size limit.
func CookieWriteEncrypted ¶ added in v1.8.0
CookieWriteEncrypted encrypts the cookie's value using AES-GCM and writes it to the HTTP response writer. The cookie name is authenticated along with its value. It returns an error if encryption fails or the cookie exceeds the maximum allowed length.
func CreateAuthTokenHashedCacheKey ¶ added in v1.9.8
CreateAuthTokenHashedCacheKey generates a hashed cache key used to store JWT tokens and session data. The key is based on the user ID and user agent, hashed with MD5.
func DisableEvents ¶
func DisableEvents()
DisableEvents sets the global variable `disableEvents` to true, disabling the handling or triggering of events.
func EnableEvents ¶
func EnableEvents()
EnableEvents sets the global variable `disableEvents` to false, effectively enabling event handling.
func Graph ¶ added in v1.8.2
func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
func NewGorm ¶
NewGorm initializes and returns a new gorm.DB instance based on the selected database driver specified in environment variables. Supported drivers include MySQL, PostgreSQL, and SQLite. Panics if the database driver is not specified or if a connection error occurs.
func RenderNamedTemplate ¶ added in v1.9.8
RenderNamedTemplate executes a named template from the registered set with the given data and returns the rendered HTML. Usage:
html, err := core.RenderNamedTemplate("tabler_table", data)
if err != nil {
// handle error
}
return c.Response.HTML(string(html))
func ResolveGorm ¶
ResolveGorm initializes and returns a singleton instance of *gorm.DB, creating it if it doesn't already exist.
func SetCookie ¶ added in v1.8.0
func SetCookie(w http.ResponseWriter, email string, token string) error
SetCookie sets an encrypted cookie with a user's email and token, using gob encoding for data serialization. The Secure flag is controlled by the COOKIE_SECURE environment variable (defaults to true, set to false for local HTTP development).
Types ¶
type App ¶
type App struct {
Config *configContainer
// contains filtered or unexported fields
}
App is a struct representing the main application container and its core components.
func New ¶
func New() *App
New initializes and returns a new instance of the App structure with default configurations and chain.
func ResolveApp ¶
func ResolveApp() *App
ResolveApp returns the global instance of the App, providing access to its methods and configuration components.
func (*App) Bootstrap ¶
func (app *App) Bootstrap()
Bootstrap initializes the application by setting up the logger, router, and events manager.
func (*App) MakeDirs ¶
MakeDirs creates the specified directories under the base path with the provided permissions (0766).
func (*App) Next ¶
Next advances to the next middleware or controller in the chain and invokes it with the given context if available.
func (*App) RegisterRoutes ¶
func (app *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httprouter.Router
RegisterRoutes sets up and registers HTTP routes and their handlers to the provided router, enabling request processing. It assigns appropriate methods, controllers, and hooks, while handling core services, errors, and fallback behaviors. Returns the configured router instance.
func (*App) RegisterTemplates ¶ added in v1.8.0
RegisterTemplates initializes the application template system by embedding provided templates for rendering views.
func (*App) Run ¶
func (app *App) Run(router *httprouter.Router)
Run initializes the application's router, configures HTTPS if enabled, and starts the HTTP/HTTPS server.
func (*App) SetBasePath ¶
SetBasePath sets the base path for the application, which is used as a prefix for file and directory operations.
func (*App) SetCacheConfig ¶
func (app *App) SetCacheConfig(c CacheConfig)
SetCacheConfig sets the cache configuration for the application using the provided CacheConfig parameter.
func (*App) SetGormConfig ¶
func (app *App) SetGormConfig(g GormConfig)
SetGormConfig sets the GormConfig for the application, overriding the current configuration with the provided value.
func (*App) SetLogsDriver ¶
func (app *App) SetLogsDriver(d logger.LogsDriver)
SetLogsDriver sets the logging driver to be used by the application.
func (*App) SetRequestConfig ¶
func (app *App) SetRequestConfig(r RequestConfig)
SetRequestConfig sets the configuration for the request in the application instance.
func (*App) SetRunMode ¶ added in v1.8.3
SetRunMode sets the application run mode to the specified value.
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
func NewCache ¶
func NewCache(cacheConfig CacheConfig) *Cache
NewCache initializes a new Cache instance with the provided configuration and connects to a Redis database. If caching is enabled in the provided configuration but the connection to Redis fails, it causes a panic. Returns a pointer to the initialized Cache structure.
func (*Cache) Delete ¶
Delete removes the specified key from the cache and returns an error if the operation fails.
func (*Cache) Get ¶
Get retrieves the value associated with the provided key from the cache. Returns an error if the operation fails.
type CacheConfig ¶
type CacheConfig struct {
EnableCache bool
}
type Context ¶
type Context struct {
Request *Request
Response *Response
GetValidator func() *Validator
GetJWT func() *JWT
GetGorm func() *gorm.DB
GetCache func() *Cache
GetHashing func() *Hashing
GetMailer func() *Mailer
GetEventsManager func() *EventsManager
GetLogger func() *logger.Logger
GetSession func() *SessionUser
}
func (*Context) CastToFloat ¶
func (*Context) CastToString ¶
func (*Context) GetBaseDirPath ¶
func (*Context) GetCookie ¶ added in v1.8.0
func (c *Context) GetCookie() (UserCookie, error)
func (*Context) GetPathParam ¶
func (*Context) GetQueueClient ¶ added in v1.9.0
func (*Context) GetRequesBodyMap ¶ added in v1.8.0
func (*Context) GetRequesBodyStruct ¶ added in v1.8.0
get json body and bind to dest interface
func (*Context) GetRequesForm ¶ added in v1.8.0
func (*Context) GetRequestParam ¶
func (*Context) GetUploadedFile ¶
func (c *Context) GetUploadedFile(name string) (*UploadedFileInfo, error)
func (Context) GetUserAgent ¶
func (*Context) RequestParamExists ¶
type Controller ¶ added in v1.7.3
type EmailAddress ¶
type EmailAttachment ¶
type EnvFileConfig ¶
type EnvFileConfig struct {
UseDotEnvFile bool
}
type EventsManager ¶
type EventsManager struct {
// contains filtered or unexported fields
}
func NewEventsManager ¶
func NewEventsManager() *EventsManager
func ResolveEventsManager ¶
func ResolveEventsManager() *EventsManager
func (*EventsManager) Fire ¶
func (m *EventsManager) Fire(e *Event) error
func (*EventsManager) Register ¶
func (m *EventsManager) Register(eName string, job EventJob)
type GormConfig ¶
type GormConfig struct {
EnableGorm bool
}
type Hooks ¶ added in v1.7.3
type Hooks struct {
// contains filtered or unexported fields
}
func ResolveHooks ¶ added in v1.7.3
func ResolveHooks() *Hooks
type JWT ¶
type JWT struct {
// contains filtered or unexported fields
}
func (*JWT) DecodeToken ¶
func (*JWT) GenerateToken ¶
type JWTOptions ¶
type Mailer ¶
type Mailer struct {
// contains filtered or unexported fields
}
func (*Mailer) SetAttachments ¶
func (m *Mailer) SetAttachments(attachments []EmailAttachment) *Mailer
func (*Mailer) SetBCC ¶
func (m *Mailer) SetBCC(emailAddresses []EmailAddress) *Mailer
func (*Mailer) SetCC ¶
func (m *Mailer) SetCC(emailAddresses []EmailAddress) *Mailer
func (*Mailer) SetFrom ¶
func (m *Mailer) SetFrom(emailAddresses EmailAddress) *Mailer
func (*Mailer) SetHTMLBody ¶
func (*Mailer) SetPlainTextBody ¶
func (*Mailer) SetSubject ¶
func (*Mailer) SetTo ¶
func (m *Mailer) SetTo(emailAddresses []EmailAddress) *Mailer
type QueueConfig ¶ added in v1.9.0
type Queuemux ¶ added in v1.9.0
type Queuemux struct {
// contains filtered or unexported fields
}
func (*Queuemux) RunQueueserver ¶ added in v1.9.0
func (q *Queuemux) RunQueueserver(config QueueConfig)
RunQueueserver starts the queue server with the given configuration and handles tasks using the assigned ServeMux. Configures the queue server with concurrency limits and priority-based queue management. Logs and terminates the program if the server fails to run.
type RequestConfig ¶
type RequestConfig struct {
MaxUploadFileSize int
}
type Response ¶
type Response struct {
HttpResponseWriter http.ResponseWriter
// contains filtered or unexported fields
}
Response represents an HTTP response to be sent to the client, including headers, body, status code, and metadata.
func (*Response) Any ¶
sets the response's content type to HTML and assigns the provided body as the response body if not terminated.
func (*Response) BufferFile ¶ added in v1.9.0
writes the contents of a buffer to the HTTP response with specified file name and type if not terminated.
func (*Response) BufferInline ¶ added in v1.9.3
writes the contents of a buffer to the HTTP response with specified file name and type if not terminated.
func (*Response) ForceSendResponse ¶
func (rs *Response) ForceSendResponse()
terminates the response processing, preventing any further modifications or actions.
func (*Response) HTML ¶
sets the response's content type to HTML and assigns the provided string as the body if the response is not terminated.
func (*Response) Json ¶
sets the response's content type to JSON and assigns the provided string as the body if the response is not terminated.
func (*Response) Redirect ¶
Redirect sends a redirect response to the given URL. By default it uses 307 (Temporary Redirect) to preserve the HTTP method. Pass true as the second argument to use 303 (See Other), which changes POST to GET.
func (*Response) SetContentType ¶
sets a custom content type for the response if it is not terminated. Returns the updated Response object.
func (*Response) SetHeader ¶
adds or updates a header to the response if it is not terminated. Returns the updated Response object.
func (*Response) SetStatusCode ¶
sets the response status code if the response is not yet terminated. Returns the updated Response object.
type Route ¶
type Route struct {
Method string
Path string
Controller Controller
Hooks []Hook
}
Route defines an HTTP route with a method, path, controller function, and optional hooks for customization.
type Router ¶
type Router struct {
Routes []Route
}
Router represents a structure for storing and managing routes in the application.
func NewRouter ¶
func NewRouter() *Router
NewRouter initializes and returns a new Router instance with an empty slice of routes.
func ResolveRouter ¶
func ResolveRouter() *Router
ResolveRouter returns the singleton instance of the Router. It initializes and manages HTTP routes configuration.
func (*Router) Delete ¶
func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Router
Delete registers a DELETE HTTP method route with the specified path, controller, and optional hooks.
func (*Router) Get ¶
func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router
Get registers a GET route with the specified path, controller, and optional hooks, returning the updated Router.
func (*Router) GetRoutes ¶
GetRoutes returns a slice of Route objects currently registered within the Router.
func (*Router) Head ¶
func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router
Head registers a route with the HTTP HEAD method, associates it with a path, controller, and optional hooks.
func (*Router) Options ¶
func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Router
Options registers a new route with the OPTIONS HTTP method, a specified path, controller, and optional hooks.
func (*Router) Patch ¶
func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Router
Patch registers a new route with the HTTP PATCH method, a specified path, a controller, and optional hooks.
type SessionUser ¶ added in v1.9.8
type SessionUser struct {
// contains filtered or unexported fields
}
SessionUser handles user session data management with thread-safe operations. Sessions are stored in the cache (Redis) and are tied to an authenticated user via JWT tokens stored in cookies.
func (*SessionUser) Delete ¶ added in v1.9.8
func (s *SessionUser) Delete(key string) interface{}
Delete removes a specific key from the session and persists the change. Returns the deleted value if it existed.
func (*SessionUser) Flush ¶ added in v1.9.8
func (s *SessionUser) Flush() error
Flush deletes all session data from the cache.
func (*SessionUser) Get ¶ added in v1.9.8
func (s *SessionUser) Get(key string) (interface{}, bool)
Get retrieves a value from the session. Returns the value and a boolean indicating if the key exists.
func (*SessionUser) GetUserID ¶ added in v1.9.8
func (s *SessionUser) GetUserID() uint
GetUserID returns the user ID of the authenticated session user.
func (*SessionUser) Init ¶ added in v1.9.8
func (s *SessionUser) Init(c *Context) bool
Init initializes the session by validating the user's JWT token from the cookie. It checks the cached token, verifies it matches, and loads any existing session data. Returns true if the session was successfully established.
func (*SessionUser) IsAuthenticated ¶ added in v1.9.8
func (s *SessionUser) IsAuthenticated() bool
IsAuthenticated returns whether the session has been successfully authenticated.
func (*SessionUser) Save ¶ added in v1.9.8
func (s *SessionUser) Save() error
Save persists the current session values to the cache.
func (*SessionUser) Set ¶ added in v1.9.8
func (s *SessionUser) Set(key string, value interface{}) error
Set stores a value in the session and persists it to the cache.
type UploadedFileInfo ¶
type UserCookie ¶ added in v1.8.0
UserCookie represents a structure to hold user-specific data stored in a cookie, including Email and Token fields.