forked from abh/bgpapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
103 lines (83 loc) · 2.37 KB
/
http.go
File metadata and controls
103 lines (83 loc) · 2.37 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"html/template"
"net/http"
"strconv"
)
type homePageData struct {
Title string
Neighbors Neighbors
Data map[string]string
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("templates/home.html", "templates/header.html", "templates/footer.html")
if err != nil {
fmt.Println("Could not parse templates", err)
fmt.Fprintln(w, "Problem parsing templates", err)
return
}
data := new(homePageData)
data.Title = "BGP Status"
data.Neighbors = neighbors
// fmt.Fprintf(w, "%s\t%s\t%v\n", neighbor, data.State, data.Updates)
// fmt.Printf("TMPL %s %#v\n", tmpl, tmpl)
tmpl.Execute(w, data)
}
func StatusHandler(w http.ResponseWriter, r *http.Request) {
for neighbor, data := range neighbors {
fmt.Fprintf(w, "%s\t%s\t%v\n", neighbor, data.State, data.Updates)
}
}
func ApiHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/json")
vars := mux.Vars(r)
neighbor_ip := vars["neighbor"]
neighbor, ok := neighbors[neighbor_ip]
if !ok {
w.WriteHeader(404)
return
}
// fmt.Printf("VARS: %#v\n", vars)
switch vars["method"] {
case "asn":
asn, err := strconv.Atoi(vars["id"])
if err != nil {
fmt.Fprintln(w, "Could not parse AS number")
return
}
prefixes := neighbor.AsnPrefix[ASN(asn)]
strPrefixes := make([]string, 0)
for prefix, _ := range prefixes {
strPrefixes = append(strPrefixes, prefix)
}
json, err := json.Marshal(map[string][]string{"prefixes": strPrefixes})
// json, err := json.Marshal(prefixes)
fmt.Fprint(w, string(json))
case "ip":
fmt.Fprintf(w, "/api/ip/%s not implemented\n", vars["id"])
return
case "prefixes":
prefixes := neighbor.PrefixAsn
json, err := json.Marshal(map[string]Prefixes{"prefixes": prefixes})
if err != nil {
fmt.Fprint(w, "error generating json", err)
}
fmt.Fprint(w, string(json))
default:
w.WriteHeader(404)
}
fmt.Fprint(w, "\n")
}
func httpServer() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/api/{neighbor:[0-9.:]+}/{method:asn}/{id:[0-9]+}", ApiHandler)
r.HandleFunc("/api/{neighbor:[0-9.:]+}/{method:ip}/{id:[0-9.:]+}", ApiHandler)
r.HandleFunc("/api/{neighbor:[0-9.:]+}/{method:prefixes}", ApiHandler)
r.HandleFunc("/status", StatusHandler)
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}