-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
users.go
63 lines (56 loc) · 1.89 KB
/
users.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
package users
import (
"fmt"
"net/http"
"sync"
"github.com/rivo/sessions"
)
// The users in our system.
var (
users []User
usersMutex sync.RWMutex
)
// Variables that help pausing access to some functions.
var (
userMutexes = make(map[string]*sync.Mutex) // Maps user email addresses to mutexes.
userMutexesMutex sync.Mutex // Mutex for the userMutexes map.
pauseMutex sync.Mutex // Single mutex for global pauses.
)
// Initialize this package.
func init() {
// See Config user functions for more information.
persistence, ok := sessions.Persistence.(sessions.ExtendablePersistenceLayer)
if ok {
persistence.LoadUserFunc = func(id interface{}) (sessions.User, error) {
userID, ok := id.(string)
if !ok {
return nil, fmt.Errorf("User ID is not a string: %#v", id)
}
usersMutex.RLock()
defer usersMutex.RUnlock()
for _, user := range users {
if user.GetID() == userID {
return user, nil
}
}
return nil, fmt.Errorf("User not found: %s", userID)
}
}
}
// Main makes your life simple by starting an HTTP server for you with the
// routes found in the Config variable. If you use this, for all remaining
// pages of your application, you only need to add your own handlers to the
// DefaultServerMux prior to calling this function. See package documentation
// for an example.
func Main() error {
Config.Log.Printf("Starting HTTP server on %s", Config.ServerAddr)
defer Config.Log.Printf("Stopping HTTP server")
http.HandleFunc(Config.RouteSignUp, SignUp)
http.HandleFunc(Config.RouteVerify, Verify)
http.HandleFunc(Config.RouteLogIn, LogIn)
http.HandleFunc(Config.RouteLogOut, LogOut)
http.HandleFunc(Config.RouteForgottenPassword, ForgottenPassword)
http.HandleFunc(Config.RouteResetPassword, ResetPassword)
http.HandleFunc(Config.RouteChange, Change)
return http.ListenAndServe(Config.ServerAddr, nil)
}