This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
71 lines (58 loc) · 1.54 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bufio"
_ "embed"
"fmt"
"github.com/goccy/go-yaml"
"log"
"os"
"strings"
)
type Config struct {
Listen string
Relays []string
NIP_11 map[string]interface{} `yaml:"nip_11"`
Favicon string
MaxConnectionsPerIP int `yaml:"max_connections_per_ip"`
}
//go:embed config.example.yaml
var exampleConf []byte
func createConf(filename string) {
fmt.Printf("Do you want to create config file? (%s) [y/N]: ", filename)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
v := scanner.Text()
switch strings.ToLower(v) {
case "y":
if err := os.WriteFile(filename, exampleConf, 0644); err != nil {
panic(fmt.Sprintf("failed to create config file: %s", err))
}
fmt.Printf("\nConfig file has been written to %s\n", filename)
fmt.Println("Please edit the config before starting bouncer.")
fmt.Println("\nAfter editing, Start the bouncer by running:")
if filename == "config.yaml" {
fmt.Println(" $ bostr2")
} else {
fmt.Printf(" $ bostr2 -configfile %s\n", filename)
}
os.Exit(0)
return
default:
fmt.Println("bostr2 cannot run without config.")
os.Exit(1)
return
}
}
func ReadConfig(filename string, c *Config) {
data, err := os.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
log.Println("Config file does not exist.")
createConf(filename)
}
panic(fmt.Sprintf("error when reading %s: %s", filename, err))
}
if err := yaml.Unmarshal(data, c); err != nil {
panic(fmt.Sprintf("error when parsing %s: %s", filename, err))
}
}