116 lines
2.2 KiB
Go
116 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
// Configuration structure
|
|
type Config struct {
|
|
Port int
|
|
OpenSearch OpenSearchConfig
|
|
}
|
|
|
|
type OpenSearchConfig struct {
|
|
Domain string
|
|
}
|
|
|
|
// Default configuration values
|
|
var defaultConfig = Config{
|
|
Port: 5000,
|
|
OpenSearch: OpenSearchConfig{
|
|
Domain: "localhost",
|
|
},
|
|
}
|
|
|
|
const configFilePath = "config.json"
|
|
|
|
func main() {
|
|
// Run the initialization process
|
|
err := initConfig()
|
|
if err != nil {
|
|
fmt.Println("Error during initialization:", err)
|
|
return
|
|
}
|
|
|
|
// Start the main application
|
|
runServer()
|
|
}
|
|
|
|
func initConfig() error {
|
|
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
|
|
return createConfig()
|
|
}
|
|
|
|
fmt.Println("Configuration file already exists.")
|
|
return nil
|
|
}
|
|
|
|
func createConfig() error {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
fmt.Println("Configuration file not found.")
|
|
fmt.Print("Do you want to use default values? (yes/no): ")
|
|
useDefaults, _ := reader.ReadString('\n')
|
|
|
|
config := defaultConfig
|
|
if useDefaults != "yes\n" {
|
|
fmt.Print("Enter port (default 5000): ")
|
|
portStr, _ := reader.ReadString('\n')
|
|
if portStr != "\n" {
|
|
port, err := strconv.Atoi(portStr[:len(portStr)-1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
config.Port = port
|
|
}
|
|
|
|
fmt.Print("Enter your domain address (e.g., domain.com): ")
|
|
domain, _ := reader.ReadString('\n')
|
|
if domain != "\n" {
|
|
config.OpenSearch.Domain = domain[:len(domain)-1]
|
|
}
|
|
}
|
|
|
|
saveConfig(config)
|
|
return nil
|
|
}
|
|
|
|
func saveConfig(config Config) {
|
|
file, err := os.Create(configFilePath)
|
|
if err != nil {
|
|
fmt.Println("Error creating config file:", err)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
configData, err := json.MarshalIndent(config, "", " ")
|
|
if err != nil {
|
|
fmt.Println("Error marshalling config data:", err)
|
|
return
|
|
}
|
|
|
|
_, err = file.Write(configData)
|
|
if err != nil {
|
|
fmt.Println("Error writing to config file:", err)
|
|
}
|
|
}
|
|
|
|
func loadConfig() Config {
|
|
configFile, err := os.Open(configFilePath)
|
|
if err != nil {
|
|
log.Fatalf("Error opening config file: %v", err)
|
|
}
|
|
defer configFile.Close()
|
|
|
|
var config Config
|
|
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
|
log.Fatalf("Error decoding config file: %v", err)
|
|
}
|
|
|
|
return config
|
|
}
|