Documentation
¶
Index ¶
- type AuthConfig
- type CollectionConfigMap
- type CollectionItemConfig
- type Config
- type CookieConfig
- type DiscoveryConfig
- type Engine
- func (e *Engine) AdminHandler() *admin.Handler
- func (e *Engine) AuthMiddleware() gin.HandlerFunc
- func (e *Engine) AuthProvider() auth.Provider
- func (e *Engine) Close() error
- func (e *Engine) DB() *sqlx.DB
- func (e *Engine) GetCollections() []*schema.Collection
- func (e *Engine) HasCollection(name string) bool
- func (e *Engine) Init(ctx context.Context) error
- func (e *Engine) Mount(rg *gin.RouterGroup)
- func (e *Engine) MountAdmin(rg *gin.RouterGroup)
- func (e *Engine) MountWithAuth(rg *gin.RouterGroup)
- func (e *Engine) MountWithOptions(rg *gin.RouterGroup, opts MountOptions)
- func (e *Engine) RefreshSchema(ctx context.Context) error
- func (e *Engine) Router() *gin.Engine
- func (e *Engine) Run(addr string) error
- func (e *Engine) SchemaManager() *schema.Manager
- func (e *Engine) SeedFromEnv(ctx context.Context) error
- func (e *Engine) SeedUsers(ctx context.Context) error
- func (e *Engine) StartSchemaWatcher(ctx context.Context) error
- func (e *Engine) StopSchemaWatcher()
- func (e *Engine) StorageManager() *storage.Manager
- func (e *Engine) TOTPManager() *auth.TOTPManager
- func (e *Engine) TriggerSchemaRefresh(ctx context.Context) error
- func (e *Engine) UserStore() auth.UserStore
- func (e *Engine) ValidatorRegistry() *validation.ValidatorRegistry
- type JWTConfig
- type MountOptions
- type PGListener
- type SchemaWatchConfig
- type SchemaWatcher
- type SeedConfig
- type SeedUser
- type ServerConfig
- type StorageConfig
- type StorageProvider
- type TOTPConfig
- type UploadOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthConfig ¶
type AuthConfig struct {
// Methods lists enabled authentication methods: "jwt", "cookie", "totp".
Methods []string
// JWT configures JWT authentication.
JWT JWTConfig
// Cookie configures cookie-based sessions.
Cookie CookieConfig
// TOTP configures time-based one-time passwords.
TOTP TOTPConfig
// CustomUserStore allows injecting a custom UserStore implementation.
// If provided, TuGo will use this instead of the default DBUserStore.
// This enables apps to use custom user tables and add business logic.
//
// The custom store must implement the auth.UserStore interface.
// See auth/types.go for the interface definition.
//
// Example with embed pattern:
//
// type Employee struct {
// auth.User // Embed the base User
// DepartmentID string `db:"department_id" json:"department_id"`
// HireDate time.Time `db:"hire_date" json:"hire_date"`
// }
//
// type EmployeeStore struct {
// db *sqlx.DB
// emailClient *email.Client
// }
//
// func (s *EmployeeStore) Create(ctx context.Context, user *auth.User, hash string) error {
// // Custom business logic: can access embedded User fields
// _, err := s.db.ExecContext(ctx, "INSERT INTO employees ...", user.ID, user.Username, hash)
// if err != nil { return err }
// s.emailClient.SendWelcome(user.Email)
// return nil
// }
//
// func (s *EmployeeStore) GetByUsername(ctx context.Context, username string) (*auth.User, error) {
// var emp Employee // Employee embeds auth.User
// if err := s.db.GetContext(ctx, &emp, "SELECT * FROM employees WHERE username = $1", username); err != nil {
// return nil, err
// }
// return &emp.User, nil // Return embedded User
// }
//
// Pass to TuGo config:
//
// tugo.New(tugo.Config{
// Auth: tugo.AuthConfig{
// CustomUserStore: &EmployeeStore{db: db, emailClient: emailClient},
// },
// })
//
CustomUserStore any // Must implement auth.UserStore interface
}
AuthConfig configures authentication.
type CollectionConfigMap ¶
type CollectionConfigMap map[string]CollectionItemConfig
CollectionConfigMap maps collection names to their configuration.
type CollectionItemConfig ¶
type CollectionItemConfig struct {
// Enabled determines if this collection is exposed via API.
Enabled bool
// PublicFields limits which fields are visible.
// nil means all fields are visible.
PublicFields []string
}
CollectionItemConfig holds configuration for a single collection.
type Config ¶
type Config struct {
// DB is an existing sqlx database connection.
// Either DB or DatabaseURL must be provided.
DB *sqlx.DB
// DatabaseURL is a PostgreSQL connection string.
// Used when DB is nil to create a new connection.
DatabaseURL string
// Discovery configures how tables are discovered and exposed.
Discovery DiscoveryConfig
// Auth configures authentication methods.
Auth AuthConfig
// Storage configures file storage providers.
Storage StorageConfig
// Server configures the HTTP server (standalone mode only).
Server ServerConfig
// Mount configures route mounting behavior.
Mount MountOptions
// Seed configures user seeding on first run.
Seed SeedConfig
// SchemaWatch configures automatic schema change detection.
SchemaWatch SchemaWatchConfig
}
Config holds the complete configuration for TuGo engine.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a configuration with sensible defaults.
type CookieConfig ¶
type CookieConfig struct {
// Name is the cookie name.
// Default: "tugo_session"
Name string
// MaxAge is the cookie max age in seconds.
MaxAge int
// Secure sets the Secure flag.
Secure bool
// HttpOnly sets the HttpOnly flag.
HttpOnly bool
// SameSite sets the SameSite attribute.
SameSite string
}
CookieConfig configures cookie-based sessions.
type DiscoveryConfig ¶
type DiscoveryConfig struct {
// Mode determines discovery strategy: "prefix", "config", or "hybrid".
// Default: "prefix"
Mode string
// Prefix is the table name prefix for auto-discovery.
// Default: "api_"
Prefix string
// AutoDiscover enables automatic exposure of discovered tables.
// Default: false (requires explicit enable)
AutoDiscover bool
// Blacklist contains table names to always exclude.
Blacklist []string
// Config provides per-collection configuration overrides.
Config CollectionConfigMap
}
DiscoveryConfig configures table discovery behavior.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the main TuGo engine.
func (*Engine) AdminHandler ¶
AdminHandler returns the admin handler.
func (*Engine) AuthMiddleware ¶
func (e *Engine) AuthMiddleware() gin.HandlerFunc
AuthMiddleware returns the auth middleware.
func (*Engine) AuthProvider ¶
AuthProvider returns the auth provider.
func (*Engine) GetCollections ¶
func (e *Engine) GetCollections() []*schema.Collection
GetCollections returns all discovered collections.
func (*Engine) HasCollection ¶
HasCollection checks if a collection exists.
func (*Engine) Mount ¶
func (e *Engine) Mount(rg *gin.RouterGroup)
Mount mounts the TuGo API routes to a Gin router group. This is the primary use case for middleware integration. If config.Mount.IncludeAdmin is true, admin routes are automatically registered.
func (*Engine) MountAdmin ¶
func (e *Engine) MountAdmin(rg *gin.RouterGroup)
MountAdmin mounts admin API routes (should be protected).
func (*Engine) MountWithAuth ¶
func (e *Engine) MountWithAuth(rg *gin.RouterGroup)
MountWithAuth mounts routes with authentication middleware.
func (*Engine) MountWithOptions ¶ added in v0.2.0
func (e *Engine) MountWithOptions(rg *gin.RouterGroup, opts MountOptions)
MountWithOptions mounts the TuGo API routes with custom options.
func (*Engine) RefreshSchema ¶
RefreshSchema re-discovers the database schema.
func (*Engine) SchemaManager ¶
SchemaManager returns the schema manager.
func (*Engine) SeedFromEnv ¶ added in v0.2.0
SeedFromEnv seeds users from environment variables. Looks for: TUGO_ADMIN_USERNAME, TUGO_ADMIN_EMAIL, TUGO_ADMIN_PASSWORD
func (*Engine) SeedUsers ¶ added in v0.2.0
SeedUsers seeds default users if configured and they don't exist. This is typically called during Init or manually after setup.
func (*Engine) StartSchemaWatcher ¶ added in v0.2.0
StartSchemaWatcher starts the schema watcher if configured.
func (*Engine) StopSchemaWatcher ¶ added in v0.2.0
func (e *Engine) StopSchemaWatcher()
StopSchemaWatcher stops the schema watcher.
func (*Engine) StorageManager ¶
StorageManager returns the storage manager.
func (*Engine) TOTPManager ¶
func (e *Engine) TOTPManager() *auth.TOTPManager
TOTPManager returns the TOTP manager.
func (*Engine) TriggerSchemaRefresh ¶ added in v0.2.0
TriggerSchemaRefresh manually triggers a schema refresh.
func (*Engine) ValidatorRegistry ¶
func (e *Engine) ValidatorRegistry() *validation.ValidatorRegistry
ValidatorRegistry returns the validator registry.
type JWTConfig ¶
type JWTConfig struct {
// Secret is the signing key for HS256.
Secret string
// Expiry is the token expiry time in seconds.
// Default: 86400 (24 hours)
Expiry int
// RefreshExp is the refresh token expiry in seconds.
// Default: 604800 (7 days)
RefreshExp int
// Issuer is the JWT issuer claim.
Issuer string
}
JWTConfig configures JWT authentication.
type MountOptions ¶ added in v0.2.0
type MountOptions struct {
// IncludeAdmin enables auto-registration of admin routes under /admin.
// Default: false
IncludeAdmin bool
// AdminPath is the path prefix for admin routes.
// Default: "/admin"
AdminPath string
// RequireAdminAuth requires admin role for admin routes.
// Default: true
RequireAdminAuth bool
}
MountOptions configures how TuGo mounts its routes.
func DefaultMountOptions ¶ added in v0.2.0
func DefaultMountOptions() MountOptions
DefaultMountOptions returns default mount options.
type PGListener ¶ added in v0.2.0
type PGListener struct {
// contains filtered or unexported fields
}
PGListener wraps PostgreSQL LISTEN/NOTIFY functionality.
func NewPGListener ¶ added in v0.2.0
func NewPGListener(db *sqlx.DB, channel string) (*PGListener, error)
NewPGListener creates a new PostgreSQL listener.
func (*PGListener) Notify ¶ added in v0.2.0
func (l *PGListener) Notify() <-chan struct{}
Notify returns the notification channel.
type SchemaWatchConfig ¶ added in v0.2.0
type SchemaWatchConfig struct {
// Enabled enables schema watching.
Enabled bool
// Mode is the watch mode: "poll" or "notify".
// "poll" uses periodic polling.
// "notify" uses PostgreSQL LISTEN/NOTIFY (more efficient).
// Default: "poll"
Mode string
// PollInterval is the interval between polls (for poll mode).
// Default: 30s
PollInterval time.Duration
// Channel is the PostgreSQL notification channel (for notify mode).
// Default: "tugo_schema_change"
Channel string
}
SchemaWatchConfig configures automatic schema change detection.
func DefaultSchemaWatchConfig ¶ added in v0.2.0
func DefaultSchemaWatchConfig() SchemaWatchConfig
DefaultSchemaWatchConfig returns default schema watch configuration.
type SchemaWatcher ¶ added in v0.2.0
type SchemaWatcher struct {
// contains filtered or unexported fields
}
SchemaWatcher watches for schema changes and triggers refresh.
func NewSchemaWatcher ¶ added in v0.2.0
func NewSchemaWatcher(engine *Engine, config SchemaWatchConfig) *SchemaWatcher
NewSchemaWatcher creates a new schema watcher.
func (*SchemaWatcher) Start ¶ added in v0.2.0
func (w *SchemaWatcher) Start(ctx context.Context) error
Start begins watching for schema changes.
func (*SchemaWatcher) Stop ¶ added in v0.2.0
func (w *SchemaWatcher) Stop()
Stop stops the schema watcher.
type SeedConfig ¶ added in v0.2.0
type SeedConfig struct {
// Enabled enables user seeding.
Enabled bool
// AdminUser is the default admin user configuration.
AdminUser *SeedUser
}
SeedConfig configures user seeding on first run.
type SeedUser ¶ added in v0.2.0
type SeedUser struct {
Username string
Email string
Password string
Role string // "admin", "user", etc.
}
SeedUser represents a user to seed.
type ServerConfig ¶
type ServerConfig struct {
// Port is the server port.
// Default: 8080
Port int
// ReadTimeout is the request read timeout.
ReadTimeout time.Duration
// WriteTimeout is the response write timeout.
WriteTimeout time.Duration
}
ServerConfig configures the HTTP server for standalone mode.
type StorageConfig ¶
type StorageConfig struct {
// Default is the default storage provider name.
Default string
// Providers maps names to storage provider implementations.
Providers map[string]StorageProvider
}
StorageConfig configures file storage.
type StorageProvider ¶
type StorageProvider interface {
// Upload stores a file and returns the storage path.
Upload(ctx any, file any, filename string, opts *UploadOptions) (string, error)
// Download retrieves a file by its storage path.
Download(ctx any, path string) (any, error)
// Delete removes a file by its storage path.
Delete(ctx any, path string) error
// GetURL returns a public URL for the file.
GetURL(path string) string
}
StorageProvider is the interface for file storage backends.
type TOTPConfig ¶
type TOTPConfig struct {
// Issuer is displayed in authenticator apps.
Issuer string
// Period is the TOTP period in seconds.
// Default: 30
Period int
// Digits is the number of digits in the code.
// Default: 6
Digits int
}
TOTPConfig configures TOTP authentication.
type UploadOptions ¶
type UploadOptions struct {
// ContentType is the MIME type.
ContentType string
// MaxSize is the maximum file size in bytes.
MaxSize int64
}
UploadOptions provides options for file uploads.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
middleware
command
Package main demonstrates TuGo integration as middleware in an existing Gin application.
|
Package main demonstrates TuGo integration as middleware in an existing Gin application. |
|
standalone
command
Package main demonstrates TuGo in standalone server mode.
|
Package main demonstrates TuGo in standalone server mode. |
|
with-auth
command
Package main demonstrates TuGo with full authentication including JWT and TOTP.
|
Package main demonstrates TuGo with full authentication including JWT and TOTP. |
|
pkg
|
|