Skip to content

Commit

Permalink
feat(server): connect to redis
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jan 15, 2024
1 parent e061549 commit 2ab74e0
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"animegarden": "workspace:*",
"anitomy": "0.0.34",
"drizzle-orm": "^0.29.3",
"postgres": "^3.4.3"
"postgres": "^3.4.3",
"unstorage": "^1.10.1"
},
"devDependencies": {
"dotenv": "^16.3.1"
Expand Down
2 changes: 2 additions & 0 deletions packages/database/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export * from './schema';

export * from './migrate';

export * from './redis';

export * from './connection';

export * from './operations';
11 changes: 11 additions & 0 deletions packages/database/src/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createStorage } from 'unstorage';
import redisDriver from 'unstorage/drivers/redis';

export function connectRedis(url: string) {
return createStorage({
driver: redisDriver({
base: 'unstorage',
url
})
});
}
13 changes: 11 additions & 2 deletions packages/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { format } from 'node:util';

import { Hono } from 'hono';

import { cors } from 'hono/cors';
import { prettyJSON } from 'hono/pretty-json';
import { logger as honoLogger } from 'hono/logger';

import { logger } from './logger';
Expand All @@ -12,12 +14,19 @@ export async function registerApp(fn: (hono: typeof app) => Promise<void>) {
await fn(app);
}

app.all(
app.use('*', cors());
app.use('*', prettyJSON());
app.use(
'*',
honoLogger((message: string, ...rest: string[]) => {
const content = format(message, ...rest);
logger.info('request', content);
})
);

app.get('/', (c) => c.text('Hello AnimeGarden!'));
app.get('/', async (c) => {
return c.json({
message: 'AnimeGarden - 動漫花園 3-rd party mirror site',
timestamp: new Date().toString()
});
});
5 changes: 3 additions & 2 deletions packages/server/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type Database, connectDatabase } from '@animegarden/database';

const DATABASE_URI = process.env.POSTGRES_URI ?? process.env.DATABASE_URI;
export const DATABASE_URI = process.env.POSTGRES_URI ?? process.env.DATABASE_URI;

if (!DATABASE_URI) {
console.log(`Can not find database connection string`);
console.log(`Can not find postgres connection string`);
process.exit(1);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { serve } from '@hono/node-server';

import { app } from './app';
import { logger } from './logger';
import { storage } from './storage';
import { database } from './database';

serve(
Expand All @@ -14,5 +15,8 @@ serve(

await database.query.resources.findFirst();
logger.info(null, `Connect to postgres`);

await storage.getItem(`state/refresh-timestamp`);
logger.info(null, `Connect to redis`);
}
);
10 changes: 10 additions & 0 deletions packages/server/src/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { connectRedis } from '@animegarden/database';

export const REDIS_URI = process.env.REDIS_URI;

if (!REDIS_URI) {
console.log(`Can not find redis connection string`);
process.exit(1);
}

export const storage = connectRedis(REDIS_URI);
1 change: 0 additions & 1 deletion packages/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ app.use('*', logger());
app.use('*', prettyJSON());

app.get('/', async (c) => {
console.log('Req', c.env);
return c.json({
message: 'AnimeGarden - 動漫花園 3-rd party mirror site',
timestamp: await getRefreshTimestamp(c.env)
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ab74e0

Please sign in to comment.