Skip to content

Commit 90ad9a5

Browse files
author
jiangwy
committed
feat: add getVersion service
1 parent 4b5659c commit 90ad9a5

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/controllers/url.controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class UrlController {
2626
}
2727
}
2828

29+
async getVersion(request: Request, env: Env): Promise<Response> {
30+
return this.service.getVersion(request, env);
31+
}
32+
2933
async add(request: Request): Promise<Response> {
3034
try {
3135
const { long_url, serve } = await request.json<{ long_url: string; serve?: string }>();

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default {
2020
.get('/', req => showPage(req, env))
2121
.get('/favicon.ico', () => new Response(null, { status: 200 }))
2222
.get('/sub', req => controller.toSub(req, env))
23+
.get('/version', req => controller.getVersion(req, env))
2324
.post('/api/add', req => controller.add(req))
2425
.delete('/api/delete', req => controller.delete(req))
2526
.get('/api/queryByCode', req => controller.queryByCode(req))

src/services/url.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ShortUrl } from '../types';
22
import { dump } from 'js-yaml';
3+
import { DEFAULT_CONFIG } from '../config';
34
import { Confuse } from '../core/confuse';
45
import { Restore } from '../core/restore';
56
import { ResponseUtil } from '../shared/response';
@@ -49,6 +50,12 @@ export class UrlService {
4950
}
5051
}
5152

53+
async getVersion(request: Request, env: Env): Promise<Response> {
54+
const { searchParams } = new URL(request.url);
55+
const backend = searchParams.get('backend') ?? env.BACKEND ?? DEFAULT_CONFIG.BACKEND;
56+
return Response.redirect(`${backend}/version`, 302);
57+
}
58+
5259
async add(long_url: string, baseUrl: string): Promise<ShortUrl> {
5360
if (!this.db) {
5461
throw new Error('Database is not initialized');

0 commit comments

Comments
 (0)