1
0
Fork 0
mirror of https://github.com/alice-lg/birdwatcher.git synced 2025-03-09 00:00:05 +01:00

added first endpoints

This commit is contained in:
hellerve 2016-11-10 16:06:38 +01:00
parent 4ce81124a1
commit 71350f616d
5 changed files with 36 additions and 15 deletions

View file

@ -6,7 +6,7 @@ import (
)
func Run(args string) ([]byte, error) {
args = "show" + args
args = "show " + args
argsList := strings.Split(args, " ")
return exec.Command("birdc", argsList...).Output()
}

View file

@ -8,14 +8,22 @@ import (
type Parsed map[string]interface{}
func getLines(input []byte) []string {
func emptyLine(line string) bool {
return len(strings.TrimSpace(line)) == 0
}
func getLinesUnfiltered(input []byte) []string {
line_sep := regexp.MustCompile(`((\r?\n)|(\r\n?))`)
lines := line_sep.Split(string(input), -1)
return line_sep.Split(string(input), -1)
}
func getLines(input []byte) []string {
lines := getLinesUnfiltered(input)
var filtered []string
for _, line := range lines {
if len(strings.TrimSpace(line)) > 0 {
if !emptyLine(line) {
filtered = append(filtered, line)
}
}
@ -37,6 +45,7 @@ func parseStatus(input []byte) Parsed {
current_server_rx := regexp.MustCompile(`^Current\sserver\stime\sis\s([0-9\-]+)\s([0-9\:]+)\s*$`)
last_reboot_rx := regexp.MustCompile(`^Last\sreboot\son\s([0-9\-]+)\s([0-9\:]+)\s*$`)
last_reconfig_rx := regexp.MustCompile(`^Last\sreconfiguration\son\s([0-9\-]+)\s([0-9\:]+)\s*$`)
for _, line := range lines {
if start_line_rx.MatchString(line) {
res["version"] = start_line_rx.FindStringSubmatch(line)[1]
@ -56,7 +65,24 @@ func parseStatus(input []byte) Parsed {
}
func parseProtocols(input []byte) Parsed {
return Parsed{}
res := Parsed{}
protocols := []string{}
lines := getLinesUnfiltered(input)
proto := ""
for _, line := range lines {
if emptyLine(line) {
if !emptyLine(proto) {
protocols = append(protocols, proto)
}
proto = ""
} else {
proto += (line + "\n")
}
}
res["protocols"] = protocols
return res
}
func parseSymbols(input []byte) Parsed {

View file

@ -116,5 +116,5 @@ func main() {
r.GET("/routes", Routes)
r.GET("/protocols", Protocols)
log.Fatal(http.ListenAndServe(":8080", r))
log.Fatal(http.ListenAndServe(":29184", r))
}

View file

@ -5,6 +5,7 @@ import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/mchackorg/birdwatcher/bird"
)
func Protocols(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@ -12,13 +13,7 @@ func Protocols(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
res["api"] = GetApiInfo()
lines, err := readLines(conf.Conf.FileName)
if err != nil {
slog.Err("Couldn't find file: " + conf.Conf.FileName)
return
}
res["protocols"] = pattern("getprotocol", lines)
res["protocols"] = bird.Protocols()["protocols"]
js, _ := json.Marshal(res)

View file

@ -3,8 +3,8 @@ package main
import (
"encoding/json"
"github.com/julienschmidt/httprouter"
"github.com/mchackorg/birdwatcher/bird"
"net/http"
"github.com/mchackorg/birdwatcher/bird"
)
func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@ -12,7 +12,7 @@ func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
res["api"] = GetApiInfo()
res["status"] = bird.Status()
res["status"] = bird.Status()
js, _ := json.Marshal(res)