Store users in s3
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsersService } from './users.service';
|
||||
import { MinioService } from 'src/minio/minio.service';
|
||||
|
||||
@Module({
|
||||
providers: [UsersService],
|
||||
exports: [UsersService]
|
||||
providers: [UsersService, MinioService],
|
||||
exports: [UsersService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
|
@@ -1,18 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
require('dotenv').config()
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Cron } from '@nestjs/schedule';
|
||||
import { MinioService } from 'src/minio/minio.service';
|
||||
require('dotenv').config();
|
||||
|
||||
// This should be a real class/interface representing a user entity
|
||||
export type User = any;
|
||||
export type User = {
|
||||
userId: number;
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
private readonly users = [
|
||||
{
|
||||
userId: 1,
|
||||
username: 'admin',
|
||||
password: process.env.ADMIN_PASS,
|
||||
},
|
||||
];
|
||||
private users: User[] = [];
|
||||
private readonly bucket: string;
|
||||
private readonly logger: Logger = new Logger(UsersService.name);
|
||||
|
||||
constructor(
|
||||
private readonly minioService: MinioService,
|
||||
private readonly configService: ConfigService,
|
||||
) {
|
||||
this.bucket = configService.get<string>('S3_BUCKET', 'api.us.dev');
|
||||
this.refreshUsers();
|
||||
}
|
||||
|
||||
@Cron('* * * * *')
|
||||
async refreshUsers(): Promise<void> {
|
||||
this.logger.verbose('Refreshing users');
|
||||
const buffer = await this.minioService.getBuffer(this.bucket, 'users.json');
|
||||
this.users = JSON.parse(buffer.toString());
|
||||
if (this.users === undefined) {
|
||||
this.logger.error('Users undefined');
|
||||
}
|
||||
if (this.users.length === 0) {
|
||||
this.logger.error('Users length is 0');
|
||||
}
|
||||
this.logger.verbose(`Refreshed with ${this.users.length} users`);
|
||||
}
|
||||
|
||||
async findOne(username: string): Promise<User | undefined> {
|
||||
return this.users.find((user) => user.username === username);
|
||||
|
Reference in New Issue
Block a user