From 053e50c248ee4bc9e5a8111998741533b6b8631b Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Sat, 31 Aug 2024 11:58:32 -0600 Subject: [PATCH] testing config file and cli --- backend/src/security-layer/container/go.sum | 2 + .../src/security-layer/container/router.go | 69 ++++++++++++++++--- backend/src/security-layer/container/services | 1 - 3 files changed, 63 insertions(+), 9 deletions(-) delete mode 160000 backend/src/security-layer/container/services diff --git a/backend/src/security-layer/container/go.sum b/backend/src/security-layer/container/go.sum index 283b558..c50e37f 100644 --- a/backend/src/security-layer/container/go.sum +++ b/backend/src/security-layer/container/go.sum @@ -55,6 +55,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quaxlyqueen/services v1.0.0 h1:k3VaNSHydxetGVGkv8zKV8H/FlVtLtcDCiENyIAYvjo= +github.com/quaxlyqueen/services v1.0.0/go.mod h1:mTP/H7h+u+H/NzcNh3igIP/hkLA4ETYRU9dh9pUfClM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/backend/src/security-layer/container/router.go b/backend/src/security-layer/container/router.go index 40effc5..61c41b2 100644 --- a/backend/src/security-layer/container/router.go +++ b/backend/src/security-layer/container/router.go @@ -14,10 +14,34 @@ import ( "time" "github.com/gorilla/mux" + "github.com/spf13/pflag" + "github.com/spf13/viper" "github.com/quaxlyqueen/services" ) +type WebpagesStructure struct { + Domain string `mapstructure:"domain"` + WebpageDir string `mapstructure:"webpage_dir"` + WebpagePort string `mapstructure:"webpage_port"` +} + +type ConfigStructure struct { + TextModel string `mapstructure:"text_model"` + ImageModel string `mapstructure:"image_model"` + VideoModel string `mapstructure:"video_model"` + DocModel string `mapstructure:"doc_model"` + ResponseStream string `mapstructure:"response_stream"` + + Domain string `mapstructure:"domain"` + RouterPort string `mapstructure:"router_port"` + API string `mapstructure:"api"` + APIPort string `mapstructure:"api_port"` + Webpages WebpagesStructure `mapstructure:"webpages"` +} + +var CONFIG_DIR string +var config ConfigStructure var chats []services.Chat // User HTTP GET request has the prompt decrypted and verified with a SHA-1 checksum. @@ -45,7 +69,7 @@ func chat(w http.ResponseWriter, r *http.Request) { apiCall := services.PromptWHistory{} apiCall.Model = model apiCall.Messages = chats - apiCall.Stream = false // TODO: Allow for this as a user setting... + apiCall.Stream = viper.Get("response_stream") log.Print("Pre-JSON ification: ") log.Println(apiCall) @@ -118,24 +142,26 @@ func generate(w http.ResponseWriter, r *http.Request) { } func serve() { + // TODO: Authenticate user credentials to verify they are allowed to even access var wg sync.WaitGroup - portfolioDir := "/home/violet/documents/development/portfolio/public_html/" endpoint := []string{ "/generate", "/chat", } + function := []func(http.ResponseWriter, *http.Request){ generate, chat, } r := mux.NewRouter() - services.servePage(r, portfolioDir, "/", 1112) - services.serveApi(r, "ai.joshashton.dev", endpoint, function, 1113) + services.servePage(r, viper.Get("webpage_dir"), "/", viper.Get("webpage_port")) + services.serveApi(r, viper.Get("api"), endpoint, function, viper.Get("api_port")) + addr := "0.0.0.0:", viper.Get("router_port") srv := &http.Server{ - Addr: "0.0.0.0:1111", + Addr: addr, // Good practice to set timeouts to avoid Slowloris attacks. WriteTimeout: time.Second * 15, ReadTimeout: time.Second * 15, @@ -152,7 +178,7 @@ func serve() { } }() - log.Println("Router is running on port 1111") + log.Println("Router is running on port ", viper.Get("router_port")) c := make(chan os.Signal, 1) // We'll accept graceful shutdowns when quit via SIGINT (Ctrl+Shift+C) @@ -175,7 +201,34 @@ func serve() { os.Exit(0) } +func parseCLI() { + // Define CLI option, shorthand, default value, and description + // TODO: Dynamically obtain default config location from environment variables. + pflag.StringP("config", "c", "/home/violet/.config/one-ai/default.json", "Configuration file used in initializing One AI.") + + pflag.Parse() + viper.BindPFlags(pflag.CommandLine) + + // Retrieve CLI argument, either the default value or the user provided value. + CONFIG_DIR = viper.GetString("config") +} + +func parseConfig() { + viper.SetConfigType("json") + viper.SetConfigFile(CONFIG) + viper.ReadInConfig() + + err := viper.Unmarshal(&config) + if err != nil { + fmt.Println("error unmarshalling config file") + return + } else { + fmt.Println(config) + } +} + func main() { - // TODO: Authenticate user credentials to verify they are allowed to even access - serve() + parseCLI() + parseConfig() + //serve() } diff --git a/backend/src/security-layer/container/services b/backend/src/security-layer/container/services deleted file mode 160000 index 1cdc735..0000000 --- a/backend/src/security-layer/container/services +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1cdc735154a5363fa33f01d1f2f1319e26c433c8