This commit is contained in:
Dave Lee 2023-06-12 17:55:03 -04:00
parent 115766205c
commit d4c6407bf4
6 changed files with 71 additions and 109 deletions

View file

@ -8,6 +8,7 @@ import (
"sync"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
)
@ -24,7 +25,6 @@ func NewConfigManager() *ConfigManager {
// Private helper method doesn't enforce the mutex. This is because loading at the directory level keeps the lock up the whole time, and I like that.
func (cm *ConfigManager) loadConfigFile(path string) (*Config, error) {
fmt.Printf("INTERNAL loadConfigFile for %s\n", path)
stub := ConfigStub{}
f, err := os.ReadFile(path)
if err != nil {
@ -33,62 +33,42 @@ func (cm *ConfigManager) loadConfigFile(path string) (*Config, error) {
if err := yaml.Unmarshal(f, &stub); err != nil {
return nil, fmt.Errorf("cannot unmarshal config file: %w", err)
}
fmt.Printf("RAW STUB: %+v\n", stub)
endpoint := stub.Registration.Endpoint
// EndpointConfigMap is generated over in localai.gen.go
// It's a map that translates a string endpoint function name to an empty SpecificConfig[T], with the type parameter for that request.
// We then dump the raw YAML configuration of that request into a map[string]interface{}
// mapstructure then copies the fields into our specific SpecificConfig[T]
if structType, ok := EndpointConfigMap[endpoint]; ok {
fmt.Printf("~~ EndpointConfigMap[%s]: %+v\n", endpoint, structType)
tmpUnmarshal := map[string]interface{}{}
if err := yaml.Unmarshal(f, &tmpUnmarshal); err != nil {
if e, ok := err.(*yaml.TypeError); ok {
fmt.Println("\n!!!!!Type error:", e)
log.Error().Msgf("[ConfigManager::loadConfigFile] Type error: %s", e.Error())
}
return nil, fmt.Errorf("cannot unmarshal config file for %s: %w", endpoint, err)
}
fmt.Printf("$$$ tmpUnmarshal: %+v\n", tmpUnmarshal)
mapstructure.Decode(tmpUnmarshal, &structType)
fmt.Printf("AFTER UNMARSHAL %T\n%+v\n=======\n", structType, structType)
// rawConfig.RequestDefaults = structType.GetRequestDefaults()
cm.configs[structType.GetRegistration()] = structType
// fmt.Printf("\n\n\n!!!!!HIT BOTTOM!!!!!!")
return &structType, nil
// fmt.Printf("\n\n\n!!!!!\n\n\nBIG MISS!\n\n%+v\n\n%T\n%T=====", specificStruct, specificStruct, structType)
}
// for i, ts := range EndpointToRequestBodyMap {
// fmt.Printf("%s: %+v\n", i, ts)
// }
return nil, fmt.Errorf("failed to parse config for endpoint %s", endpoint)
}
func (cm *ConfigManager) LoadConfigFile(path string) (*Config, error) {
fmt.Printf("LoadConfigFile TOP for %s", path)
cm.Lock()
fmt.Println("cm.Lock done")
defer cm.Unlock()
fmt.Println("cm.Unlock done")
return cm.loadConfigFile(path)
}
func (cm *ConfigManager) LoadConfigDirectory(path string) ([]ConfigRegistration, error) {
fmt.Printf("LoadConfigDirectory TOP for %s\n", path)
cm.Lock()
defer cm.Unlock()
files, err := os.ReadDir(path)
if err != nil {
return []ConfigRegistration{}, err
}
fmt.Printf("os.ReadDir done, found %d files\n", len(files))
log.Debug().Msgf("[ConfigManager::LoadConfigDirectory] os.ReadDir done, found %d files\n", len(files))
for _, file := range files {
// Skip anything that isn't yaml
@ -100,9 +80,6 @@ func (cm *ConfigManager) LoadConfigDirectory(path string) ([]ConfigRegistration,
return []ConfigRegistration{}, err
}
}
fmt.Printf("LoadConfigDirectory DONE %d", len(cm.configs))
return cm.listConfigs(), nil
}