Skip to content

Commit 50e9b44

Browse files
Created CreateSession() function and added base path in config
1 parent edab23d commit 50e9b44

File tree

4 files changed

+73
-27
lines changed

4 files changed

+73
-27
lines changed

config/app.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package config
22

3+
import (
4+
"errors"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
)
8+
39
/* app parameters */
410
type App struct {
511
Name string `yaml:"name,omitempty"`
612
Version string `yaml:"version,omitempty"`
713
DebugMode bool `yaml:"debug_mode,omitempty"`
814
SessionTimeout int `yaml:"session_timeout,omitempty"`
15+
BasePath string `yaml:"base_path,omitempty"`
916
}
1017

1118
/* normalization function */
@@ -27,5 +34,13 @@ func (a *App) Normalize() error {
2734
a.SessionTimeout = 24
2835
}
2936

37+
if a.BasePath == "" {
38+
return errors.New(heredoc.Doc(`
39+
Base path is not specified in the configuration file.
40+
41+
Please check the docs for more information:
42+
`))
43+
}
44+
3045
return nil
3146
}

internal/session/handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package session
2+
3+

internal/session/manager.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package session
2+
3+
import (
4+
"container/list"
5+
"fmt"
6+
"sync"
7+
"time"
8+
9+
"github.com/PythonHacker24/linux-acl-management-backend/config"
10+
)
11+
12+
var (
13+
activeSessions = make(map[string]*Session)
14+
activeSessionsMutex sync.RWMutex
15+
)
16+
17+
/* for creating a session for user */
18+
func CreateSession(username string) error {
19+
20+
/* lock the activeSessions mutex till the function ends */
21+
activeSessionsMutex.Lock()
22+
defer activeSessionsMutex.Unlock()
23+
24+
/* check if session exists */
25+
if _, exists := activeSessions[username]; exists {
26+
return fmt.Errorf("user already exists in active sessions")
27+
}
28+
29+
/* create the session */
30+
session := &Session{
31+
Username: username,
32+
Expiry: time.Now().Add(time.Duration(config.BackendConfig.AppInfo.SessionTimeout) * time.Hour),
33+
Timer: time.AfterFunc(time.Duration(config.BackendConfig.AppInfo.SessionTimeout) * time.Hour,
34+
func() { ExpireSession(username) },
35+
),
36+
TransactionQueue: list.New(),
37+
CurrentWorkingDir: config.BackendConfig.AppInfo.BasePath,
38+
}
39+
40+
/* add session to active sessions */
41+
activeSessions[username] = session
42+
43+
return nil
44+
}
45+
46+
/* for expiring a session */
47+
func ExpireSession(username string) {
48+
activeSessionsMutex.Lock()
49+
defer activeSessionsMutex.Unlock()
50+
51+
/* delete if user exists in active sessions */
52+
if _, exists := activeSessions[username]; exists {
53+
delete(activeSessions, username)
54+
}
55+
}

internal/session/session.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)