-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(libserver,libsession): adding a few cookies management function …
…and implementing a session api.
- Loading branch information
Showing
8 changed files
with
138 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package frizzante | ||
|
||
import "github.com/nu7hatch/gouuid" | ||
|
||
var sessions = map[string]*Session{} | ||
|
||
type Session struct { | ||
id string | ||
data map[string]interface{} | ||
} | ||
|
||
func (s *Session) get(key string, defaultValue interface{}) interface{} { | ||
sessionItem, ok := s.data[key] | ||
if !ok { | ||
s.data[key] = defaultValue | ||
return s.data[key] | ||
} | ||
|
||
return sessionItem | ||
} | ||
|
||
func (s *Session) set(key string, defaultValue interface{}) { | ||
s.data[key] = defaultValue | ||
} | ||
|
||
func sessionCreate() (*Session, error) { | ||
sessionId, uuidError := uuid.NewV4() | ||
if uuidError != nil { | ||
return nil, uuidError | ||
} | ||
|
||
return &Session{ | ||
id: sessionId.String(), | ||
data: map[string]interface{}{}, | ||
}, nil | ||
} | ||
|
||
// SessionStart starts a new session or retrieves it if it's already been started. | ||
// | ||
// SessionStart always returns two functions, a get() and a set(), which you can use to manage the session. | ||
func SessionStart(request *Request, response *Response) ( | ||
get func(key string, defaultValue interface{}) interface{}, | ||
set func(key string, defaultValue interface{}), | ||
) { | ||
sessionIdCookie, cookieError := request.HttpRequest.Cookie("session-id") | ||
if cookieError != nil { | ||
freshSession, sessionError := sessionCreate() | ||
if sessionError != nil { | ||
ServerNotifyError(request.server, sessionError) | ||
return nil, nil | ||
} | ||
|
||
SendCookie(response, "session-id", freshSession.id) | ||
sessions[freshSession.id] = freshSession | ||
get = freshSession.get | ||
set = freshSession.set | ||
return | ||
} | ||
|
||
session, sessionExists := sessions[sessionIdCookie.Value] | ||
if !sessionExists { | ||
freshSession, sessionError := sessionCreate() | ||
if sessionError != nil { | ||
ServerNotifyError(request.server, sessionError) | ||
return nil, nil | ||
} | ||
|
||
SendCookie(response, "session-id", freshSession.id) | ||
sessions[freshSession.id] = freshSession | ||
get = freshSession.get | ||
set = freshSession.set | ||
return | ||
} | ||
|
||
SendCookie(response, "session-id", sessionIdCookie.Value) | ||
get = session.get | ||
set = session.set | ||
return | ||
} | ||
|
||
// SessionDestroy destroys the session. | ||
func SessionDestroy(self *Session) { | ||
delete(sessions, self.id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,7 +294,7 @@ | |
|
||
"statuses": ["[email protected]", "", {}, "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ=="], | ||
|
||
"svelte": ["[email protected].5", "", { "dependencies": { "@ampproject/remapping": "^2.3.0", "@jridgewell/sourcemap-codec": "^1.5.0", "@types/estree": "^1.0.5", "acorn": "^8.12.1", "acorn-typescript": "^1.4.13", "aria-query": "^5.3.1", "axobject-query": "^4.1.0", "clsx": "^2.1.1", "esm-env": "^1.2.1", "esrap": "^1.4.3", "is-reference": "^3.0.3", "locate-character": "^3.0.0", "magic-string": "^0.30.11", "zimmerframe": "^1.1.2" } }, "sha512-vVAntseegJX80sgbY8CxQISSE/VoDSfP7VZHoQaf2+z+2XOPOz/N+k455HJmO9O0g8oxTtuE0TBhC/5LAP4lPg=="], | ||
"svelte": ["[email protected].6", "", { "dependencies": { "@ampproject/remapping": "^2.3.0", "@jridgewell/sourcemap-codec": "^1.5.0", "@types/estree": "^1.0.5", "acorn": "^8.12.1", "acorn-typescript": "^1.4.13", "aria-query": "^5.3.1", "axobject-query": "^4.1.0", "clsx": "^2.1.1", "esm-env": "^1.2.1", "esrap": "^1.4.3", "is-reference": "^3.0.3", "locate-character": "^3.0.0", "magic-string": "^0.30.11", "zimmerframe": "^1.1.2" } }, "sha512-6ydekB3qyqUal+UhfMjmVOjRGtxysR8vuiMhi2nwuBtPJWnctVlsGspjVFB05qmR+TXI1emuqtZt81c0XiFleA=="], | ||
|
||
"toidentifier": ["[email protected]", "", {}, "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA=="], | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters