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:]...) return exec.Command(args[0], args[1:]...)
} }
func (d *command) String() string {
return "command"
}

View file

@ -1,6 +1,7 @@
package devices package devices
import ( import (
"fmt"
"log" "log"
"time" "time"
) )
@ -13,6 +14,8 @@ type Device interface {
// Toggle the status of the device. // Toggle the status of the device.
// This is usually achieved by pressing the power button. // This is usually achieved by pressing the power button.
ToggleStatus() error ToggleStatus() error
fmt.Stringer
} }
func KeepDeviceInSync(logger *log.Logger, device Device, wantStatus func() (bool, error), exit <-chan bool) { 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 d.status = !d.status
return nil 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) raw := os.Getenv(name)
if raw == "" { if raw == "" {
raw = def 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) { switch value := out.(type) {
case *int: case *int:
i, err := strconv.ParseInt(raw, 10, 64) i, err := strconv.ParseInt(raw, 10, 64)
if err != nil { if err != nil {
log.Printf("\"%s\" is not a number (\"%s\").", name, raw) log.Printf("\"%s\" is not a number (\"%s\")", name, raw)
return return
} }

17
main.go
View file

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

View file

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

View file

@ -2,6 +2,7 @@ package routes
import ( import (
"html/template" "html/template"
"log"
"net/http" "net/http"
"strings" "strings"
"time" "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) { return func(res http.ResponseWriter, req *http.Request) {
err := req.ParseMultipartForm(10 * 1024) err := req.ParseMultipartForm(10 * 1024)
if err != nil { if err != nil {
@ -42,17 +43,18 @@ func StartSession(storage sessions.Storage) http.HandlerFunc {
description = "unknown" description = "unknown"
} }
_, err = storage.StartSession(description, time.Now()) session, err := storage.StartSession(description, time.Now())
if err != nil { if err != nil {
http.Error(res, "", http.StatusInternalServerError) http.Error(res, "", http.StatusInternalServerError)
return return
} }
http.Redirect(res, req, "/", http.StatusFound) 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) { return func(res http.ResponseWriter, req *http.Request) {
err := req.ParseMultipartForm(10 * 1024) err := req.ParseMultipartForm(10 * 1024)
if err != nil { if err != nil {
@ -72,5 +74,6 @@ func EndSession(storage sessions.Storage) http.HandlerFunc {
} }
http.Redirect(res, req, "/", http.StatusFound) 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) delete(s.sessions, id)
return nil return nil
} }
func (s *memStorage) String() string {
return "mem"
}

View file

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