package main import ( "fmt" "html/template" "log" "net/http" "git.1e99.eu/1e99/wolbodge/devices" "git.1e99.eu/1e99/wolbodge/routes" _ "embed" ) //go:embed index.tmpl var embed string func Run() error { var address string Env("WOLBODGE_ADDRESS", &address, ":3000") tmpl := template.New("") _, err := tmpl.Parse(embed) if err != nil { return fmt.Errorf("failed to parse template: %w", err) } device, err := NewDevice() if err != nil { return fmt.Errorf("failed to create device: %w", err) } storage, err := NewStorage() if err != nil { return fmt.Errorf("failed to create session storage: %w", err) } logger := log.Default() mux := http.NewServeMux() logger.Printf("Using device: %s", device.String()) logger.Printf("Using session storage: %s", storage.String()) 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) 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) } }