change .json to .ini
This commit is contained in:
parent
5660167495
commit
5294421190
5 changed files with 59 additions and 52 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
config.json
|
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
|
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.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
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=
|
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"
|
"bufio"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -14,28 +13,23 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Port int
|
Port int
|
||||||
AuthCode string
|
AuthCode string
|
||||||
Peers []string
|
Peers []string
|
||||||
PeerID string
|
PeerID string
|
||||||
OpenSearch OpenSearchConfig
|
Domain string
|
||||||
}
|
|
||||||
|
|
||||||
type OpenSearchConfig struct {
|
|
||||||
Domain string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultConfig = Config{
|
var defaultConfig = Config{
|
||||||
Port: 5000,
|
Port: 5000,
|
||||||
OpenSearch: OpenSearchConfig{
|
Domain: "localhost",
|
||||||
Domain: "localhost",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const configFilePath = "config.json"
|
const configFilePath = "config.ini"
|
||||||
|
|
||||||
var config Config
|
var config Config
|
||||||
var configLock sync.RWMutex
|
var configLock sync.RWMutex
|
||||||
|
@ -91,33 +85,25 @@ func createConfig() error {
|
||||||
fmt.Print("Do you want to use default values? (yes/no): ")
|
fmt.Print("Do you want to use default values? (yes/no): ")
|
||||||
useDefaults, _ := reader.ReadString('\n')
|
useDefaults, _ := reader.ReadString('\n')
|
||||||
|
|
||||||
config := defaultConfig
|
if strings.TrimSpace(useDefaults) != "yes" {
|
||||||
if useDefaults != "yes\n" {
|
|
||||||
fmt.Print("Enter port (default 5000): ")
|
fmt.Print("Enter port (default 5000): ")
|
||||||
portStr, _ := reader.ReadString('\n')
|
portStr, _ := reader.ReadString('\n')
|
||||||
if portStr != "\n" {
|
if portStr != "\n" {
|
||||||
port, err := strconv.Atoi(portStr[:len(portStr)-1])
|
port, err := strconv.Atoi(strings.TrimSpace(portStr))
|
||||||
if err != nil {
|
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')
|
domain, _ := reader.ReadString('\n')
|
||||||
if domain != "\n" {
|
if domain != "\n" {
|
||||||
config.OpenSearch.Domain = domain[:len(domain)-1]
|
config.Domain = strings.TrimSpace(domain)
|
||||||
}
|
|
||||||
|
|
||||||
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), ",")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
config = defaultConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.AuthCode == "" {
|
if config.AuthCode == "" {
|
||||||
|
@ -130,35 +116,50 @@ func createConfig() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveConfig(config Config) {
|
func saveConfig(config Config) {
|
||||||
file, err := os.Create(configFilePath)
|
cfg := ini.Empty()
|
||||||
if err != nil {
|
sec := cfg.Section("")
|
||||||
fmt.Println("Error creating config file:", err)
|
sec.Key("Port").SetValue(strconv.Itoa(config.Port))
|
||||||
return
|
sec.Key("AuthCode").SetValue(config.AuthCode)
|
||||||
}
|
sec.Key("PeerID").SetValue(config.PeerID)
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
configData, err := json.MarshalIndent(config, "", " ")
|
peers := strings.Join(config.Peers, ",")
|
||||||
if err != nil {
|
sec.Key("Peers").SetValue(peers)
|
||||||
fmt.Println("Error marshalling config data:", err)
|
sec.Key("Domain").SetValue(config.Domain)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = file.Write(configData)
|
err := cfg.SaveTo(configFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error writing to config file:", err)
|
fmt.Println("Error writing to config file:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig() Config {
|
func loadConfig() Config {
|
||||||
configFile, err := os.Open(configFilePath)
|
cfg, err := ini.Load(configFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error opening config file: %v", err)
|
log.Fatalf("Error opening config file: %v", err)
|
||||||
}
|
}
|
||||||
defer configFile.Close()
|
|
||||||
|
|
||||||
var config Config
|
port, err := cfg.Section("").Key("Port").Int()
|
||||||
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
if err != nil || port == 0 {
|
||||||
log.Fatalf("Error decoding config file: %v", err)
|
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
|
return config
|
||||||
|
|
|
@ -28,7 +28,7 @@ func generateOpenSearchXML(config Config) {
|
||||||
Tags: "search, engine",
|
Tags: "search, engine",
|
||||||
URL: URL{
|
URL: URL{
|
||||||
Type: "text/html",
|
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