Skip to content

Commit

Permalink
ok datasava to remote
Browse files Browse the repository at this point in the history
  • Loading branch information
ynadtiy19 committed Sep 30, 2024
1 parent 74806eb commit 7193ec1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/libraries/Dart_Packages.xml

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

1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
json_path: ^0.7.2
google_generative_ai: ^0.4.6
beautiful_soup_dart: ^0.3.0
mysql1: ^0.20.0

dev_dependencies:
mocktail: ^1.0.3
Expand Down
78 changes: 78 additions & 0 deletions routes/datasave.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'dart:async';

import 'package:dart_frog/dart_frog.dart';
import 'package:mysql1/mysql1.dart';

// 创建数据库连接
Future<MySqlConnection> connectToDatabase() async {
final settings = ConnectionSettings(
host: '69.165.65.242', // 数据库地址
port: 3306, // 数据库端口
user: 'yunyurest', // 数据库用户名
db: 'yunyurest', // 数据库名
password: 'W2XmNSXm84nGAexG', // 数据库密码
);

try {
final connection = await MySqlConnection.connect(settings);
print('Database connection successful');
return connection;
} catch (e) {
print('Database connection failed: $e');
throw Exception('Failed to connect to the database');
}
}

// 处理请求
Future<Response> onRequest(RequestContext context) async {
//datasave
if (context.request.method == HttpMethod.post) {
final body = await context.request.json();

// 从请求体中获取数据
final key = body['key'];
final deviceId = body['deviceId'];

// 尝试连接到数据库
try {
final conn = await connectToDatabase();

try {
// 插入数据
await conn.query(
'INSERT INTO your_table (key, deviceId) VALUES (?, ?)',
[key, deviceId],
);
print('Data inserted successfully: key=$key, deviceId=$deviceId');

// 返回成功响应
return Response.json(
body: {'message': 'Data added successfully'},
statusCode: 200,
);
} catch (e) {
print('Data insertion failed: $e');
return Response.json(
body: {'error': 'Failed to add data: $e'},
statusCode: 500,
);
} finally {
// 关闭数据库连接
await conn.close();
print('Database connection closed');
}
} catch (e) {
// 捕获数据库连接错误
return Response.json(
body: {'error': 'Database connection failed: $e'},
statusCode: 500,
);
}
}

// 请求方法不支持
return Response.json(
body: {'error': 'Unsupported method'},
statusCode: 405,
);
}

0 comments on commit 7193ec1

Please sign in to comment.