wolbodge/main.go
2025-03-22 13:19:55 +01:00

59 lines
1.2 KiB
Go

package main
import (
"fmt"
"html/template"
"log"
"net/http"
"time"
"git.1e99.eu/1e99/wolbodge/devices"
"git.1e99.eu/1e99/wolbodge/routes"
"git.1e99.eu/1e99/wolbodge/sessions"
_ "embed"
)
//go:embed index.tmpl
var embed string
func Run() error {
address := ":3000"
tmpl := template.New("")
_, err := tmpl.Parse(embed)
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}
device := devices.NewTest(false)
storage := sessions.NewMemStorage()
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))
mux.Handle("POST /api/session", routes.APIStartSession(storage))
mux.Handle("DELETE /api/session/{session_id}", routes.APIEndSession(storage))
exit := make(chan bool)
go PruneStorage(logger, storage, 10*time.Second, exit)
go DeviceCheck(logger, device, storage.HasSessions, exit)
logger.Printf("Listening on %s", address)
err = http.ListenAndServe(address, mux)
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
return nil
}
func main() {
err := Run()
if err != nil {
log.Fatalf("%s", err)
}
}