66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"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 new device: %w", err)
|
|
}
|
|
|
|
storage, err := NewStorage()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create new 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))
|
|
|
|
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)
|
|
}
|
|
}
|