mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
restructure project
This commit is contained in:
parent
dd2db438f3
commit
2ca35d4461
46 changed files with 158 additions and 84 deletions
10
internal/session/cookie.go
Normal file
10
internal/session/cookie.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetCookie(value string, expires time.Time) *http.Cookie {
|
||||
return &http.Cookie{Name: SessionCookieName, HttpOnly: true, SameSite: http.SameSiteStrictMode, Value: value, Expires: expires}
|
||||
}
|
93
internal/session/session.go
Normal file
93
internal/session/session.go
Normal file
|
@ -0,0 +1,93 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type SessionKeyType string
|
||||
|
||||
const SessionKey SessionKeyType = "session"
|
||||
const SessionCookieName string = "session"
|
||||
|
||||
type Session struct {
|
||||
Username string
|
||||
Expires time.Time
|
||||
// TODO Add []websocket.Conn pointer to close all active websockets, alternativly do this via context cancelation
|
||||
}
|
||||
|
||||
type SessionResponse struct {
|
||||
CommitHash string `json:"commit_hash"`
|
||||
}
|
||||
|
||||
var sessionsSync sync.Mutex
|
||||
var sessions map[string]*Session = map[string]*Session{}
|
||||
|
||||
var CommitHash = func() string {
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
for _, setting := range info.Settings {
|
||||
if setting.Key == "vcs.revision" {
|
||||
return setting.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
return "asd"
|
||||
}()
|
||||
|
||||
func ExtendSession(s *Session) {
|
||||
sessionsSync.Lock()
|
||||
defer sessionsSync.Unlock()
|
||||
if s != nil {
|
||||
s.Expires = time.Now().Add(time.Minute * 5)
|
||||
}
|
||||
}
|
||||
|
||||
func GetSession(r *http.Request) (string, *Session) {
|
||||
c, err := r.Cookie("session")
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
s, ok := sessions[c.Value]
|
||||
if ok {
|
||||
return c.Value, s
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func GenerateSession(w http.ResponseWriter, username string) {
|
||||
id := uuid.New().String()
|
||||
expires := time.Now().Add(time.Minute * 5)
|
||||
sessionsSync.Lock()
|
||||
defer sessionsSync.Unlock()
|
||||
sessions[id] = &Session{
|
||||
Username: username,
|
||||
Expires: expires,
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: SessionCookieName, HttpOnly: true, SameSite: http.SameSiteStrictMode, Value: id, Expires: expires})
|
||||
}
|
||||
|
||||
func CleanupSessions(stop chan struct{}) {
|
||||
tick := time.NewTicker(time.Minute)
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
ids := []string{}
|
||||
sessionsSync.Lock()
|
||||
for id, s := range sessions {
|
||||
if time.Now().After(s.Expires) {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
for _, id := range ids {
|
||||
delete(sessions, id)
|
||||
}
|
||||
sessionsSync.Unlock()
|
||||
case <-stop:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue