Search/init.go

97 lines
2.2 KiB
Go

package main
import (
"flag"
"os"
"time"
)
type Config struct {
Port int
AuthCode string
PeerID string
Peers []string
Domain string
NodesEnabled bool
CrawlerEnabled bool
WebsiteEnabled bool
LogLevel int
HardCacheDuration time.Duration
HardCacheEnabled bool
}
var defaultConfig = Config{
Port: 5000,
Domain: "localhost",
Peers: []string{},
AuthCode: generateStrongRandomString(64),
NodesEnabled: true,
CrawlerEnabled: true,
WebsiteEnabled: true,
LogLevel: 1,
HardCacheDuration: 0,
}
const configFilePath = "config.ini"
var config Config
func main() {
// Command-line flags
portFlag := flag.Int("port", 0, "Port number to run the application (overrides config)")
domainFlag := flag.String("domain", "", "Domain address for the application (overrides config)")
skipConfigFlag := flag.Bool("skip-config-check", false, "Skip interactive prompts and load config.ini")
// Parse command-line flags
flag.Parse()
if *skipConfigFlag {
// Skip interactive configuration
if _, err := os.Stat(configFilePath); err == nil {
// Load from config file if it exists
config = loadConfig()
} else {
// Use defaults if config file does not exist
config = defaultConfig
saveConfig(config) // Save the defaults to config.ini
printInfo("Configuration saved to %s", configFilePath)
}
} else {
// Initialize configuration interactively or from config file
err := initConfig()
if err != nil {
printErr("Error during initialization:")
return
}
}
// Override with command-line arguments if provided
if *portFlag != 0 {
config.Port = *portFlag
}
if *domainFlag != "" {
config.Domain = *domainFlag
}
loadNodeConfig()
if config.AuthCode == "" {
config.AuthCode = generateStrongRandomString(64)
printInfo("Generated connection code: %s\n", config.AuthCode)
saveConfig(config)
}
// Generate Host ID
hostID, nodeErr := generateHostID()
if nodeErr != nil {
printErr("Failed to generate host ID: %v", nodeErr)
}
config.PeerID = hostID
InitializeLanguage("en") // Initialize language before generating OpenSearch
generateOpenSearchXML(config)
go startNodeClient()
runServer()
}