Initial fork of github.com/alonswartz/notesium into librenotes: - Source moved to internal/notesium/ (package notesium) - Thin entry point at cmd/librenotes/main.go - Module renamed to git.librete.ch/public/librenotes - main() exposed as notesium.Run() - LICENSE preserved (MIT), NOTICE added with attribution - Web assets and completion.bash co-located with embedding code to satisfy go:embed path constraints Closes #3, #34, #35. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
52 lines
910 B
Go
52 lines
910 B
Go
package notesium
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
lastHeartbeat time.Time
|
|
)
|
|
|
|
func updateHeartbeat() {
|
|
mu.Lock()
|
|
lastHeartbeat = time.Now()
|
|
mu.Unlock()
|
|
}
|
|
|
|
func heartbeatH(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
updateHeartbeat()
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func heartbeatF(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
updateHeartbeat()
|
|
next(w, r)
|
|
}
|
|
}
|
|
|
|
func checkHeartbeat(server *http.Server) {
|
|
for {
|
|
time.Sleep(5 * time.Second)
|
|
mu.Lock()
|
|
if time.Since(lastHeartbeat) > 10*time.Second {
|
|
fmt.Println("No active client, stopping server.")
|
|
mu.Unlock()
|
|
if err := server.Shutdown(context.Background()); err != nil {
|
|
log.Fatalf("Server shutdown failed: %+v", err)
|
|
}
|
|
break
|
|
}
|
|
mu.Unlock()
|
|
}
|
|
}
|