-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
38 lines (29 loc) · 930 Bytes
/
session.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
package middleware
import (
"context"
"net/http"
"github.com/pkg/errors"
"github.com/dayaftereh/discover/server/api"
"github.com/dayaftereh/discover/server/api/session"
)
type SessionMiddleware struct {
manager *session.Manager
}
func NewSessionMiddleware(manager *session.Manager) SessionMiddleware {
return SessionMiddleware{
manager,
}
}
func (middleware SessionMiddleware) WrapHandler(handler api.Function) api.Function {
return func(ctx context.Context, response http.ResponseWriter, request *http.Request, variables map[string]string) error {
// try to get the session for the request
session, err := middleware.manager.Get(response, request)
// check for error
if err != nil {
return errors.Wrapf(err, "fail to extract session from incoming request")
}
// create the session context
ctx = api.ForkSessionContext(ctx, session.Id)
return handler(ctx, response, request, variables)
}
}