moved string creation helper to utils

This commit is contained in:
Ruben Cid
2020-04-10 10:11:05 +02:00
parent ffc86e7611
commit a15aa45484
3 changed files with 20 additions and 21 deletions

View File

@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- JSON Web Token support.
``` json
// .livego.json
// livego.json
{
"jwt": {
"secret": "testing",
@@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
```
- Use redis for store room keys
``` json
// .livego.json
// livego.json
{
"redis_addr": "localhost:6379",
"server": [
@@ -42,4 +42,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Show `players`.
- Show `stream_id`.
- Deleted keys saved in physical file, now the keys are in cached using `go-cache`
- Deleted keys saved in physical file, now the keys are in cached using `go-cache` by default.

View File

@@ -3,16 +3,14 @@ package configure
import (
"fmt"
"log"
"math/rand"
"livego/utils/uid"
"github.com/go-redis/redis/v7"
"github.com/patrickmn/go-cache"
)
var RoomKeys *RoomKeysType
var roomUpdated = false
var saveInLocal = true
type RoomKeysType struct {
@@ -49,7 +47,7 @@ func Init() {
func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
if !saveInLocal {
for {
key = randStringRunes(48)
key = uid.RandStringRunes(48)
if _, err = r.redisCli.Get(key).Result(); err == redis.Nil {
err = r.redisCli.Set(channel, key, 0).Err()
if err != nil {
@@ -65,14 +63,13 @@ func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
}
for {
key = randStringRunes(48)
key = uid.RandStringRunes(48)
if _, found := r.localCache.Get(key); !found {
r.localCache.SetDefault(channel, key)
r.localCache.SetDefault(key, channel)
break
}
}
roomUpdated = true
return
}
@@ -137,14 +134,3 @@ func (r *RoomKeysType) DeleteKey(key string) bool {
}
return false
}
// helpers
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}

13
utils/uid/rand.go Normal file
View File

@@ -0,0 +1,13 @@
package uid
import "math/rand"
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}