Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked webui reader #156

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/conf/config.yaml
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what's going on here.

Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ output:
password: secret # mysql database user password
database: birdnet # mysql database name
host: localhost # mysql database host
port: 3306 # mysql database port
port: 3306 # mysql database port
29 changes: 29 additions & 0 deletions internal/httpcontroller/fileserver.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package httpcontroller

import (
"bufio"
"io/fs"
"mime"
"net/http"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -34,3 +36,30 @@ func customFileServer(e *echo.Echo, fileSystem fs.FS, root string) {
fileServer.ServeHTTP(w, r)
})))
}

// readWebLog reads the content of the web.log file from the root directory and returns it as a string.
// It returns an error if there is an issue opening or reading the file.
func readWebLog() (string, error) {
// Open the web.log file
file, err := os.Open("webui.log")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making the file path for webui.log configurable to enhance flexibility and maintainability.

if err != nil {
return "", err
}
defer file.Close()

var content string
scanner := bufio.NewScanner(file)

// Read the file line by line and append each line to the content string
for scanner.Scan() {
content += scanner.Text() + "\n"
}

// Check if there was an error during scanning
if err := scanner.Err(); err != nil {
return "", err
}

// Return the content of the file
return content, nil
}
16 changes: 16 additions & 0 deletions internal/httpcontroller/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,19 @@ func (s *Server) serveSpectrogramHandler(c echo.Context) error {
// Serve the spectrogram image file
return c.File(spectrogramPath)
}

// getLogsHandler handles GET requests to the /logs endpoint.
// It reads the content of the webui.log file and renders the logs view with the content.
func (s *Server) getLogsHandler(c echo.Context) error {
// Read the content of web.log
logContent, err := readWebLog()
if err != nil {
// Return an HTTP error if there is an issue reading the file
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read web.log: "+err.Error())
}

// Render the logs view and pass the logContent as data
return c.Render(http.StatusOK, "logs", map[string]interface{}{
"LogContent": logContent,
})
}
2 changes: 1 addition & 1 deletion internal/httpcontroller/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (s *Server) initRoutes() {

// Other static routes.
s.Echo.Static("/clips", "clips")
s.Echo.GET("/logs", s.getLogsHandler)

// Additional handlers.
s.Echo.GET("/top-birds", s.topBirdsHandler)
Expand All @@ -75,7 +76,6 @@ func (s *Server) initRoutes() {
s.Echo.GET("/search", s.searchHandler)
s.Echo.GET("/spectrogram", s.serveSpectrogramHandler)


// Handle both GET and DELETE requests for the /note route
s.Echo.Add("GET", "/note", s.getNoteHandler)
s.Echo.Add("DELETE", "/note", s.deleteNoteHandler)
Expand Down
56 changes: 54 additions & 2 deletions views/logs.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
{{define "logs"}}

<span class="col-span-12">Nothing here yet ..</span>
<!doctype html>
<html lang="en">

{{end}}
<head>
<meta charset="utf-8" />
<title>BirdNET-Go Logs</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="/assets/tailwind.css" rel="stylesheet" />
<link href="/assets/custom.css" rel="stylesheet" />
<!-- htmx -->
<script src="/assets/htmx.min.js" defer></script>
<!-- alpine.js -->
<script src="/assets/alpinejs.min.js" defer></script>
</head>

<body class="drawer min-h-screen bg-base-200 lg:drawer-open">
<input id="my-drawer" type="checkbox" class="drawer-toggle" />

<div class="drawer-content">
<div class="grid grid-cols-12 grid-rows-[min-content] gap-y-0 p-3 lg:pt-4 lg:pr-8 lg:pl-8 lg:pb-0">
{{ template "header" . }}
</div>

<!-- content -->
<main>
<div id="mainContent" class="grid grid-cols-12 grid-rows-[min-content] gap-y-8 p-3 lg:p-8" >
<span class="col-span-12">
{{if .LogContent}}
<pre>{{.LogContent}}</pre>
{{else}}
It didn't work
{{end}}
</span>
</div>
<!-- Placeholder for dynamic notifications -->
<div id="status-message"></div>
</main>
</div>

{{ template "sidebar" . }}

<script>
// Set the date picker to today's date
setTimeout(function () {
var datePicker = document.getElementById('datePicker');
var today = new Date().toLocaleString('sv').split(' ')[0];
datePicker.value = today;
}, 0); // Set timeout for 0ms

</script>
</body>

</html>

{{end}}
Loading