improve logging

This commit is contained in:
1e99 2025-03-22 15:42:01 +01:00
parent 2e0e50dca5
commit 03bc0d85c4
9 changed files with 42 additions and 14 deletions

View file

@ -92,3 +92,7 @@ func (d *command) makeCommand(command string) *exec.Cmd {
return exec.Command(args[0], args[1:]...)
}
func (d *command) String() string {
return "command"
}

View file

@ -1,6 +1,7 @@
package devices
import (
"fmt"
"log"
"time"
)
@ -13,6 +14,8 @@ type Device interface {
// Toggle the status of the device.
// This is usually achieved by pressing the power button.
ToggleStatus() error
fmt.Stringer
}
func KeepDeviceInSync(logger *log.Logger, device Device, wantStatus func() (bool, error), exit <-chan bool) {

View file

@ -29,3 +29,7 @@ func (d *test) ToggleStatus() error {
d.status = !d.status
return nil
}
func (d *test) String() string {
return "test"
}

4
env.go
View file

@ -51,14 +51,14 @@ func Env(name string, out any, def string) {
raw := os.Getenv(name)
if raw == "" {
raw = def
log.Printf("No \"%s\" provided, defaulting to \"%s\".", name, def)
log.Printf("No \"%s\" provided, defaulting to \"%s\"", name, def)
}
switch value := out.(type) {
case *int:
i, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
log.Printf("\"%s\" is not a number (\"%s\").", name, raw)
log.Printf("\"%s\" is not a number (\"%s\")", name, raw)
return
}

17
main.go
View file

@ -27,23 +27,26 @@ func Run() error {
device, err := NewDevice()
if err != nil {
return fmt.Errorf("failed to create new device: %w", err)
return fmt.Errorf("failed to create device: %w", err)
}
storage, err := NewStorage()
if err != nil {
return fmt.Errorf("failed to create new storage: %w", err)
return fmt.Errorf("failed to create session storage: %w", err)
}
logger := log.Default()
mux := http.NewServeMux()
mux.Handle("GET /", routes.GetIndex(device, storage, tmpl))
mux.Handle("POST /start_session", routes.StartSession(storage))
mux.Handle("POST /end_session", routes.EndSession(storage))
logger.Printf("Using device: %s", device.String())
logger.Printf("Using session storage: %s", storage.String())
mux.Handle("POST /api/session", routes.APIStartSession(storage))
mux.Handle("DELETE /api/session/{session_id}", routes.APIEndSession(storage))
mux.Handle("GET /", routes.GetIndex(device, storage, tmpl))
mux.Handle("POST /start_session", routes.StartSession(logger, storage))
mux.Handle("POST /end_session", routes.EndSession(logger, storage))
mux.Handle("POST /api/session", routes.APIStartSession(logger, storage))
mux.Handle("DELETE /api/session/{session_id}", routes.APIEndSession(logger, storage))
exit := make(chan bool)
go devices.KeepDeviceInSync(logger, device, storage.HasSessions, exit)

View file

@ -2,6 +2,7 @@ package routes
import (
"fmt"
"log"
"net/http"
"strings"
"time"
@ -9,7 +10,7 @@ import (
"git.1e99.eu/1e99/wolbodge/sessions"
)
func APIStartSession(storage sessions.Storage) http.HandlerFunc {
func APIStartSession(logger *log.Logger, storage sessions.Storage) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
err := req.ParseMultipartForm(10 * 1024)
if err != nil {
@ -29,10 +30,11 @@ func APIStartSession(storage sessions.Storage) http.HandlerFunc {
}
fmt.Fprintf(res, "%s", session.Id)
logger.Printf("Started new session for %s via API (id = %s, description = \"%s\")", req.RemoteAddr, session.Id, session.Description)
}
}
func APIEndSession(storage sessions.Storage) http.HandlerFunc {
func APIEndSession(logger *log.Logger, storage sessions.Storage) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
sessionId := req.PathValue("session_id")
@ -45,5 +47,7 @@ func APIEndSession(storage sessions.Storage) http.HandlerFunc {
http.Error(res, "", http.StatusInternalServerError)
return
}
logger.Printf("Ended session for %s via API (id = %s)", req.RemoteAddr, sessionId)
}
}

View file

@ -2,6 +2,7 @@ package routes
import (
"html/template"
"log"
"net/http"
"strings"
"time"
@ -29,7 +30,7 @@ func GetIndex(device devices.Device, storage sessions.Storage, tmpl *template.Te
}
}
func StartSession(storage sessions.Storage) http.HandlerFunc {
func StartSession(logger *log.Logger, storage sessions.Storage) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
err := req.ParseMultipartForm(10 * 1024)
if err != nil {
@ -42,17 +43,18 @@ func StartSession(storage sessions.Storage) http.HandlerFunc {
description = "unknown"
}
_, err = storage.StartSession(description, time.Now())
session, err := storage.StartSession(description, time.Now())
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
http.Redirect(res, req, "/", http.StatusFound)
logger.Printf("Started new session for %s via webpanel (id = %s, description = \"%s\")", req.RemoteAddr, session.Id, session.Description)
}
}
func EndSession(storage sessions.Storage) http.HandlerFunc {
func EndSession(logger *log.Logger, storage sessions.Storage) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
err := req.ParseMultipartForm(10 * 1024)
if err != nil {
@ -72,5 +74,6 @@ func EndSession(storage sessions.Storage) http.HandlerFunc {
}
http.Redirect(res, req, "/", http.StatusFound)
logger.Printf("Ended session for %s via webpanel (id = %s)", req.RemoteAddr, sessionId)
}
}

View file

@ -72,3 +72,7 @@ func (s *memStorage) EndSession(id string) error {
delete(s.sessions, id)
return nil
}
func (s *memStorage) String() string {
return "mem"
}

View file

@ -2,6 +2,7 @@ package sessions
import (
"errors"
"fmt"
"time"
)
@ -32,4 +33,6 @@ type Storage interface {
// End an old session.
// Should return [ErrUnknownSession] if the session couldn't be found.
EndSession(id string) error
fmt.Stringer
}