Files
mess_api/bin/mess_api.dart
2026-01-29 14:09:06 +09:00

52 lines
1.2 KiB
Dart

import 'dart:io';
import 'package:mysql1/mysql1.dart';
import 'package:mess_api/config.dart';
Future<void> testDbConnection() async {
final settings = ConnectionSettings(
host: DBConfig.dbHost,
port: DBConfig.dbPort,
user: DBConfig.dbUser,
password: DBConfig.dbPassword,
db: DBConfig.dbName,
);
final conn = await MySqlConnection.connect(settings);
final results = await conn.query(
'SELECT test_number FROM test_table LIMIT 1',
);
if (results.isEmpty) {
throw Exception('DB CONNECT FAIL: no data');
}
final value = results.first['test_number'];
if (value != 777) {
throw Exception('DB CONNECT FAIL: value=$value');
}
print('DB CONNECT OK (777)');
await conn.close();
}
void main() async {
await testDbConnection();
final server = await HttpServer.bind(
InternetAddress.anyIPv4,
AppConfig.port,
);
print('API server running on http://localhost:${AppConfig.port}');
await for (HttpRequest request in server) {
request.response
..statusCode = HttpStatus.ok
..write('ok')
..close();
}
}