Skip to content

Commit

Permalink
Merge pull request #507 from neet/api-integration
Browse files Browse the repository at this point in the history
chore: Change api root to /rest
  • Loading branch information
neet authored Nov 9, 2022
2 parents 9c38690 + 1bd2744 commit 8a94205
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 294 deletions.
1 change: 1 addition & 0 deletions packages/@neet/vschedule-api-spec/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
rm -rf ./dist
yarn run tsc
yarn run mkdirp ./dist
node ./dist/api.js >./dist/api.json
Expand Down
6 changes: 5 additions & 1 deletion packages/@neet/vschedule-api-spec/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ for (const module of modules) {
const document = new OpenAPIGenerator(registry.definitions).generateDocument({
openapi: '3.0.1',
info: {
title: 'Refined Itsukara.link',
title: 'VSchedule Rest API',
version: JSON.parse(
fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8'),
).version,
Expand All @@ -42,6 +42,10 @@ const document = new OpenAPIGenerator(registry.definitions).generateDocument({
url: 'http://localhost:3000',
description: 'Development environment',
},
{
url: 'https://api.vschedule.app',
description: 'Production',
},
],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { registry } from '../../../api';
import { PathMediaFilename } from '../../../components/parameters/PathMediaFilename';

registry.registerPath({
path: '/api/v1/media/{mediaFilename}',
path: '/rest/v1/media/{mediaFilename}',
method: 'get',
operationId: 'showMedia',
summary: 'メディアを表示',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Organization } from '../../../components/schemas/Organization';

registry.registerPath({
method: 'get',
path: '/api/v1/organizations/{organizationId}',
path: '/rest/v1/organizations/{organizationId}',
operationId: 'showOrganization',
summary: '組織を表示',
request: {
Expand All @@ -26,7 +26,7 @@ registry.registerPath({

registry.registerPath({
method: 'get',
path: '/api/v1/organizations',
path: '/rest/v1/organizations',
operationId: 'listOrganizations',
summary: '組織を一覧',
request: {
Expand Down Expand Up @@ -56,7 +56,7 @@ const CreateOrganization = z.object({

registry.registerPath({
method: 'post',
path: '/api/v1/organizations',
path: '/rest/v1/organizations',
operationId: 'createOrganization',
summary: '組織を作成',
request: {
Expand Down Expand Up @@ -84,7 +84,7 @@ const UpdateOrganization = CreateOrganization.partial();

registry.registerPath({
method: 'patch',
path: '/api/v1/organizations/{organizationId}',
path: '/rest/v1/organizations/{organizationId}',
operationId: 'updateOrganization',
summary: '組織を作成',
request: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Performer } from '../../../components/schemas/Performer';

registry.registerPath({
method: 'get',
path: '/api/v1/performers/{performerId}',
path: '/rest/v1/performers/{performerId}',
operationId: 'showPerformer',
summary: 'パフォーマーを表示',
request: {
Expand All @@ -26,7 +26,7 @@ registry.registerPath({

registry.registerPath({
method: 'get',
path: '/api/v1/performers',
path: '/rest/v1/performers',
operationId: 'listPerformers',
summary: 'パフォーマーを一覧',
request: {
Expand Down Expand Up @@ -67,7 +67,7 @@ const CreatePerformer = z.object({

registry.registerPath({
method: 'post',
path: '/api/v1/performers',
path: '/rest/v1/performers',
operationId: 'createPerformer',
summary: 'パフォーマーを作成',
request: {
Expand Down Expand Up @@ -105,7 +105,7 @@ const UpdatePerformer = z

registry.registerPath({
method: 'patch',
path: '/api/v1/performers/{performerId}',
path: '/rest/v1/performers/{performerId}',
operationId: 'updatePerformer',
summary: 'パフォーマーを更新',
request: {
Expand Down Expand Up @@ -134,7 +134,7 @@ registry.registerPath({

registry.registerPath({
method: 'post',
path: '/api/v1/performers/{performerId}/subscribe',
path: '/rest/v1/performers/{performerId}/subscribe',
operationId: 'subscribeToPerformer',
summary: 'パフォーマーを購読',
request: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Stream } from '../../../components/schemas/Stream';

registry.registerPath({
method: 'get',
path: '/api/v1/streams/{streamId}',
path: '/rest/v1/streams/{streamId}',
operationId: 'showStream',
summary: '配信を表示',
request: {
Expand All @@ -29,7 +29,7 @@ registry.registerPath({

registry.registerPath({
method: 'post',
path: '/api/v1/streams',
path: '/rest/v1/streams',
operationId: 'createStream',
summary: '配信を作成',
request: {
Expand All @@ -56,7 +56,7 @@ registry.registerPath({

registry.registerPath({
method: 'get',
path: '/api/v1/streams',
path: '/rest/v1/streams',
operationId: 'listStreams',
summary: '配信を一覧',
request: {},
Expand Down
12 changes: 9 additions & 3 deletions packages/@neet/vschedule-api/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
NIJISANJI_YOUTUBE_CHANNEL_ID,
organizations,
} from '@neet/vschedule-seeds';
import { PrismaClient } from '@prisma/client';

import { IOrganizationRepository } from '../src/app/repositories/OrganizationRepository';
import { IPerformerRepository } from '../src/app/repositories/PerformerRepository';
Expand All @@ -16,6 +17,7 @@ import { YoutubeChannelId } from '../src/domain/_shared';
import { container } from '../src/infra/inversify-config';
import { TYPES } from '../src/types';

const prisma = container.get<PrismaClient>(TYPES.PrismaClient);
const createOrganization = container.get(CreateOrganization);
const createPerformer = container.get(CreatePerformer);
const performerRepository = container.get<IPerformerRepository>(
Expand Down Expand Up @@ -105,6 +107,10 @@ const main = async (): Promise<void> => {
}
};

main().catch((error) => {
throw error;
});
main()
.catch((error) => {
throw error;
})
.finally(async () => {
await prisma.$disconnect();
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

import { ShowMediaAttachment } from '../../../../app/use-cases/ShowMediaAttachment';

@controller('/api/v1/media')
@controller('/rest/v1/media')
export class MediaAttachmentController extends BaseHttpController {
constructor(
@inject(ShowMediaAttachment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ListOrganization } from '../../../../app/use-cases/ListOrganizations';
import { ShowOrganization } from '../../../../app/use-cases/ShowOrganization';
import { RestApiPresenter } from '../../../mappers/RestApiMapper';

@controller('/api/v1/organizations')
@controller('/rest/v1/organizations')
export class OrganizationsController extends BaseHttpController {
public constructor(
@inject(ShowOrganization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { SubscribeToPerformer } from '../../../../app/use-cases/SubscribeToYoutu
import { UpdatePerformer } from '../../../../app/use-cases/UpdatePerformer';
import { RestApiPresenter } from '../../../mappers/RestApiMapper';

@controller('/api/v1/performers')
@controller('/rest/v1/performers')
export class PerformersController extends BaseHttpController {
constructor(
@inject(ShowPerformer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ListStreams } from '../../../../app/use-cases/ListStreams';
import { ShowStream } from '../../../../app/use-cases/ShowStream';
import { RestApiPresenter } from '../../../mappers/RestApiMapper';

@controller('/api/v1/streams')
@controller('/rest/v1/streams')
export class StreamsRestApiController extends BaseHttpController {
constructor(
@inject(ListStreams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
RefreshJob,
} from '../../app/repositories/JobRepository';
import { IAppConfig } from '../../app/services/AppConfig/AppConfig';
import { ILogger } from '../../app/services/Logger';
import { TYPES } from '../../types';

@injectable()
Expand All @@ -18,13 +19,16 @@ export class JobRepository implements IJobRepository {
public constructor(
@inject(TYPES.AppConfig)
config: IAppConfig,

@inject(TYPES.Logger)
private readonly _logger: ILogger,
) {
this._origin = config.entries.server.origin;
}

async queue(job: RefreshJob): Promise<void> {
const origin = new URL(this._origin);
origin.pathname = `/api/v1/performers/${job.actorId}/subscribe`;
origin.pathname = `/rest/v1/performers/${job.actorId}/subscribe`;
const url = origin.toString();

await this._tasks.createTask({
Expand All @@ -38,5 +42,7 @@ export class JobRepository implements IJobRepository {
}),
},
});

this._logger.info(`Queued invocation of URL ${url}`, { url, job });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class RestApiPresenter {
public presentMediaAttachment(
mediaAttachment: MediaAttachment,
): Schemas.MediaAttachment {
const pathname = `/api/v1/media/${mediaAttachment.filename.value}`;
const pathname = `/rest/v1/media/${mediaAttachment.filename.value}`;
const url = new URL(this._origin);
url.pathname = pathname;

Expand Down
12 changes: 7 additions & 5 deletions packages/@neet/vschedule-api/src/infra/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import '../adapters/controllers/api/v1/performers';
import '../adapters/controllers/api/v1/organizations';
import '../adapters/controllers/api/v1/media';
import '../adapters/controllers/api/v1/streams';
import '../adapters/controllers/rest/v1/performers';
import '../adapters/controllers/rest/v1/organizations';
import '../adapters/controllers/rest/v1/media';
import '../adapters/controllers/rest/v1/streams';
import '../adapters/controllers/websub/youtube';
import './setup';

import api from '@neet/vschedule-api-spec';
import cors from 'cors';
import express, { Application } from 'express';
import * as OpenApiValidator from 'express-openapi-validator';
import expressWinston from 'express-winston';
Expand Down Expand Up @@ -36,8 +37,9 @@ export const createApp = (container: Container): Application => {
app.use(expressWinston.logger({ winstonInstance: logger }));

app.use('/docs', swaggerUi.serve, swaggerUi.setup(api));
app.use('/rest', cors());
app.use(
'/api',
'/rest',
OpenApiValidator.middleware({
apiSpec: require.resolve('@neet/vschedule-api-spec'),
validateApiSpec: true,
Expand Down
Loading

1 comment on commit 8a94205

@vercel
Copy link

@vercel vercel bot commented on 8a94205 Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.