Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
feat(handler): implement auth.session.get
Browse files Browse the repository at this point in the history
- implement `auth.session.get`

#3 Closes #6
  • Loading branch information
Leo Breuer committed Jul 22, 2023
1 parent 57a100e commit 7f0091d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
17 changes: 14 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package database

import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"time"

"github.com/redis/go-redis/v9"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

func Conn() *gorm.DB {
dsn := "host=192.168.1.28 user=miauw_user password=miauw_password dbname=miauw port=5432 sslmode=disable TimeZone=Europe/Berlin"
dsn := "host=192.168.1.28 user=miauw password=password dbname=miauw port=5432 sslmode=disable TimeZone=Europe/Berlin"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Panic(" [!] Failed to connect to database.")
Expand All @@ -23,3 +25,12 @@ func Conn() *gorm.DB {
sqlDB.SetConnMaxLifetime(time.Hour)
return db
}

func RedisConn() *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: "192.168.1.28:6379",
Password: "",
DB: 0,
})
return rdb
}
14 changes: 7 additions & 7 deletions handlers/session_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package handlers

import (
"encoding/json"
"fmt"
"time"

"gorm.io/gorm"
"miauw.social/auth/database/models"
)

type GetUserSessionData struct {
UserID string
ID string
}

func GetUserSession(db *gorm.DB, rawData []byte) (Response, error) {
Expand All @@ -25,12 +26,11 @@ func GetUserSession(db *gorm.DB, rawData []byte) (Response, error) {
},
}, err
}
// var sessions []models.Session
results := make(map[string]interface{})
db.Where("user = ?", sessionData.UserID).Take(&results)
fmt.Println(results)
deltaTtl := time.Now().Add(-12 * time.Hour)
var sessions []models.Session
db.Where(`"user"::text = ?`, sessionData.ID).Where("created_at >= ?", deltaTtl).Take(&sessions)
return Response{
Content: &results,
Content: &sessions,
Status: ResponseStatus{
Code: 200,
},
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "miauw.social/auth/handlers"
import (
"miauw.social/auth/handlers"
)

func main() {
var forever chan struct{}
Expand Down

0 comments on commit 7f0091d

Please sign in to comment.