-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
99 lines (76 loc) · 1.77 KB
/
main.go
File metadata and controls
99 lines (76 loc) · 1.77 KB
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
97
98
99
package main
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/redis/go-redis/v9"
"gopkg.in/yaml.v3"
"net/http"
"os"
)
type YamlConf struct {
Middleware struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
} `yaml:"middleware"`
Redis struct {
Addr string `yaml:"addr"`
Port string `yaml:"port"`
Password string `yaml:"password"`
Db int `yaml:"db"`
} `yaml:"redis"`
}
func NewConf() *YamlConf {
file, err := os.ReadFile("./middleware.conf.yaml")
if err != nil {
panic(err)
}
var conf YamlConf
err = yaml.Unmarshal(file, &conf)
if err != nil {
panic(err)
}
return &conf
}
type AuthSessionMiddleware struct {
next http.Handler
Conf *YamlConf
RdClient *redis.Client
}
func NewAuthSessionMiddleware(_ context.Context, next http.Handler, conf *YamlConf) (http.Handler, error) {
rdClient := redis.NewClient(&redis.Options{
Addr: conf.Redis.Addr + ":" + conf.Redis.Port,
Password: conf.Redis.Password,
DB: conf.Redis.Db,
})
return &AuthSessionMiddleware{
next: next,
Conf: conf,
RdClient: rdClient,
}, nil
}
func (a *AuthSessionMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.next.ServeHTTP(w, r)
}
func (a *AuthSessionMiddleware) middlewareLogic(w http.ResponseWriter, r *http.Request) {
sessCookie, err := r.Cookie("" /*ЗДЕСЬ СТАВИМ ИЗ YAML CONF*/)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
if sessCookie.Value == "" && sessCookie.Name == "" {
/*ЛОГИКА ВЫДАЧИ SESSION_ID*/
}
}
func genSessionId() string {
buffer := make([]byte, 16)
_, err := rand.Read(buffer)
if err != nil {
panic(err)
}
return hex.EncodeToString(buffer)
}
func main() {
f := genSessionId()
fmt.Println(f)
}