Compare commits

..

32 commits
main ... main

Author SHA1 Message Date
3d67bc0a03 Merge pull request 'develop' (#27) from develop into main
Reviewed-on: goffee/core#27
2025-09-18 01:12:53 -04:00
6b0e2e4739 Merge pull request 'main' (#26) from diana/core:main into develop
Reviewed-on: goffee/core#26
2025-09-18 01:11:31 -04:00
e8f7ab40b5 Implemented a thread-safe solution to the race condition that was causing random route overlaps 2025-09-18 00:11:08 -05:00
3c0b56c433 add id 2025-08-26 13:31:13 -05:00
5c6be4b037 Merge pull request 'main' (#6) from goffee/core:main into main
Reviewed-on: diana/core#6
2025-08-26 13:39:08 -04:00
850d2ae477 Merge pull request 'develop' (#25) from develop into main
Reviewed-on: goffee/core#25
2025-07-15 11:54:08 -04:00
5f0ca8e797 add options for cdn and app 2025-07-15 10:53:18 -05:00
c9cb539d18 check child items to active nav 2025-04-28 00:12:43 -05:00
30746a0602 check child items to active nav 2025-04-28 00:11:04 -05:00
e43ace6679 Merge pull request 'main' (#24) from main into develop
Reviewed-on: goffee/core#24
2025-04-28 00:47:40 -04:00
653fd5c64e add ID to PageNavItem 2025-04-27 23:41:25 -05:00
f1772b99f3 Merge pull request 'develop' (#23) from develop into main
Reviewed-on: goffee/core#23
2025-04-17 02:52:54 -04:00
f276f4d61d add element paginator 2025-04-17 01:49:43 -05:00
5c3559c793 Merge pull request 'template with select multiple, input custom attribute' (#22) from jacs/core:develop into develop
Reviewed-on: goffee/core#22
2025-04-16 20:59:45 -04:00
jacs
e3748c853f template with select multiple, input custom attribute 2025-04-15 07:23:34 -05:00
259f2f4b79 add img and class to table template 2025-03-27 12:41:17 -05:00
cc8c79fe3d add img and class to table template 2025-03-27 12:19:18 -05:00
db3c510f9a add img and class to table template, new responnse buffer inline 2025-03-27 12:12:49 -05:00
1a39c666a3 fix 2025-03-18 23:43:09 -05:00
790c840f76 split if 2025-03-18 20:14:52 -05:00
a71b3697b6 Merge pull request 'develop' (#21) from develop into main
Reviewed-on: goffee/core#21
2025-03-07 10:44:06 -05:00
530a1171e6 Merge pull request 'develop' (#20) from jacs/core:develop into develop
Reviewed-on: goffee/core#20
2025-03-07 10:42:26 -05:00
3b6fa12911 change panic by err 2025-03-05 15:12:11 -05:00
9816e58e7c Merge pull request 'main' (#1) from goffee/core:main into main
Reviewed-on: jacs/core#1
2025-03-05 11:54:06 -05:00
695f1f57ba add documentation 2025-02-24 09:59:07 -05:00
1b23363f6f Merge pull request 'add config option to disable queues' (#19) from develop into main
Reviewed-on: goffee/core#19
2024-12-23 23:44:23 -05:00
0db37d31b8 add config option to disable queues 2024-12-23 23:41:27 -05:00
deb119db84 Merge pull request 'develop' (#18) from develop into main
Reviewed-on: goffee/core#18
2024-12-23 23:13:02 -05:00
b274d3268f add base queue system 2024-12-23 23:11:49 -05:00
4968da25f3 change BufferPDF to BufferFile to support any kind of file 2024-12-22 10:42:56 -05:00
0afafd8c41 Merge pull request 'improve GetRequestForm, extend templates' (#17) from develop into main
Reviewed-on: goffee/core#17
2024-12-18 08:19:48 -05:00
90564daa5b improve GetRequestForm, extend templates 2024-12-17 14:26:57 -05:00
25 changed files with 399 additions and 61 deletions

3
.gitignore vendored
View file

@ -1,2 +1,3 @@
tmp tmp
.DS_STore .DS_STore
.idea

View file

@ -1,4 +1,3 @@
# GoCondor Core # Goffee Core
![Build Status](https://github.com/gocondor/core/actions/workflows/build-main.yml/badge.svg) ![Test Status](https://github.com/gocondor/core/actions/workflows/test-main.yml/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/gocondor/core/badge.svg?branch=main)](https://coveralls.io/github/gocondor/core?branch=main&cache=false) [![GoDoc](https://godoc.org/github.com/gocondor/core?status.svg)](https://godoc.org/github.com/gocondor/core) [![Go Report Card](https://goreportcard.com/badge/github.com/gocondor/core)](https://goreportcard.com/report/github.com/gocondor/core)
The core packages of GoCondor framework The core packages of Goffee framework

View file

@ -16,6 +16,9 @@ type Cache struct {
redis *redis.Client redis *redis.Client
} }
// 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 NewCache(cacheConfig CacheConfig) *Cache { func NewCache(cacheConfig CacheConfig) *Cache {
ctx = context.Background() ctx = context.Background()
dbStr := os.Getenv("REDIS_DB") dbStr := os.Getenv("REDIS_DB")
@ -40,6 +43,7 @@ func NewCache(cacheConfig CacheConfig) *Cache {
} }
} }
// Set stores a key-value pair in the Redis cache with no expiration. Returns an error if the operation fails.
func (c *Cache) Set(key string, value string) error { func (c *Cache) Set(key string, value string) error {
err := c.redis.Set(ctx, key, value, 0).Err() err := c.redis.Set(ctx, key, value, 0).Err()
if err != nil { if err != nil {
@ -48,6 +52,8 @@ func (c *Cache) Set(key string, value string) error {
return nil return nil
} }
// SetWithExpiration stores a key-value pair in the cache with a specified expiration duration.
// Returns an error if the operation fails.
func (c *Cache) SetWithExpiration(key string, value string, expiration time.Duration) error { func (c *Cache) SetWithExpiration(key string, value string, expiration time.Duration) error {
err := c.redis.Set(ctx, key, value, expiration).Err() err := c.redis.Set(ctx, key, value, expiration).Err()
if err != nil { if err != nil {
@ -56,6 +62,7 @@ func (c *Cache) SetWithExpiration(key string, value string, expiration time.Dura
return nil return nil
} }
// Get retrieves the value associated with the provided key from the cache. Returns an error if the operation fails.
func (c *Cache) Get(key string) (string, error) { func (c *Cache) Get(key string) (string, error) {
result, err := c.redis.Get(ctx, key).Result() result, err := c.redis.Get(ctx, key).Result()
if err != nil { if err != nil {
@ -64,6 +71,7 @@ func (c *Cache) Get(key string) (string, error) {
return result, nil return result, nil
} }
// Delete removes the specified key from the cache and returns an error if the operation fails.
func (c *Cache) Delete(key string) error { func (c *Cache) Delete(key string) error {
err := c.redis.Del(ctx, key).Err() err := c.redis.Del(ctx, key).Err()
if err != nil { if err != nil {

View file

@ -22,6 +22,10 @@ type GormConfig struct {
EnableGorm bool EnableGorm bool
} }
type QueueConfig struct {
EnableQueue bool
}
type CacheConfig struct { type CacheConfig struct {
EnableCache bool EnableCache bool
} }

View file

@ -19,6 +19,7 @@ import (
"syscall" "syscall"
"git.smarteching.com/goffee/core/logger" "git.smarteching.com/goffee/core/logger"
"github.com/hibiken/asynq"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -53,7 +54,11 @@ func (c *Context) Next() {
} }
func (c *Context) prepare(ctx *Context) { func (c *Context) prepare(ctx *Context) {
ctx.Request.httpRequest.ParseMultipartForm(int64(app.Config.Request.MaxUploadFileSize)) // Only parse multipart form if it hasn't been parsed already
// This prevents race conditions when multiple goroutines might access the same request
if ctx.Request.httpRequest.MultipartForm == nil {
ctx.Request.httpRequest.ParseMultipartForm(int64(app.Config.Request.MaxUploadFileSize))
}
} }
func (c *Context) GetPathParam(key string) interface{} { func (c *Context) GetPathParam(key string) interface{} {
@ -68,8 +73,9 @@ func (c *Context) RequestParamExists(key string) bool {
return c.Request.httpRequest.Form.Has(key) return c.Request.httpRequest.Form.Has(key)
} }
func (c *Context) GetRequesForm() interface{} { func (c *Context) GetRequesForm(key string) interface{} {
return c.Request.httpRequest.Form c.Request.httpRequest.ParseForm()
return c.Request.httpRequest.Form[key]
} }
func (c *Context) GetRequesBodyMap() map[string]interface{} { func (c *Context) GetRequesBodyMap() map[string]interface{} {
@ -111,34 +117,51 @@ func (c *Context) GetCookie() (UserCookie, error) {
return user, nil return user, nil
} }
func (c *Context) GetUploadedFile(name string) *UploadedFileInfo { func (c *Context) GetQueueClient() *asynq.Client {
redisAddr := fmt.Sprintf("%v:%v", os.Getenv("REDIS_HOST"), os.Getenv("REDIS_PORT"))
client := asynq.NewClient(asynq.RedisClientOpt{Addr: redisAddr})
return client
}
func (c *Context) GetUploadedFile(name string) (*UploadedFileInfo, error) {
file, fileHeader, err := c.Request.httpRequest.FormFile(name) file, fileHeader, err := c.Request.httpRequest.FormFile(name)
if err != nil { if err != nil {
panic(fmt.Sprintf("error with file,[%v]", err.Error())) return nil, fmt.Errorf("error retrieving file: %v", err)
} }
defer file.Close() defer file.Close()
// Extract the file extension
ext := strings.TrimPrefix(path.Ext(fileHeader.Filename), ".") ext := strings.TrimPrefix(path.Ext(fileHeader.Filename), ".")
tmpFilePath := filepath.Join(os.TempDir(), fileHeader.Filename) tmpFilePath := filepath.Join(os.TempDir(), fileHeader.Filename)
tmpFile, err := os.Create(tmpFilePath) tmpFile, err := os.Create(tmpFilePath)
if err != nil { if err != nil {
panic(fmt.Sprintf("error with file,[%v]", err.Error())) return nil, fmt.Errorf("error creating temporary file: %v", err)
} }
defer tmpFile.Close()
// Copy the uploaded file content to the temporary file
buff := make([]byte, 100) buff := make([]byte, 100)
for { for {
n, err := file.Read(buff) n, err := file.Read(buff)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
panic("error with uploaded file") return nil, fmt.Errorf("error reading uploaded file: %v", err)
} }
if n == 0 { if n == 0 {
break break
} }
n, _ = tmpFile.Write(buff[:n]) _, err = tmpFile.Write(buff[:n])
if err != nil {
return nil, fmt.Errorf("error writing to temporary file: %v", err)
}
} }
// Get file info for the temporary file
tmpFileInfo, err := os.Stat(tmpFilePath) tmpFileInfo, err := os.Stat(tmpFilePath)
if err != nil { if err != nil {
panic(fmt.Sprintf("error with file,[%v]", err.Error())) return nil, fmt.Errorf("error getting file info: %v", err)
} }
defer tmpFile.Close()
uploadedFileInfo := &UploadedFileInfo{ uploadedFileInfo := &UploadedFileInfo{
FullPath: tmpFilePath, FullPath: tmpFilePath,
Name: fileHeader.Filename, Name: fileHeader.Filename,
@ -146,7 +169,7 @@ func (c *Context) GetUploadedFile(name string) *UploadedFileInfo {
Extension: ext, Extension: ext,
Size: int(tmpFileInfo.Size()), Size: int(tmpFileInfo.Size()),
} }
return uploadedFileInfo return uploadedFileInfo, nil
} }
func (c *Context) MoveFile(sourceFilePath string, destFolderPath string) error { func (c *Context) MoveFile(sourceFilePath string, destFolderPath string) error {
@ -175,7 +198,7 @@ func (c *Context) MoveFile(sourceFilePath string, destFolderPath string) error {
for { for {
n, err := srcFile.Read(buff) n, err := srcFile.Read(buff)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
panic(fmt.Sprintf("error moving file %v", sourceFilePath)) return fmt.Errorf("error moving file: %v", sourceFilePath)
} }
if n == 0 { if n == 0 {
break break
@ -219,7 +242,7 @@ func (c *Context) CopyFile(sourceFilePath string, destFolderPath string) error {
for { for {
n, err := srcFile.Read(buff) n, err := srcFile.Read(buff)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
panic(fmt.Sprintf("error moving file %v", sourceFilePath)) return fmt.Errorf("error moving file: %v", sourceFilePath)
} }
if n == 0 { if n == 0 {
break break

View file

@ -21,19 +21,24 @@ import (
"strings" "strings"
) )
// ErrValueTooLong indicates that the cookie value exceeds the allowed length limit.
// ErrInvalidValue signifies that the cookie value is in an invalid format.
var ( var (
ErrValueTooLong = errors.New("cookie value too long") ErrValueTooLong = errors.New("cookie value too long")
ErrInvalidValue = errors.New("invalid cookie value") ErrInvalidValue = errors.New("invalid cookie value")
) )
// Declare the User type. // UserCookie represents a structure to hold user-specific data stored in a cookie, including Email and Token fields.
type UserCookie struct { type UserCookie struct {
Email string Email string
Token string Token string
} }
// secretcookie is a global variable used to store the decoded secret key for encrypting and decrypting cookies.
var secretcookie []byte var secretcookie []byte
// GetCookie retrieves and decrypts the user cookie from the provided HTTP request and returns it as a UserCookie.
// Returns an error if the cookie retrieval, decryption, or decoding process fails.
func GetCookie(r *http.Request) (UserCookie, error) { func GetCookie(r *http.Request) (UserCookie, error) {
var err error var err error
@ -78,6 +83,7 @@ func GetCookie(r *http.Request) (UserCookie, error) {
} }
// SetCookie sets an encrypted cookie with a user's email and token, using gob encoding for data serialization.
func SetCookie(w http.ResponseWriter, email string, token string) error { func SetCookie(w http.ResponseWriter, email string, token string) error {
// Initialize a User struct containing the data that we want to store in the // Initialize a User struct containing the data that we want to store in the
// cookie. // cookie.
@ -138,6 +144,8 @@ func SetCookie(w http.ResponseWriter, email string, token string) error {
return nil return nil
} }
// 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 CookieWrite(w http.ResponseWriter, cookie http.Cookie) error { func CookieWrite(w http.ResponseWriter, cookie http.Cookie) error {
// Encode the cookie value using base64. // Encode the cookie value using base64.
cookie.Value = base64.URLEncoding.EncodeToString([]byte(cookie.Value)) cookie.Value = base64.URLEncoding.EncodeToString([]byte(cookie.Value))
@ -154,6 +162,8 @@ func CookieWrite(w http.ResponseWriter, cookie http.Cookie) error {
return nil return nil
} }
// 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 CookieRead(r *http.Request, name string) (string, error) { func CookieRead(r *http.Request, name string) (string, error) {
// Read the cookie as normal. // Read the cookie as normal.
cookie, err := r.Cookie(name) cookie, err := r.Cookie(name)
@ -173,6 +183,9 @@ func CookieRead(r *http.Request, name string) (string, error) {
return string(value), nil return string(value), nil
} }
// 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 CookieWriteEncrypted(w http.ResponseWriter, cookie http.Cookie, secretKey []byte) error { func CookieWriteEncrypted(w http.ResponseWriter, cookie http.Cookie, secretKey []byte) error {
// Create a new AES cipher block from the secret key. // Create a new AES cipher block from the secret key.
block, err := aes.NewCipher(secretKey) block, err := aes.NewCipher(secretKey)
@ -213,6 +226,8 @@ func CookieWriteEncrypted(w http.ResponseWriter, cookie http.Cookie, secretKey [
return CookieWrite(w, cookie) return CookieWrite(w, cookie)
} }
// 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 CookieReadEncrypted(r *http.Request, name string, secretKey []byte) (string, error) { func CookieReadEncrypted(r *http.Request, name string, secretKey []byte) (string, error) {
// Read the encrypted value from the cookie as normal. // Read the encrypted value from the cookie as normal.
encryptedValue, err := CookieRead(r, name) encryptedValue, err := CookieRead(r, name)

166
core.go
View file

@ -6,6 +6,7 @@
package core package core
import ( import (
"context"
"embed" "embed"
"fmt" "fmt"
"log" "log"
@ -39,22 +40,34 @@ var basePath string
var runMode string var runMode string
var disableEvents bool = false var disableEvents bool = false
// components_resources holds embedded file system data, typically for templates or other embedded resources.
//
//go:embed all:template //go:embed all:template
var components_resources embed.FS var components_resources embed.FS
// configContainer is a configuration structure that holds request-specific configurations for the application.
type configContainer struct { type configContainer struct {
Request RequestConfig Request RequestConfig
} }
// App is a struct representing the main application container and its core components.
type App struct { type App struct {
t int // for tracking hooks t int // for tracking hooks (deprecated - maintained for backward compatibility)
chain *chain chain *chain
hooks *Hooks hooks *Hooks
Config *configContainer Config *configContainer
} }
// requestContext holds request-specific chain execution state
type requestContext struct {
chainIndex int
chainNodes []interface{}
}
// app is a global instance of the App structure used to manage application configuration and lifecycle.
var app *App var app *App
// New initializes and returns a new instance of the App structure with default configurations and chain.
func New() *App { func New() *App {
app = &App{ app = &App{
chain: &chain{}, chain: &chain{},
@ -66,24 +79,29 @@ func New() *App {
return app return app
} }
// ResolveApp returns the global instance of the App, providing access to its methods and configuration components.
func ResolveApp() *App { func ResolveApp() *App {
return app return app
} }
// SetLogsDriver sets the logging driver to be used by the application.
func (app *App) SetLogsDriver(d logger.LogsDriver) { func (app *App) SetLogsDriver(d logger.LogsDriver) {
logsDriver = &d logsDriver = &d
} }
// Bootstrap initializes the application by setting up the logger, router, and events manager.
func (app *App) Bootstrap() { func (app *App) Bootstrap() {
loggr = logger.NewLogger(*logsDriver) loggr = logger.NewLogger(*logsDriver)
NewRouter() NewRouter()
NewEventsManager() NewEventsManager()
} }
// RegisterTemplates initializes the application template system by embedding provided templates for rendering views.
func (app *App) RegisterTemplates(templates_resources embed.FS) { func (app *App) RegisterTemplates(templates_resources embed.FS) {
NewTemplates(components_resources, templates_resources) NewTemplates(components_resources, templates_resources)
} }
// Run initializes the application's router, configures HTTPS if enabled, and starts the HTTP/HTTPS server.
func (app *App) Run(router *httprouter.Router) { func (app *App) Run(router *httprouter.Router) {
portNumber := os.Getenv("App_HTTP_PORT") portNumber := os.Getenv("App_HTTP_PORT")
if portNumber == "" { if portNumber == "" {
@ -93,7 +111,7 @@ func (app *App) Run(router *httprouter.Router) {
// check if template engine is enable // check if template engine is enable
TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE") TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
if TemplateEnableStr == "" { if "" == TemplateEnableStr {
TemplateEnableStr = "false" TemplateEnableStr = "false"
} }
TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr) TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)
@ -102,13 +120,33 @@ func (app *App) Run(router *httprouter.Router) {
router.ServeFiles("/public/*filepath", http.Dir("storage/public")) router.ServeFiles("/public/*filepath", http.Dir("storage/public"))
} }
// check if app engine is enable
AppEnableStr := os.Getenv("APP_ENABLE")
if "" == AppEnableStr {
AppEnableStr = "false"
}
AppEnable, _ := strconv.ParseBool(AppEnableStr)
if AppEnable {
router.ServeFiles("/app/*filepath", http.Dir("storage/app"))
}
// check if app engine is enable
CDNEnableStr := os.Getenv("CDN_ENABLE")
if "" == CDNEnableStr {
CDNEnableStr = "false"
}
CDNEnable, _ := strconv.ParseBool(CDNEnableStr)
if CDNEnable {
router.ServeFiles("/cdn/*filepath", http.Dir("storage/cdn"))
}
useHttpsStr := os.Getenv("App_USE_HTTPS") useHttpsStr := os.Getenv("App_USE_HTTPS")
if useHttpsStr == "" { if useHttpsStr == "" {
useHttpsStr = "false" useHttpsStr = "false"
} }
useHttps, _ := strconv.ParseBool(useHttpsStr) useHttps, _ := strconv.ParseBool(useHttpsStr)
if runMode == "dev" { if "dev" == runMode {
fmt.Printf("Welcome to Goffee\n") fmt.Printf("Welcome to Goffee\n")
if useHttps { if useHttps {
fmt.Printf("Listening on https \nWaiting for requests...\n") fmt.Printf("Listening on https \nWaiting for requests...\n")
@ -156,6 +194,9 @@ func (app *App) Run(router *httprouter.Router) {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", portNumber), router)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", portNumber), 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 *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httprouter.Router { func (app *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httprouter.Router {
router.PanicHandler = panicHandler router.PanicHandler = panicHandler
router.NotFound = notFoundHandler{} router.NotFound = notFoundHandler{}
@ -194,12 +235,28 @@ func (app *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httpr
return router return router
} }
// makeHTTPRouterHandlerFunc creates an httprouter.Handle that handles HTTP requests with the specified controller and hooks.
func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Handle { func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Create request-specific chain state
reqCtx := &requestContext{
chainIndex: 0,
}
// Prepare the chain nodes for this request
mw := app.hooks.GetHooks()
for _, v := range mw {
reqCtx.chainNodes = append(reqCtx.chainNodes, v)
}
rhs := app.combHandlers(h, ms)
for _, v := range rhs {
reqCtx.chainNodes = append(reqCtx.chainNodes, v)
}
// Store chain state in request context
ctx := &Context{ ctx := &Context{
Request: &Request{ Request: &Request{
httpRequest: r, httpRequest: r.WithContext(context.WithValue(r.Context(), "goffeeChain", reqCtx)),
httpPathParams: ps, httpPathParams: ps,
}, },
Response: &Response{ Response: &Response{
@ -222,10 +279,16 @@ func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Ha
} }
ctx.prepare(ctx) ctx.prepare(ctx)
rhs := app.combHandlers(h, ms)
app.prepareChain(rhs) // Execute the first handler in the chain
app.t = 0 if len(reqCtx.chainNodes) > 0 {
app.chain.execute(ctx) n := reqCtx.chainNodes[0]
if f, ok := n.(Hook); ok {
f(ctx)
} else if ff, ok := n.(Controller); ok {
ff(ctx)
}
}
for _, header := range ctx.Response.headers { for _, header := range ctx.Response.headers {
w.Header().Add(header.key, header.val) w.Header().Add(header.key, header.val)
@ -260,9 +323,13 @@ func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Ha
} }
} }
// notFoundHandler is a type that implements the http.Handler interface to handle HTTP 404 Not Found errors.
type notFoundHandler struct{} type notFoundHandler struct{}
// methodNotAllowed is a type that implements http.Handler to handle HTTP 405 Method Not Allowed errors.
type methodNotAllowed struct{} type methodNotAllowed struct{}
// ServeHTTP handles HTTP requests by responding with a 404 status code and a JSON-formatted "Not Found" message.
func (n notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (n notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
res := "{\"message\": \"Not Found\"}" res := "{\"message\": \"Not Found\"}"
@ -272,6 +339,7 @@ func (n notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(res)) w.Write([]byte(res))
} }
// ServeHTTP handles HTTP requests with method not allowed status and logs the occurrence and stack trace.
func (n methodNotAllowed) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (n methodNotAllowed) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
res := "{\"message\": \"Method not allowed\"}" res := "{\"message\": \"Method not allowed\"}"
@ -281,6 +349,7 @@ func (n methodNotAllowed) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(res)) w.Write([]byte(res))
} }
// panicHandler handles panics during HTTP request processing, logs errors, and formats responses based on environment settings.
var panicHandler = func(w http.ResponseWriter, r *http.Request, e interface{}) { var panicHandler = func(w http.ResponseWriter, r *http.Request, e interface{}) {
isDebugModeStr := os.Getenv("APP_DEBUG_MODE") isDebugModeStr := os.Getenv("APP_DEBUG_MODE")
isDebugMode, err := strconv.ParseBool(isDebugModeStr) isDebugMode, err := strconv.ParseBool(isDebugModeStr)
@ -315,34 +384,53 @@ var panicHandler = func(w http.ResponseWriter, r *http.Request, e interface{}) {
w.Write([]byte(res)) w.Write([]byte(res))
} }
// UseHook registers a Hook middleware function by attaching it to the global Hooks instance.
func UseHook(mw Hook) { func UseHook(mw Hook) {
ResolveHooks().Attach(mw) ResolveHooks().Attach(mw)
} }
// Next advances to the next middleware or controller in the chain and invokes it with the given context if available.
func (app *App) Next(c *Context) { func (app *App) Next(c *Context) {
app.t = app.t + 1 // Get request-specific chain state from context
n := app.chain.getByIndex(app.t) if reqCtx, ok := c.Request.httpRequest.Context().Value("goffeeChain").(*requestContext); ok {
if n != nil { reqCtx.chainIndex++
f, ok := n.(Hook) if reqCtx.chainIndex < len(reqCtx.chainNodes) {
if ok { n := reqCtx.chainNodes[reqCtx.chainIndex]
f(c) if f, ok := n.(Hook); ok {
} else { f(c)
ff, ok := n.(Controller) } else if ff, ok := n.(Controller); ok {
if ok {
ff(c) ff(c)
} }
} }
} else {
// Fallback to old behavior (not thread-safe)
app.t = app.t + 1
n := app.chain.getByIndex(app.t)
if n != nil {
f, ok := n.(Hook)
if ok {
f(c)
} else {
ff, ok := n.(Controller)
if ok {
ff(c)
}
}
}
} }
} }
// chain represents a sequence of nodes, where each node is an interface, used for executing a series of operations.
type chain struct { type chain struct {
nodes []interface{} nodes []interface{}
} }
// reset clears all nodes in the chain, resetting it to an empty state.
func (cn *chain) reset() { func (cn *chain) reset() {
cn.nodes = []interface{}{} cn.nodes = []interface{}{}
} }
// getByIndex retrieves an element from the chain's nodes slice by its index. Returns nil if the index is out of range.
func (c *chain) getByIndex(i int) interface{} { func (c *chain) getByIndex(i int) interface{} {
for k := range c.nodes { for k := range c.nodes {
if k == i { if k == i {
@ -353,6 +441,7 @@ func (c *chain) getByIndex(i int) interface{} {
return nil return nil
} }
// prepareChain appends all hooks from the provided list and the application's Hooks to the chain's nodes.
func (app *App) prepareChain(hs []interface{}) { func (app *App) prepareChain(hs []interface{}) {
mw := app.hooks.GetHooks() mw := app.hooks.GetHooks()
for _, v := range mw { for _, v := range mw {
@ -363,6 +452,18 @@ func (app *App) prepareChain(hs []interface{}) {
} }
} }
// prepareRequestChain prepares a request-specific chain to avoid race conditions
func (app *App) prepareRequestChain(requestChain *chain, hs []interface{}) {
mw := app.hooks.GetHooks()
for _, v := range mw {
requestChain.nodes = append(requestChain.nodes, v)
}
for _, v := range hs {
requestChain.nodes = append(requestChain.nodes, v)
}
}
// execute executes the first node in the chain, invoking it as either a Hook or Controller if applicable.
func (cn *chain) execute(ctx *Context) { func (cn *chain) execute(ctx *Context) {
i := cn.getByIndex(0) i := cn.getByIndex(0)
if i != nil { if i != nil {
@ -378,6 +479,7 @@ func (cn *chain) execute(ctx *Context) {
} }
} }
// combHandlers combines a controller and a slice of hooks into a single slice of interfaces in reversed order.
func (app *App) combHandlers(h Controller, mw []Hook) []interface{} { func (app *App) combHandlers(h Controller, mw []Hook) []interface{} {
var rev []interface{} var rev []interface{}
for _, k := range mw { for _, k := range mw {
@ -387,6 +489,8 @@ func (app *App) combHandlers(h Controller, mw []Hook) []interface{} {
return rev return rev
} }
// getGormFunc returns a function that provides a *gorm.DB instance, ensuring gorm is enabled in the configuration.
// If gorm is not enabled, it panics with an appropriate message.
func getGormFunc() func() *gorm.DB { func getGormFunc() func() *gorm.DB {
f := func() *gorm.DB { f := func() *gorm.DB {
if !gormC.EnableGorm { if !gormC.EnableGorm {
@ -397,6 +501,9 @@ func getGormFunc() func() *gorm.DB {
return f return f
} }
// 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 NewGorm() *gorm.DB { func NewGorm() *gorm.DB {
var err error var err error
switch os.Getenv("DB_DRIVER") { switch os.Getenv("DB_DRIVER") {
@ -421,6 +528,7 @@ func NewGorm() *gorm.DB {
return db return db
} }
// ResolveGorm initializes and returns a singleton instance of *gorm.DB, creating it if it doesn't already exist.
func ResolveGorm() *gorm.DB { func ResolveGorm() *gorm.DB {
if db != nil { if db != nil {
return db return db
@ -429,6 +537,8 @@ func ResolveGorm() *gorm.DB {
return db return db
} }
// resolveCache returns a function that provides a *Cache instance when invoked.
// Panics if caching is not enabled in the configuration.
func resolveCache() func() *Cache { func resolveCache() func() *Cache {
f := func() *Cache { f := func() *Cache {
if !cacheC.EnableCache { if !cacheC.EnableCache {
@ -439,6 +549,8 @@ func resolveCache() func() *Cache {
return f return f
} }
// postgresConnect establishes a connection to a PostgreSQL database using environment variables for configuration.
// It returns a pointer to a gorm.DB instance or an error if the connection fails.
func postgresConnect() (*gorm.DB, error) { func postgresConnect() (*gorm.DB, error) {
dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v sslmode=%v TimeZone=%v", dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v sslmode=%v TimeZone=%v",
os.Getenv("POSTGRES_HOST"), os.Getenv("POSTGRES_HOST"),
@ -452,6 +564,8 @@ func postgresConnect() (*gorm.DB, error) {
return gorm.Open(postgres.Open(dsn), &gorm.Config{}) return gorm.Open(postgres.Open(dsn), &gorm.Config{})
} }
// mysqlConnect establishes a connection to a MySQL database using credentials and configurations from environment variables.
// Returns a *gorm.DB instance on success or an error if the connection fails.
func mysqlConnect() (*gorm.DB, error) { func mysqlConnect() (*gorm.DB, error) {
dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=%v&parseTime=True&loc=Local", dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=%v&parseTime=True&loc=Local",
os.Getenv("MYSQL_USERNAME"), os.Getenv("MYSQL_USERNAME"),
@ -471,6 +585,9 @@ func mysqlConnect() (*gorm.DB, error) {
}), &gorm.Config{}) }), &gorm.Config{})
} }
// getJWT returns a function that initializes and provides a *JWT instance configured with environment variables.
// The JWT instance is created using a secret signing key and a lifespan derived from the environment.
// Panics if the signing key is not set or if the lifespan cannot be parsed.
func getJWT() func() *JWT { func getJWT() func() *JWT {
f := func() *JWT { f := func() *JWT {
secret := os.Getenv("JWT_SECRET") secret := os.Getenv("JWT_SECRET")
@ -494,6 +611,7 @@ func getJWT() func() *JWT {
return f return f
} }
// getValidator returns a closure that instantiates and provides a new instance of Validator.
func getValidator() func() *Validator { func getValidator() func() *Validator {
f := func() *Validator { f := func() *Validator {
return &Validator{} return &Validator{}
@ -501,6 +619,7 @@ func getValidator() func() *Validator {
return f return f
} }
// resloveHashing returns a function that initializes and provides a pointer to a new instance of the Hashing struct.
func resloveHashing() func() *Hashing { func resloveHashing() func() *Hashing {
f := func() *Hashing { f := func() *Hashing {
return &Hashing{} return &Hashing{}
@ -508,6 +627,7 @@ func resloveHashing() func() *Hashing {
return f return f
} }
// resolveMailer initializes and returns a function that provides a singleton Mailer instance based on the configured driver.
func resolveMailer() func() *Mailer { func resolveMailer() func() *Mailer {
f := func() *Mailer { f := func() *Mailer {
if mailer != nil { if mailer != nil {
@ -536,6 +656,7 @@ func resolveMailer() func() *Mailer {
return f return f
} }
// resolveEventsManager creates and returns a function that resolves the singleton instance of EventsManager.
func resolveEventsManager() func() *EventsManager { func resolveEventsManager() func() *EventsManager {
f := func() *EventsManager { f := func() *EventsManager {
return ResolveEventsManager() return ResolveEventsManager()
@ -543,6 +664,7 @@ func resolveEventsManager() func() *EventsManager {
return f return f
} }
// resolveLogger returns a function that provides access to the initialized logger instance when invoked.
func resolveLogger() func() *logger.Logger { func resolveLogger() func() *logger.Logger {
f := func() *logger.Logger { f := func() *logger.Logger {
return loggr return loggr
@ -550,6 +672,7 @@ func resolveLogger() func() *logger.Logger {
return f return f
} }
// MakeDirs creates the specified directories under the base path with the provided permissions (0766).
func (app *App) MakeDirs(dirs ...string) { func (app *App) MakeDirs(dirs ...string) {
o := syscall.Umask(0) o := syscall.Umask(0)
defer syscall.Umask(o) defer syscall.Umask(o)
@ -558,30 +681,37 @@ func (app *App) MakeDirs(dirs ...string) {
} }
} }
// SetRequestConfig sets the configuration for the request in the application instance.
func (app *App) SetRequestConfig(r RequestConfig) { func (app *App) SetRequestConfig(r RequestConfig) {
requestC = r requestC = r
} }
// SetGormConfig sets the GormConfig for the application, overriding the current configuration with the provided value.
func (app *App) SetGormConfig(g GormConfig) { func (app *App) SetGormConfig(g GormConfig) {
gormC = g gormC = g
} }
// SetCacheConfig sets the cache configuration for the application using the provided CacheConfig parameter.
func (app *App) SetCacheConfig(c CacheConfig) { func (app *App) SetCacheConfig(c CacheConfig) {
cacheC = c cacheC = c
} }
// SetBasePath sets the base path for the application, which is used as a prefix for file and directory operations.
func (app *App) SetBasePath(path string) { func (app *App) SetBasePath(path string) {
basePath = path basePath = path
} }
// SetRunMode sets the application run mode to the specified value.
func (app *App) SetRunMode(runmode string) { func (app *App) SetRunMode(runmode string) {
runMode = runmode runMode = runmode
} }
// DisableEvents sets the global variable `disableEvents` to true, disabling the handling or triggering of events.
func DisableEvents() { func DisableEvents() {
disableEvents = true disableEvents = true
} }
// EnableEvents sets the global variable `disableEvents` to false, effectively enabling event handling.
func EnableEvents() { func EnableEvents() {
disableEvents = false disableEvents = false
} }

14
go.mod
View file

@ -4,17 +4,17 @@ replace git.smarteching.com/goffee/core/logger => ./logger
replace git.smarteching.com/goffee/core/env => ./env replace git.smarteching.com/goffee/core/env => ./env
go 1.23.1 go 1.24.1
require ( require (
git.smarteching.com/zeni/go-chart/v2 v2.1.4 git.smarteching.com/zeni/go-chart/v2 v2.1.4
github.com/brianvoe/gofakeit/v6 v6.21.0 github.com/brianvoe/gofakeit/v6 v6.21.0
github.com/golang-jwt/jwt/v5 v5.0.0 github.com/golang-jwt/jwt/v5 v5.0.0
github.com/google/uuid v1.3.0 github.com/google/uuid v1.6.0
github.com/harranali/mailing v1.2.0 github.com/harranali/mailing v1.2.0
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/julienschmidt/httprouter v1.3.0 github.com/julienschmidt/httprouter v1.3.0
github.com/redis/go-redis/v9 v9.0.5 github.com/redis/go-redis/v9 v9.7.0
golang.org/x/crypto v0.11.0 golang.org/x/crypto v0.11.0
gorm.io/driver/mysql v1.5.1 gorm.io/driver/mysql v1.5.1
gorm.io/driver/postgres v1.5.2 gorm.io/driver/postgres v1.5.2
@ -23,11 +23,14 @@ require (
) )
require ( require (
git.smarteching.com/zeni/go-charts/v2 v2.6.11 // indirect
github.com/SparkPost/gosparkpost v0.2.0 // indirect github.com/SparkPost/gosparkpost v0.2.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/hibiken/asynq v0.25.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.3.1 // indirect github.com/jackc/pgx/v5 v5.3.1 // indirect
@ -37,11 +40,16 @@ require (
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sendgrid/rest v2.6.9+incompatible // indirect github.com/sendgrid/rest v2.6.9+incompatible // indirect
github.com/sendgrid/sendgrid-go v3.12.0+incompatible // indirect github.com/sendgrid/sendgrid-go v3.12.0+incompatible // indirect
github.com/spf13/cast v1.7.0 // indirect
golang.org/x/image v0.21.0 // indirect golang.org/x/image v0.21.0 // indirect
golang.org/x/net v0.10.0 // indirect golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.19.0 // indirect golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.8.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
) )
require ( require (

21
go.sum
View file

@ -1,5 +1,7 @@
git.smarteching.com/zeni/go-chart/v2 v2.1.4 h1:pF06+F6eqJLIG8uMiTVPR5TygPGMjM/FHMzTxmu5V/Q= git.smarteching.com/zeni/go-chart/v2 v2.1.4 h1:pF06+F6eqJLIG8uMiTVPR5TygPGMjM/FHMzTxmu5V/Q=
git.smarteching.com/zeni/go-chart/v2 v2.1.4/go.mod h1:b3ueW9h3pGGXyhkormZAvilHaG4+mQti+bMNPdQBeOQ= git.smarteching.com/zeni/go-chart/v2 v2.1.4/go.mod h1:b3ueW9h3pGGXyhkormZAvilHaG4+mQti+bMNPdQBeOQ=
git.smarteching.com/zeni/go-charts/v2 v2.6.11 h1:9udzlv3uxGXszpplfkL5IaTUrgkNj++KwhbaN1vVEqI=
git.smarteching.com/zeni/go-charts/v2 v2.6.11/go.mod h1:3OpRPSXg7Qx4zcgsmwsC9ZFB9/wAkGSbnXf1wIbHYCg=
github.com/SparkPost/gosparkpost v0.2.0 h1:yzhHQT7cE+rqzd5tANNC74j+2x3lrPznqPJrxC1yR8s= github.com/SparkPost/gosparkpost v0.2.0 h1:yzhHQT7cE+rqzd5tANNC74j+2x3lrPznqPJrxC1yR8s=
github.com/SparkPost/gosparkpost v0.2.0/go.mod h1:S9WKcGeou7cbPpx0kTIgo8Q69WZvUmVeVzbD+djalJ4= github.com/SparkPost/gosparkpost v0.2.0/go.mod h1:S9WKcGeou7cbPpx0kTIgo8Q69WZvUmVeVzbD+djalJ4=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
@ -19,6 +21,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51 h1:0JZ+dUmQeA8IIVUMzysrX4/AKuQwWhV2dYQuPZdvdSQ= github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51 h1:0JZ+dUmQeA8IIVUMzysrX4/AKuQwWhV2dYQuPZdvdSQ=
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64= github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A= github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A=
@ -40,8 +44,12 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGw
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/harranali/mailing v1.2.0 h1:ihIyJwB8hyRVcdk+v465wk1PHMrSrgJqo/kMd+gZClY= github.com/harranali/mailing v1.2.0 h1:ihIyJwB8hyRVcdk+v465wk1PHMrSrgJqo/kMd+gZClY=
github.com/harranali/mailing v1.2.0/go.mod h1:4a5N3yG98pZKluMpmcYlTtll7bisvOfGQEMIng3VQk4= github.com/harranali/mailing v1.2.0/go.mod h1:4a5N3yG98pZKluMpmcYlTtll7bisvOfGQEMIng3VQk4=
github.com/hibiken/asynq v0.25.1 h1:phj028N0nm15n8O2ims+IvJ2gz4k2auvermngh9JhTw=
github.com/hibiken/asynq v0.25.1/go.mod h1:pazWNOLBu0FEynQRBvHA26qdIKRSmfdIfUm4HdsLmXg=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
@ -79,17 +87,24 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o= github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o=
github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk= github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU= github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU=
github.com/sendgrid/rest v2.6.9+incompatible h1:1EyIcsNdn9KIisLW50MKwmSRSK+ekueiEMJ7NEoxJo0= github.com/sendgrid/rest v2.6.9+incompatible h1:1EyIcsNdn9KIisLW50MKwmSRSK+ekueiEMJ7NEoxJo0=
github.com/sendgrid/rest v2.6.9+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE= github.com/sendgrid/rest v2.6.9+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE=
github.com/sendgrid/sendgrid-go v3.12.0+incompatible h1:/N2vx18Fg1KmQOh6zESc5FJB8pYwt5QFBDflYPh1KVg= github.com/sendgrid/sendgrid-go v3.12.0+incompatible h1:/N2vx18Fg1KmQOh6zESc5FJB8pYwt5QFBDflYPh1KVg=
github.com/sendgrid/sendgrid-go v3.12.0+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8= github.com/sendgrid/sendgrid-go v3.12.0+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM= github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA= golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio= golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
@ -99,11 +114,17 @@ golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

52
queue.go Normal file
View file

@ -0,0 +1,52 @@
// Copyright (c) 2025 Zeni Kim <zenik@smarteching.com>
// Use of this source code is governed by MIT-style
// license that can be found in the LICENSE file.
package core
import (
"context"
"fmt"
"log"
"os"
"github.com/hibiken/asynq"
)
type Asynqtask func(context.Context, *asynq.Task) error
type Queuemux struct {
themux *asynq.ServeMux
}
func (q *Queuemux) QueueInit() {
q.themux = asynq.NewServeMux()
}
func (q *Queuemux) AddWork(pattern string, work Asynqtask) {
q.themux.HandleFunc(pattern, work)
}
// RunQueueserver starts the queue server with predefined configurations 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.
func (q *Queuemux) RunQueueserver() {
redisAddr := fmt.Sprintf("%v:%v", os.Getenv("REDIS_HOST"), os.Getenv("REDIS_PORT"))
srv := asynq.NewServer(
asynq.RedisClientOpt{Addr: redisAddr},
asynq.Config{Concurrency: 10,
// Optionally specify multiple queues with different priority.
Queues: map[string]int{
"critical": 6,
"default": 3,
"low": 1,
},
},
)
if err := srv.Run(q.themux); err != nil {
log.Fatal(err)
}
}

View file

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
) )
// Response represents an HTTP response to be sent to the client, including headers, body, status code, and metadata.
type Response struct { type Response struct {
headers []header headers []header
body []byte body []byte
@ -22,23 +23,34 @@ type Response struct {
HttpResponseWriter http.ResponseWriter HttpResponseWriter http.ResponseWriter
} }
// header represents a single HTTP header with a key-value pair.
type header struct { type header struct {
key string key string
val string val string
} }
// TODO add doc // writes the contents of a buffer to the HTTP response with specified file name and type if not terminated.
func (rs *Response) BufferPDF(name string, b bytes.Buffer) *Response { func (rs *Response) BufferFile(name string, filetype string, b bytes.Buffer) *Response {
if rs.isTerminated == false { if rs.isTerminated == false {
rs.HttpResponseWriter.Header().Add("Content-Type", "application/pdf") rs.HttpResponseWriter.Header().Add("Content-Type", filetype)
rs.HttpResponseWriter.Header().Add("Content-Disposition", "attachment; filename="+name) rs.HttpResponseWriter.Header().Add("Content-Disposition", "attachment; filename="+name)
b.WriteTo(rs.HttpResponseWriter) b.WriteTo(rs.HttpResponseWriter)
} }
return rs return rs
} }
// TODO add doc // writes the contents of a buffer to the HTTP response with specified file name and type if not terminated.
func (rs *Response) BufferInline(name string, filetype string, b bytes.Buffer) *Response {
if rs.isTerminated == false {
rs.HttpResponseWriter.Header().Add("Content-Type", filetype)
b.WriteTo(rs.HttpResponseWriter)
}
return rs
}
// sets the response's content type to HTML and assigns the provided body as the response body if not terminated.
func (rs *Response) Any(body any) *Response { func (rs *Response) Any(body any) *Response {
if rs.isTerminated == false { if rs.isTerminated == false {
rs.contentType = CONTENT_TYPE_HTML rs.contentType = CONTENT_TYPE_HTML
@ -48,7 +60,7 @@ func (rs *Response) Any(body any) *Response {
return rs return rs
} }
// TODO add doc // sets the response's content type to JSON and assigns the provided string as the body if the response is not terminated.
func (rs *Response) Json(body string) *Response { func (rs *Response) Json(body string) *Response {
if rs.isTerminated == false { if rs.isTerminated == false {
rs.contentType = CONTENT_TYPE_JSON rs.contentType = CONTENT_TYPE_JSON
@ -57,7 +69,7 @@ func (rs *Response) Json(body string) *Response {
return rs return rs
} }
// TODO add doc // sets the response's content type to plain text and assigns the provided string as the body if not terminated.
func (rs *Response) Text(body string) *Response { func (rs *Response) Text(body string) *Response {
if rs.isTerminated == false { if rs.isTerminated == false {
rs.contentType = CONTENT_TYPE_TEXT rs.contentType = CONTENT_TYPE_TEXT
@ -66,7 +78,7 @@ func (rs *Response) Text(body string) *Response {
return rs return rs
} }
// TODO add doc // sets the response's content type to HTML and assigns the provided string as the body if the response is not terminated.
func (rs *Response) HTML(body string) *Response { func (rs *Response) HTML(body string) *Response {
if rs.isTerminated == false { if rs.isTerminated == false {
rs.contentType = CONTENT_TYPE_HTML rs.contentType = CONTENT_TYPE_HTML
@ -75,7 +87,7 @@ func (rs *Response) HTML(body string) *Response {
return rs return rs
} }
// TODO add doc // renders the specified template with the provided data and writes the result to the HTTP response if not terminated.
func (rs *Response) Template(name string, data interface{}) *Response { func (rs *Response) Template(name string, data interface{}) *Response {
var buffer bytes.Buffer var buffer bytes.Buffer
if rs.isTerminated == false { if rs.isTerminated == false {
@ -89,7 +101,7 @@ func (rs *Response) Template(name string, data interface{}) *Response {
return rs return rs
} }
// TODO add doc // sets the response status code if the response is not yet terminated. Returns the updated Response object.
func (rs *Response) SetStatusCode(code int) *Response { func (rs *Response) SetStatusCode(code int) *Response {
if rs.isTerminated == false { if rs.isTerminated == false {
rs.statusCode = code rs.statusCode = code
@ -98,7 +110,7 @@ func (rs *Response) SetStatusCode(code int) *Response {
return rs return rs
} }
// TODO add doc // sets a custom content type for the response if it is not terminated. Returns the updated Response object.
func (rs *Response) SetContentType(c string) *Response { func (rs *Response) SetContentType(c string) *Response {
if rs.isTerminated == false { if rs.isTerminated == false {
rs.overrideContentType = c rs.overrideContentType = c
@ -107,7 +119,7 @@ func (rs *Response) SetContentType(c string) *Response {
return rs return rs
} }
// TODO add doc // adds or updates a header to the response if it is not terminated. Returns the updated Response object.
func (rs *Response) SetHeader(key string, val string) *Response { func (rs *Response) SetHeader(key string, val string) *Response {
if rs.isTerminated == false { if rs.isTerminated == false {
h := header{ h := header{
@ -119,10 +131,12 @@ func (rs *Response) SetHeader(key string, val string) *Response {
return rs return rs
} }
// terminates the response processing, preventing any further modifications or actions.
func (rs *Response) ForceSendResponse() { func (rs *Response) ForceSendResponse() {
rs.isTerminated = true rs.isTerminated = true
} }
// updates the redirect URL for the response and returns the modified Response. Validates the URL before setting it.
func (rs *Response) Redirect(url string) *Response { func (rs *Response) Redirect(url string) *Response {
validator := resolveValidator() validator := resolveValidator()
v := validator.Validate(map[string]interface{}{ v := validator.Validate(map[string]interface{}{
@ -142,6 +156,7 @@ func (rs *Response) Redirect(url string) *Response {
return rs return rs
} }
// converts various primitive data types to their string representation or triggers a panic for unsupported types.
func (rs *Response) castBasicVarsToString(data interface{}) string { func (rs *Response) castBasicVarsToString(data interface{}) string {
switch dataType := data.(type) { switch dataType := data.(type) {
case string: case string:
@ -199,6 +214,7 @@ func (rs *Response) castBasicVarsToString(data interface{}) string {
} }
} }
// reinitializes the Response object to its default state, clearing its body, headers, and resetting other properties.
func (rs *Response) reset() { func (rs *Response) reset() {
rs.body = nil rs.body = nil
rs.statusCode = http.StatusOK rs.statusCode = http.StatusOK

View file

@ -5,6 +5,7 @@
package core package core
// Route defines an HTTP route with a method, path, controller function, and optional hooks for customization.
type Route struct { type Route struct {
Method string Method string
Path string Path string
@ -12,12 +13,15 @@ type Route struct {
Hooks []Hook Hooks []Hook
} }
// Router represents a structure for storing and managing routes in the application.
type Router struct { type Router struct {
Routes []Route Routes []Route
} }
// router is a global instance of the Router struct used to manage and resolve application routes.
var router *Router var router *Router
// NewRouter initializes and returns a new Router instance with an empty slice of routes.
func NewRouter() *Router { func NewRouter() *Router {
router = &Router{ router = &Router{
[]Route{}, []Route{},
@ -25,10 +29,12 @@ func NewRouter() *Router {
return router return router
} }
// ResolveRouter returns the singleton instance of the Router. It initializes and manages HTTP routes configuration.
func ResolveRouter() *Router { func ResolveRouter() *Router {
return router return router
} }
// Get registers a GET route with the specified path, controller, and optional hooks, returning the updated Router.
func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router { func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router {
r.Routes = append(r.Routes, Route{ r.Routes = append(r.Routes, Route{
Method: GET, Method: GET,
@ -39,6 +45,7 @@ func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router
return r return r
} }
// Post registers a route with the HTTP POST method, associating it with the given path, controller, and optional hooks.
func (r *Router) Post(path string, controller Controller, hooks ...Hook) *Router { func (r *Router) Post(path string, controller Controller, hooks ...Hook) *Router {
r.Routes = append(r.Routes, Route{ r.Routes = append(r.Routes, Route{
Method: POST, Method: POST,
@ -49,6 +56,7 @@ func (r *Router) Post(path string, controller Controller, hooks ...Hook) *Router
return r return r
} }
// Delete registers a DELETE HTTP method route with the specified path, controller, and optional hooks.
func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Router { func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Router {
r.Routes = append(r.Routes, Route{ r.Routes = append(r.Routes, Route{
Method: DELETE, Method: DELETE,
@ -59,6 +67,7 @@ func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Rout
return r return r
} }
// Patch registers a new route with the HTTP PATCH method, a specified path, a controller, and optional hooks.
func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Router { func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Router {
r.Routes = append(r.Routes, Route{ r.Routes = append(r.Routes, Route{
Method: PATCH, Method: PATCH,
@ -69,6 +78,7 @@ func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Route
return r return r
} }
// Put registers a new route with the HTTP PUT method, associating it to the given path, controller, and optional hooks.
func (r *Router) Put(path string, controller Controller, hooks ...Hook) *Router { func (r *Router) Put(path string, controller Controller, hooks ...Hook) *Router {
r.Routes = append(r.Routes, Route{ r.Routes = append(r.Routes, Route{
Method: PUT, Method: PUT,
@ -79,6 +89,7 @@ func (r *Router) Put(path string, controller Controller, hooks ...Hook) *Router
return r return r
} }
// Options registers a new route with the OPTIONS HTTP method, a specified path, controller, and optional hooks.
func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Router { func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Router {
r.Routes = append(r.Routes, Route{ r.Routes = append(r.Routes, Route{
Method: OPTIONS, Method: OPTIONS,
@ -89,6 +100,7 @@ func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Rou
return r return r
} }
// Head registers a route with the HTTP HEAD method, associates it with a path, controller, and optional hooks.
func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router { func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router {
r.Routes = append(r.Routes, Route{ r.Routes = append(r.Routes, Route{
Method: HEAD, Method: HEAD,
@ -99,6 +111,7 @@ func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router
return r return r
} }
// GetRoutes returns a slice of Route objects currently registered within the Router.
func (r *Router) GetRoutes() []Route { func (r *Router) GetRoutes() []Route {
return r.Routes return r.Routes
} }

View file

@ -1,10 +1,12 @@
package components package components
type ContentList struct { type ContentList struct {
ID string
Items []ContentListItem Items []ContentListItem
} }
type ContentListItem struct { type ContentListItem struct {
ID string
Text string Text string
Description string Description string
EndElement string EndElement string

View file

@ -1,8 +1,10 @@
{{define "content_list"}} {{define "content_list"}}
<ul class="list-group"> <ul class="list-group" {{ if .ID }}id="{{.ID}}"{{end}}>
{{ range .Items}} {{ range .Items}}
<li class="list-group-item text-wrap {{if eq .IsDisabled true}}disabled{{end}} {{if .TypeClass}}list-group-item-{{.TypeClass}}{{end}}" <li class="list-group-item text-wrap {{if eq .IsDisabled true}}disabled{{end}}
{{if eq .IsDisabled true}}aria-disabled="true"{{end}} {{if .TypeClass}}list-group-item-{{.TypeClass}}{{end}}"
{{if eq .IsDisabled true}}aria-disabled="true"{{end}}
{{ if .ID }}id="{{.ID}}"{{end}}
><div class="d-flex justify-content-between lh-sm"><p class="mb-1">{{.Text}}</p><span class="text-body-secondary ms-5">{{.EndElement}}</span></div> ><div class="d-flex justify-content-between lh-sm"><p class="mb-1">{{.Text}}</p><span class="text-body-secondary ms-5">{{.EndElement}}</span></div>
<small>{{.Description}}</small> <small>{{.Description}}</small>
</li> </li>

View file

@ -0,0 +1,9 @@
package components
type ContentPagination struct {
PageStartRecord int
PageEndRecord int
TotalRecords int
PrevLink string
NextLink string
}

View file

@ -0,0 +1,10 @@
{{define "content_pagination"}}
<div class="pagination-container">
<ul class="pagination pagination-sm">
<li class="page-item">{{.PageStartRecord}} - {{.PageEndRecord}} of {{.TotalRecords}}</li>
<li class="page-item">&nbsp;</li>
<li class="page-item"><a class="page-link{{if eq .PrevLink ""}} disabled"{{else}}" href="{{.PrevLink}}"{{end}}>«</a></li>
<li class="page-item"><a class="page-link{{if eq .NextLink ""}} disabled"{{else}}" href="{{.NextLink}}"{{end}}>»</a></li>
</ul>
</div>
{{end}}

View file

@ -10,11 +10,12 @@ type ContentTable struct {
type ContentTableTH struct { type ContentTableTH struct {
ID string ID string
ValueType string // -> default string, href, badge ValueType string // -> default string, href, badge, list, checkbox
Value string Value string
} }
type ContentTableTD struct { type ContentTableTD struct {
ID string ID string
Value interface{} // string or component struct according ValueType Value interface{} // string or component struct according ValueType
ValueClass string
} }

View file

@ -7,12 +7,18 @@
</thead> </thead>
<tbody> <tbody>
{{- range .AllTD}}<tr scope="row"> {{- range .AllTD}}<tr scope="row">
{{range $index, $item := .}}<td {{ if $item.ID }}id="{{$item.ID}}"{{end}}> {{range $index, $item := .}}<td {{ if $item.ID }}id="{{$item.ID}}"{{end}}{{ if $item.ValueClass }} class="{{$item.ValueClass}}"{{end}}>
{{ with $x := index $.AllTH $index }} {{ with $x := index $.AllTH $index }}
{{ if eq $x.ValueType "href"}} {{ if eq $x.ValueType "href"}}
{{template "content_href" $item.Value}} {{template "content_href" $item.Value}}
{{ else if eq $x.ValueType "badge"}} {{ else if eq $x.ValueType "badge"}}
{{template "content_badge" $item.Value}} {{template "content_badge" $item.Value}}
{{ else if eq $x.ValueType "list"}}
{{template "content_list" $item.Value}}
{{ else if eq $x.ValueType "checkbox"}}
{{template "form_checkbox" $item.Value}}
{{ else if eq $x.ValueType "image"}}
<img src="{{ $item.Value }}">
{{ else }} {{ else }}
{{ $item.Value }} {{ $item.Value }}
{{end}} {{end}}

View file

@ -1,6 +1,6 @@
{{define "form_checkbox"}} {{define "form_checkbox"}}
<div class="input-container"> <div class="input-container">
<label class="form-label">{{.Label}}</label> {{ if .Label }}<label class="form-label">{{.Label}}</label>{{end}}
{{range $options := .AllCheckbox}} {{range $options := .AllCheckbox}}
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" name="{{$options.Name}}" id="{{$options.ID}}" value="{{$options.Value}}"{{if eq $options.IsChecked true}} checked{{end}}> <input class="form-check-input" type="checkbox" name="{{$options.Name}}" id="{{$options.ID}}" value="{{$options.Value}}"{{if eq $options.IsChecked true}} checked{{end}}>

View file

@ -11,4 +11,10 @@ type FormInput struct {
IsDisabled bool IsDisabled bool
Autocomplete bool Autocomplete bool
IsRequired bool IsRequired bool
CustomAtt []CustomAtt
}
type CustomAtt struct {
AttName string
AttValue string
} }

View file

@ -14,6 +14,11 @@
{{if ne .Value ""}} {{if ne .Value ""}}
value="{{.Value}}" value="{{.Value}}"
{{end}} {{end}}
{{if .CustomAtt }}
{{range $options := .CustomAtt}}
{{$options.AttName}}="{{$options.AttValue}}"
{{end}}
{{end}}
aria-describedby="{{.ID}}Help"> aria-describedby="{{.ID}}Help">
{{if ne .Hint ""}}<small id="{{.ID}}Help" class="form-text text-muted">{{.Hint}}</small>{{end}} {{if ne .Hint ""}}<small id="{{.ID}}Help" class="form-text text-muted">{{.Hint}}</small>{{end}}
{{if ne .Error ""}}<div class="error">{{.Error}}</div>{{end}} {{if ne .Error ""}}<div class="error">{{.Error}}</div>{{end}}

View file

@ -5,6 +5,7 @@ type FormSelect struct {
SelectedOption FormSelectOption SelectedOption FormSelectOption
Label string Label string
AllOptions []FormSelectOption AllOptions []FormSelectOption
IsMultiple bool
} }
type FormSelectOption struct { type FormSelectOption struct {

View file

@ -1,7 +1,7 @@
{{define "form_select"}} {{define "form_select"}}
<div class="input-container"> <div class="input-container">
<label for="{{.ID}}" class="form-label">{{.Label}}</label> <label for="{{.ID}}" class="form-label">{{.Label}}</label>
<select class="form-select" id="{{.ID}}" name="{{.ID}}"> <select class="form-select" id="{{.ID}}" name="{{.ID}}"{{if eq .IsMultiple true}} multiple{{end}}>
{{range $options := .AllOptions}} {{range $options := .AllOptions}}
<option value="{{$options.Value}}" {{if eq $options.Value $.SelectedOption.Value }}selected="selected"{{end}}>{{$options.Caption}}</option> <option value="{{$options.Value}}" {{if eq $options.Value $.SelectedOption.Value }}selected="selected"{{end}}>{{$options.Caption}}</option>
{{end}} {{end}}

View file

@ -10,6 +10,7 @@ type PageNav struct {
type PageNavItem struct { type PageNavItem struct {
Text string Text string
Link string Link string
ID string
IsDisabled bool IsDisabled bool
IsActive bool IsActive bool
ChildItems []PageNavItem ChildItems []PageNavItem

View file

@ -1,11 +1,16 @@
{{define "page_nav"}} {{define "page_nav"}}
<ul class="nav justify-content-center {{.NavClass}} {{if eq .IsVertical true}}flex-column{{end}}{{if eq .IsTab true}}nav-tabs{{end}}" {{if eq .IsTab true}}role="tablist"{{end}}> <ul class="nav justify-content-center {{.NavClass}} {{if eq .IsVertical true}}flex-column{{end}}{{if eq .IsTab true}}nav-tabs{{end}}" {{if eq .IsTab true}}role="tablist"{{end}}>
{{range $item := .NavItems}} {{range $item := .NavItems}}
{{if gt (len $item.ChildItems) 0}} {{if gt (len $item.ChildItems) 0}}
{{ $hasActiveChild := false }}
{{range $child := $item.ChildItems}}
{{if $child.IsActive}}
{{ $hasActiveChild = true }}
{{end}}
{{end}}
<li class="nav-item dropdown"> <li class="nav-item dropdown">
<a href="{{$item.Link}}" {{if eq .IsDisabled true}}disabled{{end}} data-bs-toggle="dropdown" <a href="{{$item.Link}}" {{if eq .IsDisabled true}}disabled{{end}} data-bs-toggle="dropdown"
class="nav-link dropdown-toggle {{if eq .IsActive true}}active{{end}} {{if eq .IsDisabled true}}disabled{{end}}">{{$item.Text}}</a> class="nav-link dropdown-toggle {{if or $item.IsActive $hasActiveChild}}active{{end}} {{if eq .IsDisabled true}}disabled{{end}}">{{$item.Text}}</a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
{{ range $item.ChildItems}} {{ range $item.ChildItems}}
{{template "content_dropdown_item" .}} {{template "content_dropdown_item" .}}