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:
params_ ?¶msparser.Params
redis_ ?&redisclient.Redis @[skip; str: skip]
path_ ?pathlib.Path
pub mut:
// snippets map[string]string
config ContextConfig
}
fn (Context) guid #
fn (self Context) guid() 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
// unhashed secret pub fn (mut self Context) secret_set(secret_ string) ! { secret := secret_.trim_space() secret2 := md5.hexhash(secret) self.config.secret = secret2 self.save()! }
fn (Context) redis #
fn (mut self Context) redis() !&redisclient.Redis
fn (Context) save #
fn (mut self Context) save() !
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) 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) env_set_once #
fn (mut self Session) env_set_once(key string, value string) !
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
}