core.base #
Base Module
The Base module is a foundational component of the Hero framework that provides essential context and session management functionality.
Features
Context Management: Handles application context with support for:
Parameter management
Redis integration
Database collections
Configuration storage and retrieval
Path management
Session Handling: Provides session management capabilities through the Base and Session structures
Configuration Management:
heroscript configuration system
Support for environment variable expansion
Security Features:
Secret management
AES symmetric encryption/decryption
Secure configuration storage
Database Integration:
Redis database support with automatic database selection
File-based database collections
Key-value storage capabilities
Core Components
Context
The Context
struct is the central component that manages:- Application parameters
- Database connections
- Redis client
- File paths
- Configuration settings
Base
The Base
struct provides:- Session management
- Instance tracking
- Configuration type handling
Usage
The base module is typically used as a foundation for other Hero framework components. It provides the necessary infrastructure for:
- Managing application state
- Handling configurations
- Managing database connections
- Securing sensitive data
- Organizing application resources
Configuration
The module supports various configuration options through the ContextConfig
struct:- id
: Unique identifier for the context
name
: Context name (defaults to 'default')params
: Parameter stringcoderoot
: Root path for codeinteractive
: Interactive mode flagsecret
: Hashed secret for encryptiondb_path
: Path to database collectionencrypt
: Encryption flag
Security
The module includes built-in security features:- Secret management with encryption
- Secure configuration storage
- MD5 hashing for secrets
- AES symmetric encryption for sensitive data
fn context #
fn context() !&Context
fn context_get #
fn context_get(id u32) !&Context
fn context_new #
fn context_new(args_ ContextConfigArgs) !&Context
configure a context object params: .
id u32 //if not set then redis will get a new context id
name string = 'default'
params string
coderoot string
interactive bool
secret string
priv_key_hex string //hex representation of private key
fn context_select #
fn context_select(id u32) !&Context
enum ErrorType #
enum ErrorType {
uknown
value
}
struct Base #
struct Base {
configtype string @[required]
mut:
instance string
session_ ?&Session
}
fn (Base) session #
fn (mut self Base) session() !&Session
struct Context #
struct Context {
mut:
// priv_key_ ?&secp256k1.Secp256k1 @[skip; str: skip]
params_ ?¶msparser.Params
dbcollection_ ?&dbfs.DBCollection @[skip; str: skip]
redis_ ?&redisclient.Redis @[skip; str: skip]
path_ ?pathlib.Path
pub mut:
// snippets map[string]string
config ContextConfig
}
fn (Context) db_config_get #
fn (mut self Context) db_config_get() !dbfs.DB
always return the config db which is the same for all apps in context
fn (Context) db_get #
fn (mut self Context) db_get(dbname string) !dbfs.DB
fn (Context) dbcollection #
fn (mut self Context) dbcollection() !&dbfs.DBCollection
return db collection
fn (Context) guid #
fn (self Context) guid() string
fn (Context) hero_config_delete #
fn (mut self Context) hero_config_delete(cat string, name string) !
fn (Context) hero_config_exists #
fn (mut self Context) hero_config_exists(cat string, name string) bool
fn (Context) hero_config_get #
fn (mut self Context) hero_config_get(cat string, name string) !string
fn (Context) hero_config_set #
fn (mut self Context) hero_config_set(cat string, name string, content_ string) !
fn (Context) id #
fn (self Context) id() string
fn (Context) load #
fn (mut self Context) load() !
get context from out of redis
fn (Context) name #
fn (self Context) name() string
fn (Context) params #
fn (mut self Context) params() !¶msparser.Params
return the gistructure as is being used in context
fn (Context) path #
fn (mut self Context) path() !pathlib.Path
fn (Context) redis #
fn (mut self Context) redis() !&redisclient.Redis
fn (Context) save #
fn (mut self Context) save() !
fn (Context) secret_configure #
fn (mut self Context) secret_configure() !
show a UI in console to configure the secret
fn (Context) secret_decrypt #
fn (mut self Context) secret_decrypt(txt string) !string
fn (Context) secret_encrypt #
fn (mut self Context) secret_encrypt(txt string) !string
fn (Context) secret_get #
fn (mut self Context) secret_get() !string
fn (Context) secret_set #
fn (mut self Context) secret_set(secret_ string) !
unhashed secret
fn (Context) session_get #
fn (mut context Context) session_get(args_ ContextSessionGetArgs) !Session
fn (Context) session_latest #
fn (mut context Context) session_latest() !Session
fn (Context) session_new #
fn (mut context Context) session_new(args_ SessionConfig) !Session
get a session object based on the name / params:
name string
struct ContextConfig #
struct ContextConfig {
pub mut:
id u32 @[required]
name string = 'default'
params string
coderoot string
interactive bool
secret string // is hashed secret
priv_key string // encrypted version
db_path string // path to dbcollection
encrypt bool
}
struct ContextConfigArgs #
struct ContextConfigArgs {
pub mut:
id u32
name string = 'default'
params string
coderoot string
interactive bool
secret string
encrypt bool
priv_key_hex string // hex representation of private key
}
struct ContextSessionGetArgs #
struct ContextSessionGetArgs {
pub mut:
name string
}
struct ErrorArgs #
struct ErrorArgs {
pub mut:
cat string
error string
errortype ErrorType
}
struct ErrorItem #
struct ErrorItem {
pub mut:
time ourtime.OurTime
cat string
error string
errortype ErrorType
session string // the unique name for the session
}
struct Session #
struct Session {
mut:
path_ ?pathlib.Path
logger_ ?logger.Logger
pub mut:
name string // unique id for session (session id), can be more than one per context
interactive bool = true
params paramsparser.Params
start ourtime.OurTime
end ourtime.OurTime
context &Context @[skip; str: skip]
config SessionConfig
env map[string]string
}
fn (Session) check #
fn (self Session) check() !
//////// REPRESENTATION
fn (Session) db_config_get #
fn (mut self Session) db_config_get() !dbfs.DB
get the db of the config, is unique per context
fn (Session) db_get #
fn (mut self Session) db_get() !dbfs.DB
get db of the session, is unique per session
fn (Session) env_delete #
fn (mut self Session) env_delete(key string)
Delete an environment variable
fn (Session) env_get #
fn (mut self Session) env_get(key string) !string
Get an environment variable
fn (Session) env_set #
fn (mut self Session) env_set(key string, value string) !
Set an environment variable
fn (Session) error #
fn (mut session Session) error(args_ ErrorArgs) !ErrorItem
fn (Session) guid #
fn (self Session) guid() string
fn (Session) load #
fn (mut self Session) load() !
load the params from redis
fn (Session) logger #
fn (mut session Session) logger() !logger.Logger
fn (Session) path #
fn (mut self Session) path() !pathlib.Path
fn (Session) save #
fn (mut self Session) save() !
save the params to redis
struct SessionConfig #
struct SessionConfig {
pub mut:
name string // unique name for session (id), there can be more than 1 session per context
start string // can be e.g. +1h
description string
params string
}
- README
- fn context
- fn context_get
- fn context_new
- fn context_select
- enum ErrorType
- struct Base
- struct Context
- fn db_config_get
- fn db_get
- fn dbcollection
- fn guid
- fn hero_config_delete
- fn hero_config_exists
- fn hero_config_get
- fn hero_config_set
- fn id
- fn load
- fn name
- fn params
- fn path
- fn redis
- fn save
- fn secret_configure
- fn secret_decrypt
- fn secret_encrypt
- fn secret_get
- fn secret_set
- fn session_get
- fn session_latest
- fn session_new
- struct ContextConfig
- struct ContextConfigArgs
- struct ContextSessionGetArgs
- struct ErrorArgs
- struct ErrorItem
- struct Session
- struct SessionConfig