forked from gaodoo/golang-restful-starter-kit
/
scope.go
78 lines (66 loc) · 2.25 KB
/
scope.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
package app
import (
"net/http"
"time"
"github.com/Sirupsen/logrus"
"github.com/go-ozzo/ozzo-dbx"
)
// RequestScope contains the application-specific information that are carried around in a request.
type RequestScope interface {
Logger
// UserID returns the ID of the user for the current request
UserID() string
// Tx returns the currently active database transaction that can be used for DB query purpose
Tx() *dbx.Tx
// SetTx sets the database transaction
SetTx(tx *dbx.Tx)
// Rollback returns a value indicating whether the current database transaction should be rolled back
Rollback() bool
// SetRollback sets a value indicating whether the current database transaction should be rolled back
SetRollback(bool)
// Now returns the timestamp representing the time when the request is being processed
Now() time.Time
}
type requestScope struct {
Logger // the logger tagged with the current request information
now time.Time // the time when the request is being processed
userID string // an ID identifying the current user
rollback bool // whether to roll back the current transaction
tx *dbx.Tx // the currently active transaction
}
func (rs *requestScope) UserID() string {
return rs.userID
}
func (rs *requestScope) Tx() *dbx.Tx {
return rs.tx
}
func (rs *requestScope) SetTx(tx *dbx.Tx) {
rs.tx = tx
}
func (rs *requestScope) Rollback() bool {
return rs.rollback
}
func (rs *requestScope) SetRollback(v bool) {
rs.rollback = v
}
func (rs *requestScope) Now() time.Time {
return rs.now
}
// newRequestScope creates a new RequestScope with the current request information.
func newRequestScope(now time.Time, logger *logrus.Logger, request *http.Request) RequestScope {
userID := authenticate(request)
return &requestScope{
Logger: NewLogger(logger, logrus.Fields{
"UserID": userID,
}),
now: now,
userID: userID,
}
}
// authenticate authenticates the current user.
// The default implementation simply returns the value of the X-User-Id HTTP header, which assumes
// the authentication has been done by an API gateway.
// If this is not the case, you may customize this method with the actual authentication logic.
func authenticate(request *http.Request) string {
return request.Header.Get("X-User-Id")
}