feat: refactor server with shelf_static, return JSON responses, add API docs

This commit is contained in:
dmit.b
2026-05-09 09:12:44 +03:00
parent cd85f5f2db
commit 97db1b6b58
46 changed files with 172153 additions and 24 deletions
+6 -6
View File
@@ -221,7 +221,7 @@ class DatabaseProvider {
final row = results.first;
return Geoposition(
id: int.parse(row[0].toString()),
id: row[0].toString(),
xValue: double.parse(row[1].toString()),
yValue: double.parse(row[2].toString()),
lastUpdate: DateTime.parse(row[3].toString()),
@@ -230,7 +230,7 @@ class DatabaseProvider {
}
Future<Geoposition> updatePosition(
int id,
String id,
double x,
double y,
) async {
@@ -253,7 +253,7 @@ class DatabaseProvider {
final row = results.first;
return Geoposition(
id: int.parse(row[0].toString()),
id: row[0].toString(),
xValue: double.parse(row[1].toString()),
yValue: double.parse(row[2].toString()),
lastUpdate: DateTime.parse(row[3].toString()),
@@ -261,10 +261,10 @@ class DatabaseProvider {
);
}
Future<Geoposition?> getLatestPosition() async {
Future<Geoposition?> getLatestPosition() async {
final results = await _dbConnection.execute(
Sql.named('''
SELECT x_value, y_value, last_update, expires_at
SELECT id, x_value, y_value, last_update, expires_at
FROM geopositions
WHERE expires_at > NOW()
ORDER BY last_update DESC
@@ -276,7 +276,7 @@ class DatabaseProvider {
final row = results.first;
return Geoposition(
id: int.parse(row[0].toString()),
id: row[0].toString(),
xValue: double.parse(row[1].toString()),
yValue: double.parse(row[2].toString()),
lastUpdate: DateTime.parse(row[3].toString()),
+4 -3
View File
@@ -1,6 +1,7 @@
import 'package:shelf/shelf.dart';
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import 'package:dotenv/dotenv.dart';
import 'dart:convert';
class AuthMiddleware {
final Future<Response> Function(Request, String) handler;
@@ -11,7 +12,7 @@ class AuthMiddleware {
final authorization = request.headers['authorization'];
if (authorization == null || !authorization.startsWith('Bearer ')) {
return Response(401, body: 'Authorization header missing or invalid');
return Response(401, body: jsonEncode({'error': 'Authorization header missing or invalid'}), headers: {'Content-Type': 'application/json'});
}
final token = authorization.substring(7);
@@ -25,9 +26,9 @@ class AuthMiddleware {
return handler(request, login);
} on JWTExpiredException {
return Response(401, body: 'Token expired');
return Response(401, body: jsonEncode({'error': 'Token expired'}), headers: {'Content-Type': 'application/json'});
} on JWTException {
return Response(401, body: 'Invalid token');
return Response(401, body: jsonEncode({'error': 'Invalid token'}), headers: {'Content-Type': 'application/json'});
}
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
class Geoposition {
final int id;
final String id;
final double xValue;
final double yValue;
final DateTime lastUpdate;
+6 -6
View File
@@ -31,7 +31,7 @@ class AuthRoutes {
if (user == null || !BCrypt.checkpw(password, user.pwdHash)) {
await database.createLog(login, 'Failed login attempt');
return Response(401, body: 'Invalid credentials');
return Response(401, body: jsonEncode({'error': 'Invalid credentials'}), headers: {'Content-Type': 'application/json'});
}
// Генерация JWT токена
@@ -44,7 +44,7 @@ class AuthRoutes {
final token = jwt.sign(SecretKey(secret));
await database.createLog(login, 'Successful login');
return Response(200, body: jsonEncode(token));
return Response(200, body: jsonEncode({'token': token}), headers: {'Content-Type': 'application/json'});
}
Future<Response> _register(Request request) async {
@@ -56,7 +56,7 @@ class AuthRoutes {
await database.createUser(login, password);
await database.createLog(login, 'User registration');
return Response(201);
return Response(201, body: jsonEncode({'message': 'User registered'}), headers: {'Content-Type': 'application/json'});
}
Future<Response> _watch(Request request, String login) async {
@@ -64,14 +64,14 @@ class AuthRoutes {
if (!database.isValidShareId(uniqueId!)) {
await database.createLog(login, 'Accessed invalid share link');
return Response(404, body: 'Share link not found');
return Response(404, body: jsonEncode({'error': 'Share link not found'}), headers: {'Content-Type': 'application/json'});
}
final position = await database.getLatestPosition();
if (position == null) {
await database.createLog(login, 'Accessed share link - no position');
return Response(404, body: 'No position available');
return Response(404, body: jsonEncode({'error': 'No position available'}), headers: {'Content-Type': 'application/json'});
}
await database.createLog(login, 'Accessed share link');
@@ -80,6 +80,6 @@ class AuthRoutes {
'y': position.yValue,
'last_update': position.lastUpdate,
'expires_at': position.expiresAt,
}));
}), headers: {'Content-Type': 'application/json'});
}
}
+3 -3
View File
@@ -17,7 +17,7 @@ class GeoRoutes {
}
Future<Response> _updatePosition(Request request, String login) async {
final id = int.parse(request.url.queryParameters['id']!);
final id = request.url.queryParameters['id']!;
final body = await request.readAsString();
final data = jsonDecode(body);
@@ -26,7 +26,7 @@ class GeoRoutes {
await database.updatePosition(id, x, y);
await database.createLog(login, 'Updated position id=$id');
return Response(200);
return Response(200, body: jsonEncode({'message': 'Position updated'}), headers: {'Content-Type': 'application/json'});
}
Future<Response> _createShare(Request request, String login) async {
@@ -43,6 +43,6 @@ class GeoRoutes {
return Response(201, body: jsonEncode({
'geo_id': position.id,
'share_id': shareId
}));
}), headers: {'Content-Type': 'application/json'});
}
}
+10 -3
View File
@@ -4,6 +4,8 @@ import 'dart:async';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';
import 'package:shelf_static/shelf_static.dart';
import 'database/database_provider.dart';
import 'routes/auth_routes.dart';
@@ -22,12 +24,17 @@ void main(List<String> args) async {
final router = Router()
..mount('/', authRoutes.routes.call)
..mount('/', geoRoutes.routes.call)
..get('/', (Request req) => Response.ok('Family Safety Tracker API\n'));
..mount('/', geoRoutes.routes.call);
final staticHandler = createStaticHandler('web', defaultDocument: 'index.html');
final handler = Pipeline()
.addMiddleware(corsHeaders())
.addMiddleware(logRequests())
.addHandler(router.call);
.addHandler(Cascade()
.add(staticHandler)
.add(router.call)
.handler);
final ip = InternetAddress.anyIPv4;
final port = int.parse(Platform.environment['PORT'] ?? '9090');