-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.go
40 lines (33 loc) · 1.13 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
// Package service provides utilities used for implementing services.
package service
import (
"net/http"
"github.com/spf13/viper"
"github.com/France-ioi/AlgoreaBackend/app/auth"
"github.com/France-ioi/AlgoreaBackend/app/database"
"github.com/France-ioi/AlgoreaBackend/app/domain"
"github.com/France-ioi/AlgoreaBackend/app/token"
)
// Base is the common service context data.
type Base struct {
store *database.DataStore
ServerConfig *viper.Viper
AuthConfig *viper.Viper
DomainConfig []domain.ConfigItem
TokenConfig *token.Config
}
// SetGlobalStore sets the global store shared by all the request (should be called only once on start).
func (srv *Base) SetGlobalStore(store *database.DataStore) {
srv.store = store
}
// GetUser returns the authenticated user data from context.
func (srv *Base) GetUser(r *http.Request) *database.User {
return auth.UserFromContext(r.Context())
}
// GetStore returns a data store with the given request's context.
func (srv *Base) GetStore(r *http.Request) *database.DataStore {
if srv.store == nil {
return nil
}
return database.NewDataStoreWithContext(r.Context(), srv.store.DB)
}