diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-11-02 16:10:57 -0300 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | bc9051fd1a17e793647cf309c973a7feefebd98f (patch) | |
tree | bf16a2176e9129c3921ef6743a6c22781e744064 /db/redis.go | |
parent | d80afd8a49f552c5dc51d8346d40809298fef11f (diff) |
down to the main package it seems
Diffstat (limited to 'db/redis.go')
-rw-r--r-- | db/redis.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/db/redis.go b/db/redis.go new file mode 100644 index 0000000..873ca27 --- /dev/null +++ b/db/redis.go @@ -0,0 +1,68 @@ +package db + +import ( + "bufio" + "fmt" + "net/http" + "os" + + "github.com/FChannel0/FChannel-Server/config" + "github.com/gomodule/redigo/redis" +) + +var Cache redis.Conn + +func InitCache() error { + conn, err := redis.DialURL(config.Redis) + Cache = conn + return err +} + +func CloseCache() error { + return Cache.Close() +} + +func CheckSession(w http.ResponseWriter, r *http.Request) (interface{}, error) { + c, err := r.Cookie("session_token") + + if err != nil { + if err == http.ErrNoCookie { + w.WriteHeader(http.StatusUnauthorized) + return nil, err + } + + w.WriteHeader(http.StatusBadRequest) + return nil, err + } + + sessionToken := c.Value + + response, err := Cache.Do("GET", sessionToken) + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return nil, err + } + if response == nil { + w.WriteHeader(http.StatusUnauthorized) + return nil, err + } + + return response, nil +} + +func GetClientKey() (string, error) { + file, err := os.Open("clientkey") + if err != nil { + return "", err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + var line string + for scanner.Scan() { + line = fmt.Sprintf("%s", scanner.Text()) + } + + return line, nil +} |