-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathid.go
More file actions
39 lines (31 loc) · 740 Bytes
/
id.go
File metadata and controls
39 lines (31 loc) · 740 Bytes
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
package main
import (
"fmt"
"sync"
"github.com/gophergala/correct-horse-battery-staple/urlgen"
)
var id int64
var idLock sync.Mutex
func getUniqueId() int64 {
idLock.Lock()
id++
value := id
idLock.Unlock()
return value
}
func generateRoomId() string {
return urlgen.GetTokenFromId(getUniqueId())
}
// validateRoomId returns an error if id is of unexpected format.
func validateRoomId(id string) error {
if len(id) < 3 || len(id) > 64 {
return fmt.Errorf("id length is %v", len(id))
}
for _, b := range []byte(id) {
ok := ('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z') || ('0' <= b && b <= '9') || b == '-' || b == '_'
if !ok {
return fmt.Errorf("id contains unexpected character %+q", b)
}
}
return nil
}