Quellcode durchsuchen

add new custom config byom.conf

Virgil Ollivier vor 5 Monaten
Ursprung
Commit
7d342abd1c
7 geänderte Dateien mit 94 neuen und 42 gelöschten Zeilen
  1. 4 0
      byom.conf
  2. 66 0
      config/byom.go
  3. 2 1
      config/ovh.go
  4. 3 7
      handlers/dns_handler.go
  5. 13 1
      main.go
  6. 0 28
      routes/dns.go
  7. 6 5
      routes/routes.go

+ 4 - 0
byom.conf

@@ -0,0 +1,4 @@
+foo="bar"
+#database_host=localhost
+#api_key=secret123
+dns_zone=linuxforward.com

+ 66 - 0
config/byom.go

@@ -0,0 +1,66 @@
+package config
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"path/filepath"
+	"strings"
+)
+
+// ByomConfig holds the configuration key-value pairs
+var ByomConfig map[string]string
+
+// InitByomConfig initializes the configuration by reading from a byom.conf file
+func InitByomConfig() error {
+	// Initialize the map
+	ByomConfig = make(map[string]string)
+
+	// Get absolute path to byom.conf in current directory
+	configPath, err := filepath.Abs("./byom.conf")
+	if err != nil {
+		return fmt.Errorf("error getting config path: %v", err)
+	}
+
+	// Open the configuration file
+	file, err := os.Open(configPath)
+	if err != nil {
+		return fmt.Errorf("error opening config file: %v", err)
+	}
+	defer file.Close()
+
+	// Read the file line by line
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		line := strings.TrimSpace(scanner.Text())
+
+		// Skip empty lines and comments
+		if line == "" || strings.HasPrefix(line, "#") {
+			continue
+		}
+
+		// Split the line into key and value
+		parts := strings.SplitN(line, "=", 2)
+		if len(parts) != 2 {
+			continue // Skip invalid lines
+		}
+
+		key := strings.TrimSpace(parts[0])
+		value := strings.TrimSpace(parts[1])
+
+		// Store in the configuration map
+		ByomConfig[key] = value
+	}
+
+	if err := scanner.Err(); err != nil {
+		return fmt.Errorf("error reading config file: %v", err)
+	}
+
+	return nil
+}
+
+// GetConfig returns the value for a given configuration key
+func GetConfig(key string) (string, bool) {
+	value, exists := ByomConfig[key]
+	return value, exists
+}

+ 2 - 1
config/ovh.go

@@ -24,7 +24,8 @@ func InitOVHClient() {
 		"",       // Application secret (will be loaded from config)
 		"",       // Consumer key (will be loaded from config)
 	)
-	
+	log.Printf("client: %v", client)
+
 	if err != nil {
 		log.Fatalf("Error creating OVH client: %v", err)
 	}

+ 3 - 7
handlers/dns_handler.go

@@ -27,13 +27,8 @@ type DNSHandler struct {
 }
 
 func NewDNSHandler() *DNSHandler {
-	// Try to get zone from environment variable first
-	zone := os.Getenv("OVH_DNS_ZONE")
-
-	// Fallback to default if neither is set
-	if zone == "" {
-		zone = "linuxforward.com"
-	}
+	zone, exists := config.GetConfig("dns_zone")
+	log.Printf("DNS Handler initialized - zone = %s (exists: %v)\n", zone, exists)
 
 	return &DNSHandler{
 		Zone: zone,
@@ -160,6 +155,7 @@ func (h *DNSHandler) refreshZone() error {
 // ListDNSRecords handles listing all A records in the DNS zone
 func (h *DNSHandler) ListDNSRecords(w http.ResponseWriter, r *http.Request) {
 	log.Printf("Handling GET request for DNS records from %s", r.RemoteAddr)
+	//log.Printf("Loading DNS Zone : %s", h.Zone)
 
 	var recordIDs []int
 	err := config.OVHClient.Get("/domain/zone/"+h.Zone+"/record", &recordIDs)

+ 13 - 1
main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"byom-infra-api/config"
+	"byom-infra-api/handlers"
 	"byom-infra-api/routes"
 	"log"
 	"net/http"
@@ -13,11 +14,22 @@ func main() {
 	// Initialize the OVH client configuration for API interactions
 	config.InitOVHClient()
 
+	// Initialize the BYOM configuration file
+	err := config.InitByomConfig()
+	if err != nil {
+		log.Fatalf("Failed to initialize config: %v", err)
+	}
+	log.Printf("BYOM Config initialized successfully")
+
+	// Load all handlers from the handlers package
+	log.Printf("Loading handlers...")
+	dnshandler := handlers.NewDNSHandler()
+
 	// Create a new Gorilla Mux router instance for handling HTTP requests
 	r := mux.NewRouter()
 
 	// Configure all application routes and their handlers
-	routes.SetupRoutes(r)
+	routes.SetupRoutes(r, dnshandler)
 	log.Println("Routes configured...")
 
 	// Start the HTTP server on port 8080

+ 0 - 28
routes/dns.go

@@ -1,28 +0,0 @@
-package routes
-
-import (
-	"byom-infra-api/handlers"
-	"net/http"
-)
-
-var dnsHandler = handlers.NewDNSHandler()
-
-// CreateDNSRecord forwards the request to the DNSHandler
-func CreateDNSRecord(w http.ResponseWriter, r *http.Request) {
-	dnsHandler.CreateDNSRecord(w, r)
-}
-
-// DeleteDNSRecord forwards the request to the DNSHandler
-func DeleteDNSRecord(w http.ResponseWriter, r *http.Request) {
-	dnsHandler.DeleteDNSRecord(w, r)
-}
-
-// ListDNSRecords forwards the request to the DNSHandler
-func ListDNSRecords(w http.ResponseWriter, r *http.Request) {
-	dnsHandler.ListDNSRecords(w, r)
-}
-
-// GetDNSRecordID forwards the request to the DNSHandler
-func GetDNSRecordID(w http.ResponseWriter, r *http.Request) {
-	dnsHandler.GetDNSRecordID(w, r)
-}

+ 6 - 5
routes/routes.go

@@ -1,13 +1,14 @@
 package routes
 
 import (
+	"byom-infra-api/handlers"
 	"byom-infra-api/middleware"
 
 	"github.com/gorilla/mux"
 )
 
 // SetupRoutes configures all the routes for the application
-func SetupRoutes(r *mux.Router) {
+func SetupRoutes(r *mux.Router, dnshandler *handlers.DNSHandler) {
 	// Add logging middleware
 	r.Use(middleware.LoggingMiddleware)
 
@@ -18,9 +19,9 @@ func SetupRoutes(r *mux.Router) {
 	r.HandleFunc("/sshkey/{id}", DeleteSSHKey).Methods("DELETE")
 
 	// DNS routes
-	r.HandleFunc("/dns", ListDNSRecords).Methods("GET")
-	r.HandleFunc("/dns/{subdomain}", GetDNSRecordID).Methods("GET")
-	r.HandleFunc("/dns", CreateDNSRecord).Methods("POST")
-	r.HandleFunc("/dns/{id}", DeleteDNSRecord).Methods("DELETE")
+	r.HandleFunc("/dns", dnshandler.ListDNSRecords).Methods("GET")
+	r.HandleFunc("/dns/{subdomain}", dnshandler.GetDNSRecordID).Methods("GET")
+	r.HandleFunc("/dns", dnshandler.CreateDNSRecord).Methods("POST")
+	r.HandleFunc("/dns/{id}", dnshandler.DeleteDNSRecord).Methods("DELETE")
 
 }