diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 5529ef7..a7a321f 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -40,6 +40,7 @@ class Kernel extends HttpKernel 'api' => [ \App\Http\Middleware\ValidateHeaders::class, + \App\Http\Middleware\CustomCors::class, \Illuminate\Routing\Middleware\ThrottleRequests::class.':api', \Illuminate\Routing\Middleware\SubstituteBindings::class, ], diff --git a/app/Http/Middleware/CustomCors.php b/app/Http/Middleware/CustomCors.php new file mode 100644 index 0000000..7f8b57a --- /dev/null +++ b/app/Http/Middleware/CustomCors.php @@ -0,0 +1,36 @@ +getMethod() === 'OPTIONS') { + $response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE, PATCH'); + $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, X-API-Version, X-Client-ID, Authorization, X-Requested-With, Accept'); + $response->headers->set('Access-Control-Max-Age', '86400'); + } + + // 允許所有域名 + $response->headers->set('Access-Control-Allow-Origin', '*'); + + // 當允許所有域名時,不能設置 credentials + // $response->headers->set('Access-Control-Allow-Credentials', 'true'); + + return $response; + } +} diff --git a/config/cors.php b/config/cors.php new file mode 100644 index 0000000..92e3c6c --- /dev/null +++ b/config/cors.php @@ -0,0 +1,19 @@ + ['api/*'], + 'allowed_methods' => ['*'], + 'allowed_origins' => ['*'], + 'allowed_origins_patterns' => [], + 'allowed_headers' => [ + 'Content-Type', + 'X-Requested-With', + 'Authorization', + 'X-API-Version', + 'X-Client-ID', + 'Accept', + ], + 'exposed_headers' => [], + 'max_age' => 0, + 'supports_credentials' => true, +]; diff --git a/doc/llmapiv2.html b/doc/llmapiv2.html deleted file mode 100644 index 587bc50..0000000 --- a/doc/llmapiv2.html +++ /dev/null @@ -1,339 +0,0 @@ - - - - - - LLM API Documentation v2 - - - -

LLM API Documentation v2

- -

Overview

-

This document describes the API endpoints for the LLM service.

- -

Base URL

-
https://llmbackend.local:7890
- -

Authentication

- - -

Required Headers

-

All API requests must include:

-
Content-Type: application/json
-Accept: application/json
-X-API-Version: 1.0
-X-Client-ID: your-client-id
- -

Endpoints

- -

Public Routes

- -
-

GET /

-

Root endpoint that returns the API status.

-
Response:
-
{
-    "success": true,
-    "data": {
-        "status": "ok",
-        "version": "1.0"
-    }
-}
-
- -
-

GET /api/test

-

Simple test endpoint to verify API connectivity.

-
- -
-

POST /api/auth/token

-

Exchange auth token for an access token.

-
Request Body:
-
{
-    "auth_token": "64-character-auth-token"
-}
-
Response:
-
{
-    "success": true,
-    "data": {
-        "access_token": "generated-access-token",
-        "expires_in": 3600
-    }
-}
-
- -

Protected Client Routes

- -
-

POST /api/llm/request

-

Make a request to the LLM service.

-
Headers:
-
Authorization: Bearer {access_token}
-
Request Body:
-
{
-    "prompt": "Your prompt text here",
-    "max_tokens": 100,
-    "temperature": 0.7,
-    "top_p": 1,
-    "frequency_penalty": 0,
-    "presence_penalty": 0
-}
-
Response:
-
{
-    "success": true,
-    "data": {
-        "response": "LLM generated response"
-    }
-}
-
- -

Admin Authentication

- -
-

POST /api/admin/login

-
Request Body:
-
{
-    "email": "your-email",
-    "password": "your-password"
-}
-
Response:
-
{
-    "success": true,
-    "data": {
-        "token": "admin-bearer-token",
-        "admin": {
-            "id": 1,
-            "email": "your-email"
-        }
-    }
-}
-
- -
-

POST /api/admin/logout

-
Headers:
-
Authorization: Bearer {admin_token}
-
- -
-

PUT /api/admin/change-password

-
Headers:
-
Authorization: Bearer {admin_token}
-
Request Body:
-
{
-    "current_password": "current-password",
-    "new_password": "new-password",
-    "new_password_confirmation": "new-password"
-}
-
- -

Client Management

- -
-

GET /api/admin/clients

-
Headers:
-
Authorization: Bearer {admin_token}
-
Response:
-
{
-    "success": true,
-    "data": {
-        "items": [
-            {
-                "id": 1,
-                "name": "Client Name",
-                "llm_provider_id": 1,
-                "created_at": "2024-12-05T00:00:00Z"
-            }
-        ]
-    }
-}
-
- -
-

POST /api/admin/clients

-
Headers:
-
Authorization: Bearer {admin_token}
-
Request Body:
-
{
-    "name": "New Client Name",
-    "llm_provider_id": 1
-}
-
- -
-

GET /api/admin/clients/{client_id}

-
Headers:
-
Authorization: Bearer {admin_token}
-
- -
-

PUT /api/admin/clients/{client_id}

-
Headers:
-
Authorization: Bearer {admin_token}
-
Request Body:
-
{
-    "name": "Updated Client Name",
-    "llm_provider_id": 1
-}
-
- -
-

DELETE /api/admin/clients/{client_id}

-
Headers:
-
Authorization: Bearer {admin_token}
-
- -
-

POST /api/admin/clients/{client_id}/auth-token

-
Headers:
-
Authorization: Bearer {admin_token}
-
Response:
-
{
-    "success": true,
-    "data": {
-        "client_id": 1,
-        "auth_token": "generated-auth-token",
-        "created_at": "2024-12-05T00:00:00Z"
-    }
-}
-
- -

LLM Provider Management

- -
-

GET /api/admin/llm-providers

-
Headers:
-
Authorization: Bearer {admin_token}
-
Response:
-
{
-    "success": true,
-    "data": {
-        "items": [
-            {
-                "id": 1,
-                "name": "OpenAI",
-                "service_name": "openai",
-                "api_url": "https://api.openai.com/v1",
-                "status": "active",
-                "created_at": "2024-12-05T00:00:00Z"
-            }
-        ]
-    }
-}
-
- -
-

POST /api/admin/llm-providers

-
Headers:
-
Authorization: Bearer {admin_token}
-
Request Body:
-
{
-    "name": "OpenAI",
-    "service_name": "openai",
-    "api_url": "https://api.openai.com/v1",
-    "api_token": "your-api-token",
-    "status": "active"
-}
-
- -
-

GET /api/admin/llm-providers/{provider_id}

-
Headers:
-
Authorization: Bearer {admin_token}
-
- -
-

PUT /api/admin/llm-providers/{provider_id}

-
Headers:
-
Authorization: Bearer {admin_token}
-
Request Body:
-
{
-    "name": "Updated OpenAI",
-    "service_name": "openai",
-    "api_url": "https://api.openai.com/v1",
-    "api_token": "your-api-token",
-    "status": "active"
-}
-
- -
-

DELETE /api/admin/llm-providers/{provider_id}

-
Headers:
-
Authorization: Bearer {admin_token}
-
- -

Error Responses

-

All endpoints return standardized error responses:

-
{
-    "success": false,
-    "error": "error_code",
-    "message": "Error message",
-    "errors": {
-        "field": ["Error details"]
-    }
-}
- -

Success Responses

-

All successful responses follow the format:

-
{
-    "success": true,
-    "data": {
-        // Response data
-    }
-}
- -