topology-map/main.go

114 lines
2.5 KiB
Go
Raw Normal View History

2024-09-17 16:39:53 +00:00
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
2024-09-22 08:27:05 +00:00
"git.faercol.me/faercol/topology-map/internal/cache"
"git.faercol.me/faercol/topology-map/internal/models"
"git.faercol.me/faercol/topology-map/internal/netbox"
2024-09-17 16:39:53 +00:00
)
const apiKey = "4e75b8927940adc29e2e1eac042bf92bcddd57fe"
const netboxBaseURL = "https://netbox.internal.faercol.me/api/"
2024-09-22 08:27:05 +00:00
func main() {
2024-09-17 16:39:53 +00:00
2024-09-22 08:27:05 +00:00
fmt.Println("Initializing data")
dataCache := cache.NewDataCache()
netboxClt := netbox.NewClient(netboxBaseURL, apiKey)
2024-09-17 16:39:53 +00:00
2024-09-22 08:27:05 +00:00
fmt.Println("Getting devices")
devices, err := netboxClt.GetDevices()
2024-09-17 16:39:53 +00:00
if err != nil {
2024-09-22 08:27:05 +00:00
panic(err)
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
for _, d := range devices {
dataCache.AddDevice(d)
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
fmt.Println("Getting VMs")
vms, err := netboxClt.GetVMs()
2024-09-17 16:39:53 +00:00
if err != nil {
2024-09-22 08:27:05 +00:00
panic(err)
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
for _, v := range vms {
dataCache.AddVM(v)
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
fmt.Println("Getting cables")
cables, err := netboxClt.GetCables()
2024-09-17 16:39:53 +00:00
if err != nil {
2024-09-22 08:27:05 +00:00
panic(err)
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
for _, c := range cables {
dataCache.AddCable(c)
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
fmt.Println("Getting interfaces")
interfaces, err := netboxClt.GetInterfaces()
2024-09-17 16:39:53 +00:00
if err != nil {
2024-09-22 08:27:05 +00:00
panic(err)
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
for _, i := range interfaces {
dataCache.AddInterface(i)
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
fmt.Println("Reconciling data")
dataCache.ReconcileData()
fmt.Println("Done")
2024-09-17 16:39:53 +00:00
srv := http.NewServeMux()
srv.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Serving static file")
fs := http.FileServer(http.Dir("./static"))
http.StripPrefix("/static", fs).ServeHTTP(w, r)
})
srv.HandleFunc("/api/data", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Serving API route")
2024-09-22 08:27:05 +00:00
resp := []models.Element{}
for _, d := range dataCache.GetDevices() {
resp = append(resp, d.Element())
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
for _, c := range dataCache.GetCables() {
resp = append(resp, c.Element())
2024-09-17 16:39:53 +00:00
}
2024-09-22 08:27:05 +00:00
for _, v := range dataCache.GetVMs() {
resp = append(resp, v.Elements()...)
2024-09-17 16:39:53 +00:00
}
respBody, err := json.Marshal(resp)
if err != nil {
fmt.Printf("Failed to serialize data: %s\n", err)
w.WriteHeader(500)
return
}
2024-09-22 08:27:05 +00:00
w.Header().Add("Content-Type", "application/json")
2024-09-17 16:39:53 +00:00
w.Write(respBody)
})
srv.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Serving main root")
tpl, err := template.New("index.html").ParseFiles("templates/index.html")
if err != nil {
fmt.Printf("Failed to read template: %s\n", err)
w.WriteHeader(500)
return
}
if err := tpl.Execute(w, nil); err != nil {
fmt.Printf("Failed to execute template: %s\n", err)
w.WriteHeader(500)
return
}
})
2024-09-22 08:27:05 +00:00
if err := http.ListenAndServe("0.0.0.0:5000", srv); err != nil {
2024-09-17 16:39:53 +00:00
panic(err)
}
}