This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
96 lines (83 loc) · 1.91 KB
/
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package config
import (
"io"
"log"
"math/rand"
"os"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v2"
)
var CFG Config
type Config struct {
PostgresqlDsn string `yaml:"postgresql_dsn"`
LogFile string `yaml:"log_file"`
JWTSigningString string `yaml:"jwt_signing_string"`
RedisDsn string `yaml:"redis_dsn"`
RedisPassword string `yaml:"redis_password"`
RedisDB int `yaml:"redis_db"`
JWTExpire int `yaml:"jwt_expire"`
}
func ReadConfig() (*Config, error) {
_, err := os.Stat("config.yaml")
if os.IsNotExist(err) {
f, err := os.Create("config.yaml")
if err != nil {
return nil, err
}
config := &Config{
PostgresqlDsn: "host=localhost user=postgres password=1234 dbname=API_BIG_WORK port=5432 sslmode=disable",
LogFile: "log.txt",
JWTSigningString: GenerateRandomString(32),
RedisDsn: "localhost:6379",
RedisPassword: "",
RedisDB: 0,
JWTExpire: 3600,
}
data, err := yaml.Marshal(&config)
if err != nil {
return nil, err
}
_, err = f.Write(data)
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}
}
f, err := os.Open("config.yaml")
if err != nil {
return nil, err
}
defer f.Close()
bytes, err := io.ReadAll(f)
if err != nil {
return nil, err
}
var cfg Config
err = yaml.Unmarshal(bytes, &cfg)
if err != nil {
return nil, err
}
CFG = cfg
return &cfg, nil
}
func InitLog(config *Config) {
f, err := os.OpenFile(config.LogFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
log.SetOutput(f)
log.SetFlags(log.LstdFlags | log.Lshortfile)
gin.DefaultWriter = f
}
func GenerateRandomString(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}