Add connect methods to libraries.

This commit is contained in:
Mike Cao 2022-10-06 15:00:16 -07:00
parent 186f484ff1
commit e442617421
5 changed files with 59 additions and 28 deletions

View file

@ -1,14 +1,13 @@
import { parseToken } from 'next-basics';
import { validate } from 'uuid';
import { uuid } from 'lib/crypto';
import { secret, uuid } from 'lib/crypto';
import redis, { DELETED } from 'lib/redis';
import clickhouse from 'lib/clickhouse';
import { getClientInfo, getJsonBody } from 'lib/request';
import { createSession, getSessionByUuid, getWebsiteByUuid } from 'queries';
export async function getSession(req) {
const { payload } = getJsonBody(req);
const hasRedis = process.env.REDIS_URL;
const hasClickhouse = process.env.CLICKHOUSE_URL;
if (!payload) {
throw new Error('Invalid request');
@ -17,7 +16,7 @@ export async function getSession(req) {
const cache = req.headers['x-umami-cache'];
if (cache) {
const result = await parseToken(cache);
const result = await parseToken(cache, secret());
if (result) {
return result;
@ -33,8 +32,8 @@ export async function getSession(req) {
let websiteId = null;
// Check if website exists
if (hasRedis) {
websiteId = Number(await redis.client.get(`website:${website_uuid}`));
if (redis.enabled) {
websiteId = Number(await redis.get(`website:${website_uuid}`));
}
// Check database if does not exists in Redis
@ -53,10 +52,10 @@ export async function getSession(req) {
let sessionId = null;
let session = null;
if (!hasClickhouse) {
if (!clickhouse.enabled) {
// Check if session exists
if (hasRedis) {
sessionId = Number(await redis.client.get(`session:${session_uuid}`));
if (redis.enabled) {
sessionId = Number(await redis.get(`session:${session_uuid}`));
}
// Check database if does not exists in Redis