change .json to .ini
This commit is contained in:
parent
5660167495
commit
5294421190
5 changed files with 59 additions and 52 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
config.json
|
||||
opensearch.xml
|
||||
opensearch.xml
|
||||
config.ini
|
5
go.mod
5
go.mod
|
@ -22,4 +22,7 @@ require (
|
|||
golang.org/x/time v0.5.0 // indirect
|
||||
)
|
||||
|
||||
require github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
require (
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -71,3 +71,5 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
|
|||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
|
|
99
init.go
99
init.go
|
@ -4,7 +4,6 @@ import (
|
|||
"bufio"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -14,28 +13,23 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port int
|
||||
AuthCode string
|
||||
Peers []string
|
||||
PeerID string
|
||||
OpenSearch OpenSearchConfig
|
||||
}
|
||||
|
||||
type OpenSearchConfig struct {
|
||||
Domain string
|
||||
Port int
|
||||
AuthCode string
|
||||
Peers []string
|
||||
PeerID string
|
||||
Domain string
|
||||
}
|
||||
|
||||
var defaultConfig = Config{
|
||||
Port: 5000,
|
||||
OpenSearch: OpenSearchConfig{
|
||||
Domain: "localhost",
|
||||
},
|
||||
Port: 5000,
|
||||
Domain: "localhost",
|
||||
}
|
||||
|
||||
const configFilePath = "config.json"
|
||||
const configFilePath = "config.ini"
|
||||
|
||||
var config Config
|
||||
var configLock sync.RWMutex
|
||||
|
@ -91,33 +85,25 @@ func createConfig() error {
|
|||
fmt.Print("Do you want to use default values? (yes/no): ")
|
||||
useDefaults, _ := reader.ReadString('\n')
|
||||
|
||||
config := defaultConfig
|
||||
if useDefaults != "yes\n" {
|
||||
if strings.TrimSpace(useDefaults) != "yes" {
|
||||
fmt.Print("Enter port (default 5000): ")
|
||||
portStr, _ := reader.ReadString('\n')
|
||||
if portStr != "\n" {
|
||||
port, err := strconv.Atoi(portStr[:len(portStr)-1])
|
||||
port, err := strconv.Atoi(strings.TrimSpace(portStr))
|
||||
if err != nil {
|
||||
return err
|
||||
config.Port = 5000
|
||||
} else {
|
||||
config.Port = port
|
||||
}
|
||||
config.Port = port
|
||||
}
|
||||
|
||||
fmt.Print("Enter your domain address (e.g., domain.com): ")
|
||||
fmt.Print("Enter your domain address (default localhost): ")
|
||||
domain, _ := reader.ReadString('\n')
|
||||
if domain != "\n" {
|
||||
config.OpenSearch.Domain = domain[:len(domain)-1]
|
||||
}
|
||||
|
||||
fmt.Print("Do you want to connect to other nodes? (yes/no): ")
|
||||
connectNodes, _ := reader.ReadString('\n')
|
||||
if strings.TrimSpace(connectNodes) == "yes" {
|
||||
fmt.Println("Enter peer addresses (comma separated, e.g., /ip4/127.0.0.1/tcp/5000,/ip4/127.0.0.1/tcp/5001): ")
|
||||
peersStr, _ := reader.ReadString('\n')
|
||||
if peersStr != "\n" {
|
||||
config.Peers = strings.Split(strings.TrimSpace(peersStr), ",")
|
||||
}
|
||||
config.Domain = strings.TrimSpace(domain)
|
||||
}
|
||||
} else {
|
||||
config = defaultConfig
|
||||
}
|
||||
|
||||
if config.AuthCode == "" {
|
||||
|
@ -130,35 +116,50 @@ func createConfig() error {
|
|||
}
|
||||
|
||||
func saveConfig(config Config) {
|
||||
file, err := os.Create(configFilePath)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating config file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
cfg := ini.Empty()
|
||||
sec := cfg.Section("")
|
||||
sec.Key("Port").SetValue(strconv.Itoa(config.Port))
|
||||
sec.Key("AuthCode").SetValue(config.AuthCode)
|
||||
sec.Key("PeerID").SetValue(config.PeerID)
|
||||
|
||||
configData, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
fmt.Println("Error marshalling config data:", err)
|
||||
return
|
||||
}
|
||||
peers := strings.Join(config.Peers, ",")
|
||||
sec.Key("Peers").SetValue(peers)
|
||||
sec.Key("Domain").SetValue(config.Domain)
|
||||
|
||||
_, err = file.Write(configData)
|
||||
err := cfg.SaveTo(configFilePath)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing to config file:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func loadConfig() Config {
|
||||
configFile, err := os.Open(configFilePath)
|
||||
cfg, err := ini.Load(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)
|
||||
port, err := cfg.Section("").Key("Port").Int()
|
||||
if err != nil || port == 0 {
|
||||
port = 5000 // Default to 5000 if not set or error
|
||||
}
|
||||
|
||||
peersStr := cfg.Section("").Key("Peers").String()
|
||||
var peers []string
|
||||
if peersStr != "" {
|
||||
peers = strings.Split(peersStr, ",")
|
||||
}
|
||||
|
||||
domain := cfg.Section("").Key("Domain").String()
|
||||
if domain == "" {
|
||||
domain = "localhost" // Default to localhost if not set
|
||||
}
|
||||
|
||||
config = Config{
|
||||
Port: port,
|
||||
AuthCode: cfg.Section("").Key("AuthCode").String(),
|
||||
PeerID: cfg.Section("").Key("PeerID").String(),
|
||||
Peers: peers,
|
||||
Domain: domain,
|
||||
}
|
||||
|
||||
return config
|
||||
|
|
|
@ -28,7 +28,7 @@ func generateOpenSearchXML(config Config) {
|
|||
Tags: "search, engine",
|
||||
URL: URL{
|
||||
Type: "text/html",
|
||||
Template: fmt.Sprintf("https://%s/search?q={searchTerms}", config.OpenSearch.Domain),
|
||||
Template: fmt.Sprintf("https://%s/search?q={searchTerms}", config.Domain),
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue