Skip to content

Commit

Permalink
API: status
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Oct 20, 2024
1 parent 9132401 commit 1e1f337
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.

## [v2.0.4] - 2024-
### Added
- Notification test [#147](https://github.com/aceberg/WatchYourLAN/issues/147)
- API status [#148](https://github.com/aceberg/WatchYourLAN/issues/148)

### Fixed
- [#101](https://github.com/aceberg/WatchYourLAN/issues/101)
- The same problem for Theme, Color mode, Log level
Expand Down
14 changes: 13 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ Edit host with ID `id`. Can change `name`. `known` is optional, when set to `tog
```http
GET /api/host/del/:id
```
Remove host with ID `id`.
Remove host with ID `id`.


```http
GET /api/notify_test
```
Send test notification.


```http
GET /api/status/*iface
```
Show status (Total number of hosts, online/offline, known/unknown). The `iface` parameter is optional and shows status for one interface only. For all interfaces just call `/api/status/`.
9 changes: 9 additions & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ type Host struct {
Now int `db:"NOW"`
}

// Stat - status
type Stat struct {
Total int
Online int
Offline int
Known int
Unknown int
}

// GuiData - all data sent to html page
type GuiData struct {
Config Conf
Expand Down
39 changes: 37 additions & 2 deletions internal/web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,45 @@ func apiEdit(c *gin.Context) {
c.IndentedJSON(http.StatusOK, "OK")
}

func testNotifyHandler(c *gin.Context) {
func apiNotifyTest(c *gin.Context) {

msg := "Test notification from WatchYourLAN"
msg := "WatchYourLAN: test notification"
notify.Shout(msg, appConfig.ShoutURL)

c.Status(http.StatusOK)
}

func apiStatus(c *gin.Context) {
var status models.Stat
var searchHosts []models.Host

iface := c.Param("iface")
iface = iface[1:]

if iface != "" {
for _, host := range allHosts {
if iface == host.Iface {
searchHosts = append(searchHosts, host)
}
}
} else {
searchHosts = allHosts
}

for _, host := range searchHosts {
status.Total = status.Total + 1

if host.Known > 0 {
status.Known = status.Known + 1
} else {
status.Unknown = status.Unknown + 1
}
if host.Now > 0 {
status.Online = status.Online + 1
} else {
status.Offline = status.Offline + 1
}
}

c.IndentedJSON(http.StatusOK, status)
}
4 changes: 2 additions & 2 deletions internal/web/public/js/notify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
async function testNotifications() {

const url = '/api/test_notify'
await fetch(url, { method: 'post' })
const url = '/api/notify_test';
await fetch(url);
}
2 changes: 1 addition & 1 deletion internal/web/scan-routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func compareHosts(foundHosts []models.Host) {

fHost.Name, fHost.DNS = updateDNS(fHost)

msg := fmt.Sprintf("Unknown host Names: '%s', IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.DNS, fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface)
msg := fmt.Sprintf("WatchYourLAN: unknown host found. Names: '%s', IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.DNS, fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface)
slog.Warn(msg)
notify.Shout(msg, appConfig.ShoutURL) // Notify through Shoutrrr

Expand Down
2 changes: 1 addition & 1 deletion internal/web/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</tr>
<tr>
<td><button type="submit" class="btn btn-primary">Save</button></td>
<td><button type="button" style="float: right;" class="btn btn-success" onclick="testNotifications()">Test notifications</button></td>
<td><button type="button" style="float: right;" class="btn btn-info" onclick="testNotifications()">Test notification</button></td>
<td></td>
</tr>
</form>
Expand Down
15 changes: 8 additions & 7 deletions internal/web/webgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ func Gui(dirPath, nodePath string) {

router.StaticFS("/fs/", http.FS(pubFS)) // public

router.GET("/api/all", apiAll) // api.go
router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go
router.GET("/api/history/*mac", apiHistory) // api.go
router.GET("/api/host/:id", apiHost) // api.go
router.GET("/api/host/del/:id", apiHostDel) // api.go
router.GET("/api/port/:addr/:port", apiPort) // api.go
router.POST("/api/test_notify", testNotifyHandler) // api.go
router.GET("/api/all", apiAll) // api.go
router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go
router.GET("/api/history/*mac", apiHistory) // api.go
router.GET("/api/host/:id", apiHost) // api.go
router.GET("/api/host/del/:id", apiHostDel) // api.go
router.GET("/api/notify_test", apiNotifyTest) // api.go
router.GET("/api/port/:addr/:port", apiPort) // api.go
router.GET("/api/status/*iface", apiStatus) // api.go

router.GET("/", indexHandler) // index.go
router.GET("/history/", historyHandler) // index.go
Expand Down

0 comments on commit 1e1f337

Please sign in to comment.