A simple HTTP session manager. Got bored and needed to have a session manager that manages idle timeouts as well as longer max timeouts.
Example:
typeSessionEntrystruct {ArbitraryContentstring}sessionManager=NewStore[SessionEntry]("session",time.Duration(IdleTimeDuration)*time.Second)authorisedRoutes:=http.NewServeMux()authorisedRoutes.HandleFunc("/status",status)authorisedRoutes.HandleFunc("/dashboard/",dashboard)log.Fatal(http.ListenAndServe(addr,sessionManager.AuthorisationChecks(authorisedRoutes,nil)))
_,data:=sessionManager.GetSessionFromRequest(r)ifdata==nil {http.Error(w,"No",http.StatusUnauthorized)return}
sessionKey:=sessionManager.StartSession(w,r,currentSession,func(sessionSessionEntry) {// Do something on session expiry})// Do stuffsessionManager.DeleteSession(w,r)