Files
gogwapi/app/main.go
2025-11-29 03:09:40 +01:00

63 lines
1.8 KiB
Go

package main
import (
"fmt"
"net/http"
"git.secnex.io/secnex/gogwapi/config"
"git.secnex.io/secnex/gogwapi/database"
"git.secnex.io/secnex/gogwapi/models"
"git.secnex.io/secnex/gogwapi/proxy"
"git.secnex.io/secnex/masterlog"
)
func main() {
config, err := config.LoadYAMLConfig("../config.yaml")
if err != nil {
masterlog.Error("failed to load config", map[string]interface{}{"error": err.Error()})
return
}
targets := config.Targets
masterlog.Info("Targets loaded", map[string]interface{}{"count": len(targets)})
for _, target := range targets {
targetDescription := target.Name
if target.Group != nil {
targetDescription = fmt.Sprintf("[%s] %s", *target.Group, target.Name)
}
masterlog.Info(targetDescription, map[string]interface{}{"target": target.Name, "group": *target.Group, "records": len(target.Records)})
}
allModels := []interface{}{
&models.Endpoint{},
}
dbConfig := database.NewDatabaseConfigurationFromEnvAndConfig(config)
masterlog.Info("Connecting to database", map[string]interface{}{"host": dbConfig.Host, "port": dbConfig.Port, "database": dbConfig.Database})
if err := dbConfig.Connect(allModels...); err != nil {
masterlog.Error("failed to connect to database", map[string]interface{}{"error": err.Error()})
return
}
masterlog.Info("Connected to database!")
// Initialize reverse proxy
reverseProxy := proxy.NewReverseProxy(config)
// Setup HTTP server
mux := http.NewServeMux()
mux.HandleFunc("/", reverseProxy.ServeHTTP)
// Start server
addr := fmt.Sprintf(":%d", config.Server.Port)
masterlog.Info("Starting reverse proxy server", map[string]interface{}{
"port": config.Server.Port,
"address": addr,
})
if err := http.ListenAndServe(addr, mux); err != nil {
masterlog.Error("server failed to start", map[string]interface{}{"error": err.Error()})
return
}
}