forked from mito-systems/sol-mem-gen
Use NodeJS global for typeorm
This commit is contained in:
parent
0e3ca90cef
commit
b5c9cb7956
@ -14,6 +14,6 @@ PINATA_JWT=
|
|||||||
PINATA_GATEWAY=
|
PINATA_GATEWAY=
|
||||||
|
|
||||||
# Change to your website URL
|
# Change to your website URL
|
||||||
# For development: set to http://localhost:3000
|
# For development: SITE_URL=http://localhost:3000
|
||||||
SITE_URL=https://memes.markto.market
|
SITE_URL=https://memes.markto.market
|
||||||
NEXT_PUBLIC_ACCOUNT_HANDLE=
|
NEXT_PUBLIC_ACCOUNT_HANDLE=
|
||||||
|
17
server.ts
17
server.ts
@ -1,11 +1,15 @@
|
|||||||
import { createServer } from 'http';
|
import { createServer } from 'http';
|
||||||
import { parse } from 'url';
|
import { parse } from 'url';
|
||||||
import next from 'next';
|
import next from 'next';
|
||||||
|
import { DataSource, EntityTarget } from 'typeorm';
|
||||||
|
|
||||||
// Reference: https://github.com/motdotla/dotenv?tab=readme-ov-file#how-do-i-use-dotenv-with-import
|
// Reference: https://github.com/motdotla/dotenv?tab=readme-ov-file#how-do-i-use-dotenv-with-import
|
||||||
import 'dotenv/config'
|
import 'dotenv/config'
|
||||||
|
|
||||||
import { QuotesService } from './quotes-service';
|
import { QuotesService } from './quotes-service';
|
||||||
|
import { initializeDataSource } from './src/data-source';
|
||||||
|
import { Payment } from './src/entity/Payment';
|
||||||
|
import { Tweet } from './src/entity/Tweet';
|
||||||
|
|
||||||
const port = parseInt(process.env.PORT || '3000', 10);
|
const port = parseInt(process.env.PORT || '3000', 10);
|
||||||
const app = next({ dev: process.env.NODE_ENV !== 'production' });
|
const app = next({ dev: process.env.NODE_ENV !== 'production' });
|
||||||
@ -16,16 +20,25 @@ const quotesService = new QuotesService();
|
|||||||
declare global {
|
declare global {
|
||||||
namespace NodeJS {
|
namespace NodeJS {
|
||||||
interface Global {
|
interface Global {
|
||||||
quotesService: typeof quotesService
|
quotesService: QuotesService
|
||||||
|
appDataSource: DataSource
|
||||||
|
entities: { [key: string]: EntityTarget<any>}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Look for a better way to use quotesService
|
// TODO: Look for a better way to use quotesService
|
||||||
// Initialize global quotes service
|
// Initialize global quotes service
|
||||||
(global as any).quotesService = quotesService
|
global.quotesService = quotesService
|
||||||
|
|
||||||
|
global.entities = {
|
||||||
|
Payment,
|
||||||
|
Tweet
|
||||||
|
};
|
||||||
|
|
||||||
app.prepare().then(async() => {
|
app.prepare().then(async() => {
|
||||||
|
global.appDataSource = await initializeDataSource();
|
||||||
|
|
||||||
const server = createServer(async (req, res) => {
|
const server = createServer(async (req, res) => {
|
||||||
const parsedUrl = parse(req.url!, true);
|
const parsedUrl = parse(req.url!, true);
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ import BN from 'bn.js';
|
|||||||
|
|
||||||
import { fal } from "@fal-ai/client"
|
import { fal } from "@fal-ai/client"
|
||||||
import { FLUX_MODELS } from '../../../services/fluxService'
|
import { FLUX_MODELS } from '../../../services/fluxService'
|
||||||
import { initializeDataSource } from '../../../data-source'
|
|
||||||
import { verifyPayment, markSignatureAsUsed } from '../../../utils/verifyPayment';
|
import { verifyPayment, markSignatureAsUsed } from '../../../utils/verifyPayment';
|
||||||
import { uploadToPinata } from '../../../utils/uploadToPinata';
|
import { uploadToPinata } from '../../../utils/uploadToPinata';
|
||||||
|
|
||||||
@ -22,8 +21,6 @@ const IMAGE_HEIGHT: number = 1024
|
|||||||
|
|
||||||
export async function POST(req: NextRequest): Promise<NextResponse> {
|
export async function POST(req: NextRequest): Promise<NextResponse> {
|
||||||
try {
|
try {
|
||||||
await initializeDataSource();
|
|
||||||
|
|
||||||
const { prompt, modelId, transactionSignature } = await req.json();
|
const { prompt, modelId, transactionSignature } = await req.json();
|
||||||
const host = req.headers.get("host"); // Get the hostname from request headers
|
const host = req.headers.get("host"); // Get the hostname from request headers
|
||||||
const protocol = req.headers.get("x-forwarded-proto") || "http"; // Handle reverse proxies
|
const protocol = req.headers.get("x-forwarded-proto") || "http"; // Handle reverse proxies
|
||||||
|
@ -1,13 +1,9 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
import { saveTweet, verifySignatureInTweet } from '../../../utils/verifyTweet';
|
import { saveTweet, verifySignatureInTweet } from '../../../utils/verifyTweet';
|
||||||
import { initializeDataSource } from '../../../data-source';
|
|
||||||
|
|
||||||
export async function POST(req: NextRequest): Promise<NextResponse> {
|
export async function POST(req: NextRequest): Promise<NextResponse> {
|
||||||
try {
|
try {
|
||||||
// TODO: Move initialization to server file
|
|
||||||
await initializeDataSource();
|
|
||||||
|
|
||||||
const { tweetUrl } = await req.json();
|
const { tweetUrl } = await req.json();
|
||||||
|
|
||||||
const url = `https://publish.twitter.com/oembed?url=${tweetUrl}&maxwidth=600`;
|
const url = `https://publish.twitter.com/oembed?url=${tweetUrl}&maxwidth=600`;
|
||||||
|
@ -1,24 +1,21 @@
|
|||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
|
|
||||||
import { Payment } from './entity/Payment';
|
export async function initializeDataSource() {
|
||||||
|
try {
|
||||||
|
console.log('Initializing Data Source');
|
||||||
|
|
||||||
export const AppDataSource = new DataSource({
|
const appDataSource = new DataSource({
|
||||||
type: 'sqlite',
|
type: 'sqlite',
|
||||||
database: './database.sqlite',
|
database: './database.sqlite',
|
||||||
synchronize: true,
|
synchronize: true,
|
||||||
logging: false,
|
logging: false,
|
||||||
// entities: [__dirname + "./entity/*{.js,.ts}"],
|
entities: [global.entities.Payment, global.entities.Tweet],
|
||||||
entities: [Payment],
|
|
||||||
migrations: [],
|
migrations: [],
|
||||||
subscribers: [],
|
subscribers: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
export async function initializeDataSource() {
|
await appDataSource.initialize();
|
||||||
try {
|
return appDataSource;
|
||||||
if (!AppDataSource.isInitialized){
|
|
||||||
await AppDataSource.initialize();
|
|
||||||
console.log('Data Source has been initialized!');
|
|
||||||
};
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error during Data Source initialization:', err);
|
console.error('Error during Data Source initialization:', err);
|
||||||
throw err;
|
throw err;
|
||||||
|
@ -4,7 +4,6 @@ import BN from 'bn.js';
|
|||||||
import { Connection } from '@solana/web3.js';
|
import { Connection } from '@solana/web3.js';
|
||||||
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
||||||
|
|
||||||
import { AppDataSource } from '../data-source';
|
|
||||||
import { Payment } from '../entity/Payment';
|
import { Payment } from '../entity/Payment';
|
||||||
|
|
||||||
assert(process.env.NEXT_PUBLIC_SOLANA_RPC_URL, 'SOLANA_RPC_URL is required');
|
assert(process.env.NEXT_PUBLIC_SOLANA_RPC_URL, 'SOLANA_RPC_URL is required');
|
||||||
@ -22,7 +21,7 @@ const connection = new Connection(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export async function isSignatureUsed(transactionSignature: string): Promise<boolean> {
|
export async function isSignatureUsed(transactionSignature: string): Promise<boolean> {
|
||||||
const paymentRepository = AppDataSource.getRepository(Payment);
|
const paymentRepository = global.appDataSource.getRepository(global.entities.Payment);
|
||||||
const payment = await paymentRepository.findOneBy({ transactionSignature });
|
const payment = await paymentRepository.findOneBy({ transactionSignature });
|
||||||
if (payment) {
|
if (payment) {
|
||||||
return true;
|
return true;
|
||||||
@ -31,8 +30,8 @@ export async function isSignatureUsed(transactionSignature: string): Promise<boo
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function markSignatureAsUsed(transactionSignature: string): Promise<void> {
|
export async function markSignatureAsUsed(transactionSignature: string): Promise<void> {
|
||||||
await AppDataSource.transaction(async (transactionalEntityManager) => {
|
await global.appDataSource.transaction(async (transactionalEntityManager) => {
|
||||||
const paymentRepository = transactionalEntityManager.getRepository(Payment);
|
const paymentRepository = transactionalEntityManager.getRepository(global.entities.Payment);
|
||||||
|
|
||||||
// Check if the payment with the given signature already exists
|
// Check if the payment with the given signature already exists
|
||||||
const exists = await paymentRepository.exists({ where: { transactionSignature } });
|
const exists = await paymentRepository.exists({ where: { transactionSignature } });
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
import { AppDataSource } from '../data-source';
|
|
||||||
import { Payment } from '../entity/Payment';
|
|
||||||
import { Tweet } from '../entity/Tweet';
|
import { Tweet } from '../entity/Tweet';
|
||||||
|
|
||||||
export async function verifySignatureInTweet(transactionSignature: string): Promise<boolean> {
|
export async function verifySignatureInTweet(transactionSignature: string): Promise<boolean> {
|
||||||
const paymentRepository = AppDataSource.getRepository(Payment);
|
const paymentRepository = global.appDataSource.getRepository(global.entities.Payment);
|
||||||
const payment = await paymentRepository.findOneBy({ transactionSignature });
|
const payment = await paymentRepository.findOneBy({ transactionSignature });
|
||||||
|
|
||||||
if (!payment) {
|
if (!payment) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tweetRepository = AppDataSource.getRepository(Tweet);
|
const tweetRepository = global.appDataSource.getRepository(global.entities.Tweet);
|
||||||
const tweet = await tweetRepository.findOneBy({ transactionSignature });
|
const tweet = await tweetRepository.findOneBy({ transactionSignature });
|
||||||
|
|
||||||
if (tweet) {
|
if (tweet) {
|
||||||
@ -21,8 +19,8 @@ export async function verifySignatureInTweet(transactionSignature: string): Prom
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function saveTweet(data: Partial<Tweet>): Promise<{ isFourthUser: boolean }> {
|
export async function saveTweet(data: Partial<Tweet>): Promise<{ isFourthUser: boolean }> {
|
||||||
return await AppDataSource.transaction(async (transactionalEntityManager) => {
|
return await global.appDataSource.transaction(async (transactionalEntityManager) => {
|
||||||
const tweetRepository = transactionalEntityManager.getRepository(Tweet);
|
const tweetRepository = transactionalEntityManager.getRepository(global.entities.Tweet);
|
||||||
|
|
||||||
const tweet = await tweetRepository.save(data);
|
const tweet = await tweetRepository.save(data);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user