wip new cfg params

This commit is contained in:
partisan 2024-08-10 13:27:23 +02:00
parent 26e404f6bc
commit aa5943cd13
7 changed files with 141 additions and 49 deletions

View file

@ -9,7 +9,6 @@ import (
) )
var ( var (
debugMode bool = true
funcs = template.FuncMap{ funcs = template.FuncMap{
"sub": func(a, b int) int { "sub": func(a, b int) int {
return a - b return a - b

View file

@ -2,7 +2,6 @@ package main
import ( import (
"bufio" "bufio"
"fmt"
"log" "log"
"os" "os"
"strconv" "strconv"
@ -18,7 +17,7 @@ func initConfig() error {
return createConfig() return createConfig()
} }
fmt.Println("Configuration file already exists.") printInfo("Configuration file already exists.")
config = loadConfig() config = loadConfig()
return nil return nil
} }
@ -26,12 +25,12 @@ func initConfig() error {
func createConfig() error { func createConfig() error {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
fmt.Println("Configuration file not found.") printMessage("Configuration file not found.")
fmt.Print("Do you want to use default values? (yes/no): ") printMessage("Do you want to use default values? (yes/no): ")
useDefaults, _ := reader.ReadString('\n') useDefaults, _ := reader.ReadString('\n')
if strings.TrimSpace(useDefaults) != "yes" { if strings.TrimSpace(useDefaults) != "yes" {
fmt.Print("Enter port (default 5000): ") printMessage("Enter port (default 5000): ")
portStr, _ := reader.ReadString('\n') portStr, _ := reader.ReadString('\n')
if portStr != "\n" { if portStr != "\n" {
port, err := strconv.Atoi(strings.TrimSpace(portStr)) port, err := strconv.Atoi(strings.TrimSpace(portStr))
@ -42,7 +41,7 @@ func createConfig() error {
} }
} }
fmt.Print("Enter your domain address (default localhost): ") printMessage("Enter your domain address (default localhost): ")
domain, _ := reader.ReadString('\n') domain, _ := reader.ReadString('\n')
if domain != "\n" { if domain != "\n" {
config.Domain = strings.TrimSpace(domain) config.Domain = strings.TrimSpace(domain)
@ -53,10 +52,13 @@ func createConfig() error {
if config.AuthCode == "" { if config.AuthCode == "" {
config.AuthCode = generateStrongRandomString(64) config.AuthCode = generateStrongRandomString(64)
fmt.Printf("Generated connection code: %s\n", config.AuthCode) printMessage("Generated connection code: %s\n", config.AuthCode)
} }
config.NodesEnabled = len(config.Peers) > 0 config.NodesEnabled = len(config.Peers) > 0
config.CrawlerEnabled = true
config.WebsiteEnabled = true
config.LogLevel = 1
saveConfig(config) saveConfig(config)
return nil return nil
@ -73,17 +75,20 @@ func saveConfig(config Config) {
sec.Key("Peers").SetValue(peers) sec.Key("Peers").SetValue(peers)
sec.Key("Domain").SetValue(config.Domain) sec.Key("Domain").SetValue(config.Domain)
sec.Key("NodesEnabled").SetValue(strconv.FormatBool(config.NodesEnabled)) sec.Key("NodesEnabled").SetValue(strconv.FormatBool(config.NodesEnabled))
sec.Key("CrawlerEnabled").SetValue(strconv.FormatBool(config.CrawlerEnabled))
sec.Key("WebsiteEnabled").SetValue(strconv.FormatBool(config.WebsiteEnabled))
sec.Key("LogLevel").SetValue(strconv.Itoa(config.LogLevel))
err := cfg.SaveTo(configFilePath) err := cfg.SaveTo(configFilePath)
if err != nil { if err != nil {
fmt.Println("Error writing to config file:", err) printErr("Error writing to config file: %v", err)
} }
} }
func loadConfig() Config { func loadConfig() Config {
cfg, err := ini.Load(configFilePath) cfg, err := ini.Load(configFilePath)
if err != nil { if err != nil {
log.Fatalf("Error opening config file: %v", err) printErr("Error opening config file: %v", err)
} }
port, err := cfg.Section("").Key("Port").Int() port, err := cfg.Section("").Key("Port").Int()
@ -110,6 +115,21 @@ func loadConfig() Config {
nodesEnabled = len(peers) > 0 // Enable nodes if peers are configured nodesEnabled = len(peers) > 0 // Enable nodes if peers are configured
} }
crawlerEnabled, err := cfg.Section("").Key("CrawlerEnabled").Bool()
if err != nil { // Default to true if not found
crawlerEnabled = true
}
websiteEnabled, err := cfg.Section("").Key("WebsiteEnabled").Bool()
if err != nil { // Default to true if not found
websiteEnabled = true
}
logLevel, err := cfg.Section("").Key("LogLevel").Int()
if err != nil || logLevel < 0 || logLevel > 4 { // Default to 1 if not found or out of range
logLevel = 1
}
config = Config{ config = Config{
Port: port, Port: port,
AuthCode: cfg.Section("").Key("AuthCode").String(), AuthCode: cfg.Section("").Key("AuthCode").String(),
@ -117,6 +137,9 @@ func loadConfig() Config {
Peers: peers, Peers: peers,
Domain: domain, Domain: domain,
NodesEnabled: nodesEnabled, NodesEnabled: nodesEnabled,
CrawlerEnabled: crawlerEnabled,
WebsiteEnabled: websiteEnabled,
LogLevel: logLevel,
} }
return config return config
@ -127,7 +150,7 @@ var configLock sync.RWMutex
func startFileWatcher() { func startFileWatcher() {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
log.Fatal(err) printErr("%v", err)
} }
go func() { go func() {
@ -139,7 +162,7 @@ func startFileWatcher() {
return return
} }
if event.Op&fsnotify.Write == fsnotify.Write { if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("Modified file:", event.Name) printInfo("Modified file: %v", event.Name)
configLock.Lock() configLock.Lock()
config = loadConfig() config = loadConfig()
configLock.Unlock() configLock.Unlock()
@ -148,7 +171,7 @@ func startFileWatcher() {
if !ok { if !ok {
return return
} }
log.Println("Error watching configuration file:", err) printWarn("Error watching configuration file: %v", err)
} }
} }
}() }()

15
init.go
View file

@ -1,8 +1,6 @@
package main package main
import ( import (
"fmt"
"log"
"time" "time"
) )
@ -13,6 +11,9 @@ type Config struct {
Peers []string Peers []string
Domain string Domain string
NodesEnabled bool NodesEnabled bool
CrawlerEnabled bool
WebsiteEnabled bool
LogLevel int
} }
var defaultConfig = Config{ var defaultConfig = Config{
@ -20,6 +21,10 @@ var defaultConfig = Config{
Domain: "localhost", Domain: "localhost",
Peers: []string{}, Peers: []string{},
AuthCode: generateStrongRandomString(64), AuthCode: generateStrongRandomString(64),
NodesEnabled: true,
CrawlerEnabled: true,
WebsiteEnabled: true,
LogLevel: 1,
} }
const configFilePath = "config.ini" const configFilePath = "config.ini"
@ -29,7 +34,7 @@ var config Config
func main() { func main() {
err := initConfig() err := initConfig()
if err != nil { if err != nil {
fmt.Println("Error during initialization:", err) printErr("Error during initialization:")
return return
} }
@ -39,14 +44,14 @@ func main() {
if config.AuthCode == "" { if config.AuthCode == "" {
config.AuthCode = generateStrongRandomString(64) config.AuthCode = generateStrongRandomString(64)
fmt.Printf("Generated connection code: %s\n", config.AuthCode) printInfo("Generated connection code: %s\n", config.AuthCode)
saveConfig(config) saveConfig(config)
} }
// Generate Host ID // Generate Host ID
hostID, nodeErr := generateHostID() hostID, nodeErr := generateHostID()
if nodeErr != nil { if nodeErr != nil {
log.Fatalf("Failed to generate host ID: %v", nodeErr) printErr("Failed to generate host ID: %v", nodeErr)
} }
config.PeerID = hostID config.PeerID = hostID

View file

@ -104,7 +104,7 @@ func handleNodeRequest(w http.ResponseWriter, r *http.Request) {
} }
defer r.Body.Close() defer r.Body.Close()
log.Printf("Received message: %+v\n", msg) printDebug("Received message: %+v\n", msg)
w.Write([]byte("Message received")) w.Write([]byte("Message received"))
interpretMessage(msg) interpretMessage(msg)
@ -133,9 +133,9 @@ func startNodeClient() {
func interpretMessage(msg Message) { func interpretMessage(msg Message) {
switch msg.Type { switch msg.Type {
case "test": case "test":
fmt.Println("Received test message:", msg.Content) printDebug("Received test message: %v", msg.Content)
case "update": case "update":
fmt.Println("Received update message:", msg.Content) printDebug("Received update message: %v", msg.Content)
go update() go update()
case "heartbeat": case "heartbeat":
handleHeartbeat(msg.Content) handleHeartbeat(msg.Content)
@ -162,6 +162,6 @@ func interpretMessage(msg Message) {
case "file-results": case "file-results":
handleFileResultsMessage(msg) handleFileResultsMessage(msg)
default: default:
fmt.Println("Received unknown message type:", msg.Type) printWarn("Received unknown message type: %v", msg.Type)
} }
} }

View file

@ -36,7 +36,7 @@ func generateOpenSearchXML(config Config) {
file, err := os.Create("static/opensearch.xml") file, err := os.Create("static/opensearch.xml")
if err != nil { if err != nil {
fmt.Println("Error creating OpenSearch file:", err) printErr("Error creating OpenSearch file: %v", err)
return return
} }
defer file.Close() defer file.Close()
@ -44,9 +44,9 @@ func generateOpenSearchXML(config Config) {
enc := xml.NewEncoder(file) enc := xml.NewEncoder(file)
enc.Indent(" ", " ") enc.Indent(" ", " ")
if err := enc.Encode(opensearch); err != nil { if err := enc.Encode(opensearch); err != nil {
fmt.Println("Error encoding OpenSearch XML:", err) printErr("Error encoding OpenSearch XML: %v", err)
return return
} }
fmt.Println("OpenSearch description file generated successfully.") printInfo("OpenSearch description file generated successfully.")
} }

70
printing.go Normal file
View file

@ -0,0 +1,70 @@
package main
import (
"fmt"
"time"
)
// printDebug logs debug-level messages when LogLevel is set to 4.
func printDebug(format string, args ...interface{}) {
if config.LogLevel >= 4 {
logMessage("DEBUG", format, args...)
}
}
// printInfo logs info-level messages when LogLevel is set to 3 or higher.
func printInfo(format string, args ...interface{}) {
if config.LogLevel >= 3 {
logMessage("INFO", format, args...)
}
}
// printWarn logs warning-level messages when LogLevel is set to 2 or higher.
func printWarn(format string, args ...interface{}) {
if config.LogLevel >= 2 {
logMessage("WARN", format, args...)
}
}
// printErr logs error-level messages regardless of LogLevel.
func printErr(format string, args ...interface{}) {
if config.LogLevel >= 1 {
logMessage("ERROR", format, args...)
}
}
// printMessage logs messages without a specific log level (e.g., general output).
func printMessage(format string, args ...interface{}) {
logMessage("", format, args...)
}
// logMessage handles the actual logging logic without using the default logger's timestamp.
func logMessage(level string, format string, args ...interface{}) {
timestamp := time.Now().Format("2006-01-02|15:04:05")
message := fmt.Sprintf(format, args...)
if level != "" {
fmt.Printf("[%s|%s] %s\n", timestamp, level, message)
} else {
fmt.Printf("[%s] %s\n", timestamp, message)
}
}
/////////////
func printErrf(format string, args ...interface{}) {
if config.LogLevel >= 1 {
logMessage("ERROR", fmt.Sprintf(format, args...))
}
}
func printWarnf(format string, args ...interface{}) {
if config.LogLevel >= 2 {
logMessage("WARN", fmt.Sprintf(format, args...))
}
}
func printInfof(format string, args ...interface{}) {
if config.LogLevel >= 3 {
logMessage("INFO", fmt.Sprintf(format, args...))
}
}

View file

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -53,9 +52,7 @@ func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchRe
duration := time.Since(startTime) // Calculate the duration duration := time.Since(startTime) // Calculate the duration
if len(results) == 0 { if len(results) == 0 {
if debugMode { printDebug("No results found from Google Search")
log.Println("No results found from Google")
}
} }
return results, duration, nil return results, duration, nil
@ -83,9 +80,7 @@ func parseResults(doc *goquery.Document) []TextSearchResult {
link := s.Find("a") link := s.Find("a")
href, exists := link.Attr("href") href, exists := link.Attr("href")
if !exists { if !exists {
if debugMode { printDebug("No href attribute found for result %d\n", i)
log.Printf("No href attribute found for result %d\n", i)
}
return return
} }