forked from Chapa-Et/chapa-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
37 lines (32 loc) · 764 Bytes
/
config.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
package chapa
import (
"context"
"crypto/rand"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"log"
"math/big"
)
func InitConfig() {
viper.SetConfigFile("config.yaml")
err := viper.ReadInConfig()
if err != nil {
log.Panic(context.Background(), fmt.Sprintf("Failed to read config: %v", err))
}
viper.OnConfigChange(func(e fsnotify.Event) {
log.Printf("Config file changed: file %v", e.Name)
})
}
const alphabet = "abcdefghijklmnopqrstuvwxyz"
func RandomString(length int) string {
var str string
for i := 0; i < length; i++ {
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphabet))))
if err != nil {
log.Printf("error while generating string: %v", err)
}
str += string(alphabet[n.Int64()])
}
return str
}