yourname пре 6 месеци
родитељ
комит
2dc0a2336e

+ 36 - 41
src/server/api.ts

@@ -1,47 +1,42 @@
 import { OpenAPIHono } from '@hono/zod-openapi'
-import { cors } from 'hono/cors'
-import { logger } from 'hono/logger'
 import { errorHandler } from './middleware/errorHandler'
 import { authMiddleware } from './middleware/auth.middleware'
 import { checkPermission } from './middleware/permission.middleware'
 import base from './api/base'
 import { userOpenApiApp } from './api/user'
-import { authOpenApiApp } from './api/auth'
-
-const app = new OpenAPIHono()
-
-// Middleware chain
-app.use('*', logger())
-app.use('*', cors(
-  // {
-  //   origin: ['http://localhost:3000'],
-  //   allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
-  //   credentials: true
-  // }
-))
-app.use('/api/v1/*', authMiddleware)
-app.onError(errorHandler)
-
-// Rate limiting
-app.use('/api/v1/*', async (c, next) => {
-  const ip = c.req.header('x-forwarded-for') || c.req.header('cf-connecting-ip')
-  // 实现速率限制逻辑
-  await next()
-})
-
-// Register routes
-app.route('/api/v1', base)
-app.route('/api/v1/users', userOpenApiApp)
-app.route('/api/v1/auth', new AuthController().routes())
-
-// OpenAPI documentation endpoint
-app.doc('/doc', {
-  openapi: '3.1.0',
-  info: {
-    title: 'API Documentation',
-    version: '1.0.0'
-  },
-  servers: [{ url: '/api/v1' }]
-})
-
-export default app
+// import { authOpenApiApp } from './api/auth'
+
+// const app = new OpenAPIHono()
+
+const createApi = (app:OpenAPIHono) => {
+
+  // app.use('/api/v1/*', authMiddleware)
+  app.onError(errorHandler)
+
+  // Rate limiting
+  app.use('/api/v1/*', async (c, next) => {
+    const ip = c.req.header('x-forwarded-for') || c.req.header('cf-connecting-ip')
+    // 实现速率限制逻辑
+    await next()
+  })
+
+  // Register routes
+  app.route('/api/v1', base)
+  app.route('/api/v1', userOpenApiApp)
+  // app.route('/api/v1/auth', new AuthController().routes())
+
+  // OpenAPI documentation endpoint
+  app.doc('/doc', {
+    openapi: '3.1.0',
+    info: {
+      title: 'API Documentation',
+      version: '1.0.0'
+    },
+    // servers: [{ url: '/api/v1' }]
+  })
+
+  return app;
+
+}
+
+export default createApi

+ 20 - 1
src/server/index.tsx

@@ -1,9 +1,28 @@
+// import "reflect-metadata"
 import { Hono } from 'hono'
+import { OpenAPIHono } from '@hono/zod-openapi'
+import { cors } from 'hono/cors'
+import { logger } from 'hono/logger'
 import { swaggerUI } from '@hono/swagger-ui'
 import { renderer } from './renderer'
-import app from './api'
+import { AppDataSource } from './data-source'
+import createApi from './api'
 // const app = new Hono()
 
+if(!AppDataSource.isInitialized) await AppDataSource.initialize();
+
+const app = new OpenAPIHono()
+// Middleware chain
+app.use('*', logger())
+app.use('*', cors(
+  // {
+  //   origin: ['http://localhost:3000'],
+  //   allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
+  //   credentials: true
+  // }
+))
+createApi(app);
+
 app.use(renderer)
 
 app.get('/ui', swaggerUI({ url: '/doc' }))

+ 2 - 1
src/server/middleware/errorHandler.ts

@@ -6,7 +6,8 @@ export const errorHandler = async (err: Error, c: Context) => {
     return c.json(
       { 
         error: err.message,
-        status: err.status 
+        status: err.status ,
+        cause: err.cause
       },
       err.status
     )

+ 3 - 3
src/server/modules/users/role.entity.ts

@@ -5,11 +5,11 @@ export class Role {
   @PrimaryGeneratedColumn()
   id!: number;
 
-  @Column({ unique: true })
+  @Column({ type: 'varchar', length: 50, unique: true })
   name!: string;
 
-  @Column()
-  description!: string;
+  @Column({ type: 'text', nullable: true })
+  description?: string;
 
   constructor(partial?: Partial<Role>) {
     Object.assign(this, partial);

+ 3 - 3
src/server/modules/users/user.entity.ts

@@ -6,13 +6,13 @@ export class User {
   @PrimaryGeneratedColumn()
   id!: number;
 
-  @Column({ unique: true })
+  @Column({ unique: true, type: 'varchar', length: 50 })
   username!: string;
 
-  @Column()
+  @Column({ type: 'varchar' })
   password!: string;
 
-  @Column({ unique: true })
+  @Column({ unique: true, type: 'varchar', length: 100 })
   email!: string;
 
   @ManyToMany(() => Role)

+ 2 - 1
src/server/modules/users/user.service.ts

@@ -1,3 +1,4 @@
+import { HTTPException } from 'hono/http-exception'
 import { AppDataSource } from '../../data-source';
 import { User } from './user.entity';
 import * as bcrypt from 'bcrypt';
@@ -100,7 +101,7 @@ export class UserService {
       return users;
     } catch (error) {
       console.error('Error getting users:', error);
-      throw new Error('Failed to get users');
+      throw new HTTPException(500, { message: 'Failed to get users', cause: error })
     }
   }