mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 14:53:25 +00:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f94c1afdf5 | ||
|
f7074157c2 | ||
|
1cecc2cccd | ||
|
effe139683 | ||
|
16fb41c898 | ||
|
01c96babd4 | ||
|
53801f926a | ||
|
ae0ffa9894 | ||
|
b1a79242aa |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "backend",
|
"name": "backend",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"homepage": "https://github.com/movie-web/backend",
|
"homepage": "https://github.com/movie-web/backend",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
@@ -2,7 +2,7 @@ import { FragmentSchema } from '@/config/fragments/types';
|
|||||||
|
|
||||||
export const devFragment: FragmentSchema = {
|
export const devFragment: FragmentSchema = {
|
||||||
server: {
|
server: {
|
||||||
cors: 'http://localhost:5173',
|
allowAnySite: true,
|
||||||
trustProxy: true,
|
trustProxy: true,
|
||||||
},
|
},
|
||||||
logging: {
|
logging: {
|
||||||
|
@@ -8,7 +8,7 @@ const fragments = {
|
|||||||
dockerdev: dockerFragment,
|
dockerdev: dockerFragment,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const version = '1.0.0';
|
export const version = '1.0.3';
|
||||||
|
|
||||||
export const conf = createConfigLoader()
|
export const conf = createConfigLoader()
|
||||||
.addFromEnvironment('MWB_')
|
.addFromEnvironment('MWB_')
|
||||||
|
20
src/config/orm.ts
Normal file
20
src/config/orm.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { createConfigLoader } from 'neat-config';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
export const ormConfigSchema = z.object({
|
||||||
|
postgres: z.object({
|
||||||
|
// connection URL for postgres database
|
||||||
|
connection: z.string(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const ormConf = createConfigLoader()
|
||||||
|
.addFromEnvironment('MWB_')
|
||||||
|
.addFromCLI('mwb-')
|
||||||
|
.addFromFile('.env', {
|
||||||
|
prefix: 'MWB_',
|
||||||
|
})
|
||||||
|
.addFromFile('config.json')
|
||||||
|
.addZodSchema(ormConfigSchema)
|
||||||
|
.freeze()
|
||||||
|
.load();
|
@@ -9,6 +9,10 @@ export const configSchema = z.object({
|
|||||||
// space seperated list of allowed cors domains
|
// space seperated list of allowed cors domains
|
||||||
cors: z.string().default(''),
|
cors: z.string().default(''),
|
||||||
|
|
||||||
|
// disable cross origin restrictions, allow any site.
|
||||||
|
// overwrites the cors option above
|
||||||
|
allowAnySite: z.coerce.boolean().default(false),
|
||||||
|
|
||||||
// should it trust reverse proxy headers? (for ip gathering)
|
// should it trust reverse proxy headers? (for ip gathering)
|
||||||
trustProxy: z.coerce.boolean().default(false),
|
trustProxy: z.coerce.boolean().default(false),
|
||||||
|
|
||||||
|
@@ -184,6 +184,24 @@
|
|||||||
"nullable": true,
|
"nullable": true,
|
||||||
"mappedType": "string"
|
"mappedType": "string"
|
||||||
},
|
},
|
||||||
|
"season_number": {
|
||||||
|
"name": "season_number",
|
||||||
|
"type": "int",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": true,
|
||||||
|
"mappedType": "integer"
|
||||||
|
},
|
||||||
|
"episode_number": {
|
||||||
|
"name": "episode_number",
|
||||||
|
"type": "int",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": true,
|
||||||
|
"mappedType": "integer"
|
||||||
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"name": "meta",
|
"name": "meta",
|
||||||
"type": "jsonb",
|
"type": "jsonb",
|
||||||
|
14
src/db/migrations/Migration20231105150807.ts
Normal file
14
src/db/migrations/Migration20231105150807.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { Migration } from '@mikro-orm/migrations';
|
||||||
|
|
||||||
|
export class Migration20231105150807 extends Migration {
|
||||||
|
async up(): Promise<void> {
|
||||||
|
this.addSql(
|
||||||
|
'alter table "progress_items" add column "season_number" int null, add column "episode_number" int null;',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async down(): Promise<void> {
|
||||||
|
this.addSql('alter table "progress_items" drop column "season_number";');
|
||||||
|
this.addSql('alter table "progress_items" drop column "episode_number";');
|
||||||
|
}
|
||||||
|
}
|
@@ -6,7 +6,7 @@ export const progressMetaSchema = z.object({
|
|||||||
title: z.string(),
|
title: z.string(),
|
||||||
year: z.number(),
|
year: z.number(),
|
||||||
poster: z.string().optional(),
|
poster: z.string().optional(),
|
||||||
type: z.string(),
|
type: z.union([z.literal('show'), z.literal('movie')]),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type ProgressMeta = z.infer<typeof progressMetaSchema>;
|
export type ProgressMeta = z.infer<typeof progressMetaSchema>;
|
||||||
@@ -29,6 +29,12 @@ export class ProgressItem {
|
|||||||
@Property({ name: 'episode_id', nullable: true })
|
@Property({ name: 'episode_id', nullable: true })
|
||||||
episodeId?: string;
|
episodeId?: string;
|
||||||
|
|
||||||
|
@Property({ name: 'season_number', nullable: true })
|
||||||
|
seasonNumber?: number;
|
||||||
|
|
||||||
|
@Property({ name: 'episode_number', nullable: true })
|
||||||
|
episodeNumber?: number;
|
||||||
|
|
||||||
@Property({
|
@Property({
|
||||||
name: 'meta',
|
name: 'meta',
|
||||||
type: types.json,
|
type: types.json,
|
||||||
@@ -48,8 +54,14 @@ export class ProgressItem {
|
|||||||
|
|
||||||
export interface ProgressItemDTO {
|
export interface ProgressItemDTO {
|
||||||
tmdbId: string;
|
tmdbId: string;
|
||||||
seasonId?: string;
|
season: {
|
||||||
episodeId?: string;
|
id?: string;
|
||||||
|
number?: number;
|
||||||
|
};
|
||||||
|
episode: {
|
||||||
|
id?: string;
|
||||||
|
number?: number;
|
||||||
|
};
|
||||||
meta: {
|
meta: {
|
||||||
title: string;
|
title: string;
|
||||||
year: number;
|
year: number;
|
||||||
@@ -66,8 +78,14 @@ export function formatProgressItem(
|
|||||||
): ProgressItemDTO {
|
): ProgressItemDTO {
|
||||||
return {
|
return {
|
||||||
tmdbId: progressItem.tmdbId,
|
tmdbId: progressItem.tmdbId,
|
||||||
seasonId: progressItem.seasonId,
|
episode: {
|
||||||
episodeId: progressItem.episodeId,
|
id: progressItem.episodeId,
|
||||||
|
number: progressItem.episodeNumber,
|
||||||
|
},
|
||||||
|
season: {
|
||||||
|
id: progressItem.seasonId,
|
||||||
|
number: progressItem.seasonNumber,
|
||||||
|
},
|
||||||
meta: {
|
meta: {
|
||||||
title: progressItem.meta.title,
|
title: progressItem.meta.title,
|
||||||
year: progressItem.meta.year,
|
year: progressItem.meta.year,
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
|
import { ormConf } from '@/config/orm';
|
||||||
import { makeOrmConfig } from '@/modules/mikro/orm';
|
import { makeOrmConfig } from '@/modules/mikro/orm';
|
||||||
import { conf } from '@/config';
|
|
||||||
|
|
||||||
export default makeOrmConfig(conf.postgres.connection);
|
export default makeOrmConfig(ormConf.postgres.connection);
|
||||||
|
@@ -56,8 +56,10 @@ export async function setupFastify(): Promise<FastifyInstance> {
|
|||||||
|
|
||||||
// plugins
|
// plugins
|
||||||
log.info(`setting up plugins`, { evt: 'setup-plugins' });
|
log.info(`setting up plugins`, { evt: 'setup-plugins' });
|
||||||
|
const corsDomains = conf.server.cors.split(' ').filter((v) => v.length > 0);
|
||||||
|
const corsSetting = conf.server.allowAnySite ? true : corsDomains;
|
||||||
await app.register(cors, {
|
await app.register(cors, {
|
||||||
origin: conf.server.cors.split(' ').filter((v) => v.length > 0),
|
origin: corsSetting,
|
||||||
credentials: true,
|
credentials: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { ChallengeCode } from '@/db/models/ChallengeCode';
|
import { ChallengeCode } from '@/db/models/ChallengeCode';
|
||||||
import { formatSession } from '@/db/models/Session';
|
import { formatSession } from '@/db/models/Session';
|
||||||
import { User } from '@/db/models/User';
|
import { User, formatUser } from '@/db/models/User';
|
||||||
import { assertChallengeCode } from '@/services/challenge';
|
import { assertChallengeCode } from '@/services/challenge';
|
||||||
import { StatusError } from '@/services/error';
|
import { StatusError } from '@/services/error';
|
||||||
import { handle } from '@/services/handler';
|
import { handle } from '@/services/handler';
|
||||||
@@ -85,6 +85,7 @@ export const loginAuthRouter = makeRouter((app) => {
|
|||||||
await em.persistAndFlush([session, user]);
|
await em.persistAndFlush([session, user]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
user: formatUser(user),
|
||||||
session: formatSession(session),
|
session: formatSession(session),
|
||||||
token: makeSessionToken(session),
|
token: makeSessionToken(session),
|
||||||
};
|
};
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { conf } from '@/config';
|
import { conf, version } from '@/config';
|
||||||
import { handle } from '@/services/handler';
|
import { handle } from '@/services/handler';
|
||||||
import { makeRouter } from '@/services/router';
|
import { makeRouter } from '@/services/router';
|
||||||
|
|
||||||
@@ -26,6 +26,7 @@ export const metaRouter = makeRouter((app) => {
|
|||||||
return {
|
return {
|
||||||
name: conf.meta.name,
|
name: conf.meta.name,
|
||||||
description: conf.meta.description,
|
description: conf.meta.description,
|
||||||
|
version: version,
|
||||||
hasCaptcha: conf.captcha.enabled,
|
hasCaptcha: conf.captcha.enabled,
|
||||||
captchaClientKey: conf.captcha.clientKey,
|
captchaClientKey: conf.captcha.clientKey,
|
||||||
};
|
};
|
||||||
|
@@ -19,10 +19,12 @@ export const userProgressRouter = makeRouter((app) => {
|
|||||||
}),
|
}),
|
||||||
body: z.object({
|
body: z.object({
|
||||||
meta: progressMetaSchema,
|
meta: progressMetaSchema,
|
||||||
seasonId: z.string().optional(),
|
|
||||||
episodeId: z.string().optional(),
|
|
||||||
duration: z.number(),
|
duration: z.number(),
|
||||||
watched: z.number(),
|
watched: z.number(),
|
||||||
|
seasonId: z.string().optional(),
|
||||||
|
episodeId: z.string().optional(),
|
||||||
|
seasonNumber: z.number().optional(),
|
||||||
|
episodeNumber: z.number().optional(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -38,12 +40,15 @@ export const userProgressRouter = makeRouter((app) => {
|
|||||||
episodeId: body.episodeId,
|
episodeId: body.episodeId,
|
||||||
seasonId: body.seasonId,
|
seasonId: body.seasonId,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!progressItem) {
|
if (!progressItem) {
|
||||||
progressItem = new ProgressItem();
|
progressItem = new ProgressItem();
|
||||||
progressItem.tmdbId = params.tmdbid;
|
progressItem.tmdbId = params.tmdbid;
|
||||||
progressItem.userId = params.uid;
|
progressItem.userId = params.uid;
|
||||||
progressItem.episodeId = body.episodeId;
|
progressItem.episodeId = body.episodeId;
|
||||||
progressItem.seasonId = body.seasonId;
|
progressItem.seasonId = body.seasonId;
|
||||||
|
progressItem.episodeNumber = body.episodeNumber;
|
||||||
|
progressItem.seasonNumber = body.seasonNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
em.assign(progressItem, {
|
em.assign(progressItem, {
|
||||||
|
@@ -64,6 +64,8 @@ export function scopedLogger(service: string, meta: object = {}) {
|
|||||||
return logger;
|
return logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ignoredUrls = ['/healthcheck', '/metrics'];
|
||||||
|
|
||||||
export function makeFastifyLogger(logger: winston.Logger) {
|
export function makeFastifyLogger(logger: winston.Logger) {
|
||||||
logger.format = winston.format.combine(
|
logger.format = winston.format.combine(
|
||||||
winston.format((info) => {
|
winston.format((info) => {
|
||||||
@@ -78,6 +80,9 @@ export function makeFastifyLogger(logger: winston.Logger) {
|
|||||||
let url = request.url;
|
let url = request.url;
|
||||||
try {
|
try {
|
||||||
const pathParts = (request.url as string).split('?', 2);
|
const pathParts = (request.url as string).split('?', 2);
|
||||||
|
|
||||||
|
if (ignoredUrls.includes(pathParts[0])) return false;
|
||||||
|
|
||||||
if (pathParts[1]) {
|
if (pathParts[1]) {
|
||||||
const searchParams = new URLSearchParams(pathParts[1]);
|
const searchParams = new URLSearchParams(pathParts[1]);
|
||||||
pathParts[1] = searchParams.toString();
|
pathParts[1] = searchParams.toString();
|
||||||
|
Reference in New Issue
Block a user