This commit is contained in:
2025-08-16 10:07:35 +02:00
parent 27bc172257
commit be19609855
13 changed files with 61 additions and 1313 deletions

View File

@@ -2,23 +2,14 @@ module base
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.core.redisclient
import freeflowuniverse.herolib.data.dbfs
import freeflowuniverse.herolib.crypt.aes_symmetric
import freeflowuniverse.herolib.ui
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.rootpath
import json
import os
import crypto.md5
@[heap]
pub struct Context {
mut:
// priv_key_ ?&secp256k1.Secp256k1 @[skip; str: skip]
params_ ?&paramsparser.Params
dbcollection_ ?&dbfs.DBCollection @[skip; str: skip]
redis_ ?&redisclient.Redis @[skip; str: skip]
path_ ?pathlib.Path
pub mut:
@@ -34,10 +25,10 @@ pub mut:
params string
coderoot string
interactive bool
secret string // is hashed secret
priv_key string // encrypted version
db_path string // path to dbcollection
encrypt bool
// secret string // is hashed secret
// priv_key string // encrypted version
// db_path string // path to dbcollection
// encrypt bool
}
// return the gistructure as is being used in context
@@ -97,105 +88,44 @@ fn (mut self Context) cfg_redis_exists() !bool {
return r.exists('context:config')!
}
// return db collection
pub fn (mut self Context) dbcollection() !&dbfs.DBCollection {
mut dbc2 := self.dbcollection_ or {
if self.config.db_path.len == 0 {
self.config.db_path = '${os.home_dir()}/hero/db/${self.config.id}'
}
mut dbc := dbfs.get(
contextid: self.config.id
dbpath: self.config.db_path
secret: self.config.secret
)!
self.dbcollection_ = &dbc
&dbc
}
return dbc2
}
pub fn (mut self Context) db_get(dbname string) !dbfs.DB {
mut dbc := self.dbcollection()!
return dbc.db_get_create(name: dbname, withkeys: true)!
}
// always return the config db which is the same for all apps in context
pub fn (mut self Context) db_config_get() !dbfs.DB {
mut dbc := self.dbcollection()!
return dbc.db_get_create(name: 'config', withkeys: true)!
}
// pub fn (mut self Context) hero_config_set(cat string, name string, content_ string) ! {
// mut content := texttools.dedent(content_)
// content = rootpath.shell_expansion(content)
// path := '${self.path()!.path}/${cat}/${name}.json'
// mut config_file := pathlib.get_file(path: path,create: true)!
// config_file.write(content)!
// pub fn (mut self Context) secret_encrypt(txt string) !string {
// return aes_symmetric.encrypt_str(txt, self.secret_get()!)
// }
// pub fn (mut self Context) hero_config_delete(cat string, name string) ! {
// path := '${self.path()!.path}/${cat}/${name}.json'
// mut config_file := pathlib.get_file(path: path)!
// config_file.delete()!
// pub fn (mut self Context) secret_decrypt(txt string) !string {
// return aes_symmetric.decrypt_str(txt, self.secret_get()!)
// }
// pub fn (mut self Context) hero_config_exists(cat string, name string) bool {
// path := '${os.home_dir()}/hero/context/${self.config.name}/${cat}/${name}.json'
// return os.exists(path)
// pub fn (mut self Context) secret_get() !string {
// mut secret := self.config.secret
// if secret == '' {
// self.secret_configure()!
// secret = self.config.secret
// self.save()!
// }
// if secret == '' {
// return error("can't get secret")
// }
// return secret
// }
// pub fn (mut self Context) hero_config_get(cat string, name string) !string {
// path := '${self.path()!.path}/${cat}/${name}.json'
// mut config_file := pathlib.get_file(path: path, create: false)!
// return config_file.read()!
// // show a UI in console to configure the secret
// pub fn (mut self Context) secret_configure() ! {
// mut myui := ui.new()!
// console.clear()
// secret_ := myui.ask_question(question: 'Please enter your hero secret string:')!
// self.secret_set(secret_)!
// }
// pub fn (mut self Context) hero_config_list(cat string) ![]string {
// path := '${self.path()!.path}/${cat}'
// mut config_files := os.ls(path)!
// config_files = config_files.filter(it.ends_with('.json').map(it.split('.')[0] or {panic("bug")})
// return config_files
// // 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()!
// }
pub fn (mut self Context) secret_encrypt(txt string) !string {
return aes_symmetric.encrypt_str(txt, self.secret_get()!)
}
pub fn (mut self Context) secret_decrypt(txt string) !string {
return aes_symmetric.decrypt_str(txt, self.secret_get()!)
}
pub fn (mut self Context) secret_get() !string {
mut secret := self.config.secret
if secret == '' {
self.secret_configure()!
secret = self.config.secret
self.save()!
}
if secret == '' {
return error("can't get secret")
}
return secret
}
// show a UI in console to configure the secret
pub fn (mut self Context) secret_configure() ! {
mut myui := ui.new()!
console.clear()
secret_ := myui.ask_question(question: 'Please enter your hero secret string:')!
self.secret_set(secret_)!
}
// 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()!
}
pub fn (mut self Context) path() !pathlib.Path {
return self.path_ or {
path2 := '${os.home_dir()}/hero/context/${self.config.name}'

View File

@@ -36,30 +36,12 @@ pub fn context_new(args_ ContextConfigArgs) !&Context {
params: args_.params
coderoot: args_.coderoot
interactive: args_.interactive
secret: args_.secret
encrypt: args_.encrypt
}
if args.encrypt && args.secret == '' && args.interactive {
mut myui := ui.new()!
console.clear()
args.secret = myui.ask_question(question: 'Please enter your hero secret string:')!
}
if args.encrypt && args.secret.len > 0 {
args.secret = md5.hexhash(args.secret)
}
mut c := Context{
config: args
}
// if args_.priv_key_hex.len > 0 {
// c.privkey_set(args_.priv_key_hex)!
// }
// c.save()!
if args.params.len > 0 {
mut p := paramsparser.new('')!
c.params_ = &p

View File

@@ -3,7 +3,6 @@ module base
import freeflowuniverse.herolib.data.ourtime
// import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.data.dbfs
import freeflowuniverse.herolib.core.logger
import json
import freeflowuniverse.herolib.core.pathlib
@@ -32,15 +31,6 @@ pub mut:
// return 'hero:sessions:${self.guid()}'
// }
// get db of the session, is unique per session
pub fn (mut self Session) db_get() !dbfs.DB {
return self.context.db_get('session_${self.name}')!
}
// get the db of the config, is unique per context
pub fn (mut self Session) db_config_get() !dbfs.DB {
return self.context.db_get('config')!
}
// load the params from redis
pub fn (mut self Session) load() ! {