testing config file and cli

This commit is contained in:
Joshua Ashton
2024-08-31 11:58:32 -06:00
parent 9606c2d886
commit 053e50c248
3 changed files with 63 additions and 9 deletions
@@ -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 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= 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/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.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.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+61 -8
View File
@@ -14,10 +14,34 @@ import (
"time" "time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/quaxlyqueen/services" "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 var chats []services.Chat
// User HTTP GET request has the prompt decrypted and verified with a SHA-1 checksum. // 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 := services.PromptWHistory{}
apiCall.Model = model apiCall.Model = model
apiCall.Messages = chats 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.Print("Pre-JSON ification: ")
log.Println(apiCall) log.Println(apiCall)
@@ -118,24 +142,26 @@ func generate(w http.ResponseWriter, r *http.Request) {
} }
func serve() { func serve() {
// TODO: Authenticate user credentials to verify they are allowed to even access
var wg sync.WaitGroup var wg sync.WaitGroup
portfolioDir := "/home/violet/documents/development/portfolio/public_html/"
endpoint := []string{ endpoint := []string{
"/generate", "/generate",
"/chat", "/chat",
} }
function := []func(http.ResponseWriter, *http.Request){ function := []func(http.ResponseWriter, *http.Request){
generate, generate,
chat, chat,
} }
r := mux.NewRouter() r := mux.NewRouter()
services.servePage(r, portfolioDir, "/", 1112) services.servePage(r, viper.Get("webpage_dir"), "/", viper.Get("webpage_port"))
services.serveApi(r, "ai.joshashton.dev", endpoint, function, 1113) services.serveApi(r, viper.Get("api"), endpoint, function, viper.Get("api_port"))
addr := "0.0.0.0:", viper.Get("router_port")
srv := &http.Server{ srv := &http.Server{
Addr: "0.0.0.0:1111", Addr: addr,
// Good practice to set timeouts to avoid Slowloris attacks. // Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15, WriteTimeout: time.Second * 15,
ReadTimeout: 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) c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+Shift+C) // We'll accept graceful shutdowns when quit via SIGINT (Ctrl+Shift+C)
@@ -175,7 +201,34 @@ func serve() {
os.Exit(0) 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() { func main() {
// TODO: Authenticate user credentials to verify they are allowed to even access parseCLI()
serve() parseConfig()
//serve()
} }