-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
136 lines (114 loc) · 3.58 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2017 jungle Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// date : 2017/12/5 15:01
// author : caimmy@hotmail.com
package session
import (
"crypto/rand"
"encoding/base64"
"errors"
"io"
"strings"
"sync"
"time"
"github.com/caimmy/jungle/context"
)
const (
FILE_SESSION = "file"
REDIS_SESSION = "redis"
)
var (
SESS_ID string
)
func init() {
SESS_ID = "jungleid"
}
type SessionMgrInterface interface {
OpenSession(ctx *context.Context)
CloseSession(ctx *context.Context)
SetSession(ctx *context.Context, session Session)
Set(ctx *context.Context, key string, value interface{})
GetSession(ctx *context.Context) (Session, error)
Get(ctx *context.Context, key string) interface{}
UpdateSession(ctx *context.Context)
GC()
}
type Session struct {
SessID string `json:"sessid"`
LastTimeAccessed int64 `json:"lsttm"`
Values map[string]interface{} `json:"values"`
}
type SessionManager struct {
m_strCookieName string
m_iMaxlife int64
mLock sync.RWMutex
}
func (this *SessionManager) LoadCookieValue(ctx *context.Context) string {
cookie, err := ctx.Request.Cookie(this.m_strCookieName)
if err != nil || cookie.Value == `` {
n_id, _ := this.NewSessionID()
return n_id
} else {
return cookie.Value
}
}
func (this *SessionManager) OpenSession(ctx *context.Context) {
panic("tobe implements.")
}
func (this *SessionManager) CloseSession(ctx *context.Context) {
panic("tobe implements.")
}
func (this *SessionManager) SetSession(ctx *context.Context, session Session) {
panic("tobe implements.")
}
func (this *SessionManager) Set(ctx *context.Context, key string, value interface{}) {
panic("tobe implements.")
}
func (this *SessionManager) GetSession(ctx *context.Context) (Session, error) {
panic("tobe implements.")
}
func (this *SessionManager) Get(ctx *context.Context, key string) interface{} {
panic("tobe implements.")
}
func (this *SessionManager) UpdateSession(ctx *context.Context) {
panic("tobe implements.")
}
func (this *SessionManager) GC() {
panic("tobe implements.")
}
func (this *SessionManager) NewSessionID() (string, error) {
b := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return "", errors.New("failed to generate session id")
}
return base64.URLEncoding.EncodeToString(b), nil
}
func NewSessionManager(sess_type string, iMaxlife int64, ext_params map[string]interface{}) SessionMgrInterface {
var ret_session_manager SessionMgrInterface
switch strings.ToLower(sess_type) {
case REDIS_SESSION:
ret_session_manager = &RedisSession{SessionManager: SessionManager{m_strCookieName: SESS_ID, m_iMaxlife: iMaxlife}}
case FILE_SESSION:
cache_path, ok := ext_params["path"]
if !ok {
return nil
}
ret_session_manager = &FileSession{SessPath: cache_path.(string), SessionManager: SessionManager{m_strCookieName: SESS_ID, m_iMaxlife: iMaxlife}}
}
go ret_session_manager.GC()
return ret_session_manager
}
func NewSessionObject() *Session {
return &Session{SESS_ID, time.Now().Unix(), make(map[string]interface{})}
}