Skip to content

Commit

Permalink
api: olsr: add aredn hosts count api
Browse files Browse the repository at this point in the history
  • Loading branch information
USA-RedDragon committed May 1, 2024
1 parent 7fafa6c commit e55501d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
18 changes: 14 additions & 4 deletions internal/olsrd/hosts_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"net"
"net/url"
"os"
"regexp"
"strings"
"sync/atomic"
)

const hostsFile = "/var/run/hosts_olsr"

type HostsParser struct {
currentHosts []*AREDNHost
isParsing atomic.Bool
currentHosts []*AREDNHost
arednNodesCount int
isParsing atomic.Bool
}

func NewHostsParser() *HostsParser {
Expand All @@ -28,6 +30,10 @@ func (p *HostsParser) GetHostsCount() int {
return len(p.currentHosts)
}

func (p *HostsParser) GetAREDNHostsCount() int {
return p.arednNodesCount
}

func (p *HostsParser) GetHostsPaginated(page int, limit int, filter string) []*AREDNHost {
ret := []*AREDNHost{}
for _, host := range p.currentHosts {
Expand All @@ -54,10 +60,11 @@ func (p *HostsParser) Parse() (err error) {
}
p.isParsing.Store(true)
defer p.isParsing.Store(false)
hosts, err := parseHosts()
hosts, arednCount, err := parseHosts()
if err != nil {
return
}
p.arednNodesCount = arednCount
p.currentHosts = hosts
return
}
Expand Down Expand Up @@ -96,7 +103,7 @@ func (h *AREDNHost) String() string {
// Lines with only whitespace or that are empty are ignored
//
//nolint:golint,gocyclo
func parseHosts() (ret []*AREDNHost, err error) {
func parseHosts() (ret []*AREDNHost, arednCount int, err error) {
hostsFile, err := os.ReadFile(hostsFile)
if err != nil {
return
Expand Down Expand Up @@ -147,6 +154,9 @@ func parseHosts() (ret []*AREDNHost, err error) {
}

if strings.Contains(split[1], ".") {
if regexp.MustCompile(`mid\d+\.`).MatchString(split[1]) {
arednCount++
}
continue
}

Expand Down
11 changes: 11 additions & 0 deletions internal/server/api/controllers/v1/olsr.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ func GETOLSRHosts(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"nodes": nodes, "total": total})
}

func GETOLSRHostsCount(c *gin.Context) {
olsrdParser, ok := c.MustGet("OLSRDHostParser").(*olsrd.HostsParser)
if !ok {
fmt.Println("POSTLogin: OLSRDHostParser not found in context")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Try again later"})
return
}

c.JSON(http.StatusOK, gin.H{"nodes": olsrdParser.GetAREDNHostsCount(), "total": olsrdParser.GetHostsCount()})
}

func GETOLSRRunning(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"running": olsrd.IsRunning()})
}
1 change: 1 addition & 0 deletions internal/server/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func v1(group *gin.RouterGroup, config *config.Config) {

v1OLSR := group.Group("/olsr")
v1OLSR.GET("/hosts", v1Controllers.GETOLSRHosts)
v1OLSR.GET("/hosts/count", v1Controllers.GETOLSRHostsCount)
v1OLSR.GET("/running", v1Controllers.GETOLSRRunning)

v1VTun := group.Group("/vtun")
Expand Down

0 comments on commit e55501d

Please sign in to comment.