RidgeChat/main.go
2023-09-06 14:00:14 -06:00

63 lines
1.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"RidgedChat/ui"
"os"
)
// Config holds the client configuration
type Config struct {
ServerAddress string `json:"server_address"`
Username string `json:"username"`
OfflineMode bool `json:"offline_mode"`
}
// ReadConfig reads the client configuration from a JSON file
func ReadConfig(filePath string) (*Config, error) {
file, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
var config Config
if err := json.Unmarshal(file, &config); err != nil {
return nil, err
}
return &config, nil
}
func main() {
// Read client configuration from client_config.json
config, err := ReadConfig("config/client_config.json")
if err != nil {
fmt.Println("Error reading config:", err)
os.Exit(1)
}
// Initialize the client with the server address and username from the config
client := NewClient(config.ServerAddress, config.Username)
// Decide whether to connect to the server based on the OfflineMode flag
if !config.OfflineMode {
if err := client.Connect(); err != nil {
fmt.Println("Error connecting to server:", err)
os.Exit(1)
}
go client.ListenForMessages() // Listen for messages only if connected to server
} else {
fmt.Println("Running in offline mode. No server connection.")
}
// Launch the GUI (moved to main goroutine)
ui.StartGUI()
// TODO: Implement channel joining and message sending logic
// TODO: Implement graceful shutdown logic
// No need for the select{} anymore, as StartGUI will block until the GUI window is closed
}