Frontend WIP - just websocket support left
This commit is contained in:
@@ -24,6 +24,7 @@ class Authenticator {
|
||||
|
||||
final iat = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
final jwt = JWT(
|
||||
header: {'algo': 'HS256'},
|
||||
JWTBody(uuid: newUser.uuid, roomUuid: newUser.gameRoomUuid, iat: iat, exp: iat + expTimeSecs).toJson(),
|
||||
);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ final log = Logger('Environment');
|
||||
|
||||
bool _isDevEnv = false;
|
||||
|
||||
void checkEnvironment(bool isDevEnv) {
|
||||
void checkEnvironment({required bool isDevEnv}) {
|
||||
_isDevEnv = isDevEnv;
|
||||
getJWTSecret();
|
||||
}
|
||||
@@ -46,7 +46,9 @@ String? getJWTSecret() {
|
||||
if (_isDevEnv) {
|
||||
log.warning('JWT secret not configured. Define JWT_TOKEN_SECRET in environment.');
|
||||
final secret = List.generate(
|
||||
24, (_) => String.fromCharCode((65 + Random().nextInt(26)) + (Random().nextInt(2) == 0 ? 0 : 32))).join();
|
||||
24,
|
||||
(_) => String.fromCharCode((65 + Random().nextInt(26)) + (Random().nextInt(2) == 0 ? 0 : 32)),
|
||||
).join();
|
||||
log.warning('Generated random JWT secret for development: $secret');
|
||||
return secret;
|
||||
} else {
|
||||
|
||||
+3
-46
@@ -3,7 +3,7 @@ import 'dart:io';
|
||||
|
||||
import 'package:backend/utils/environment.dart';
|
||||
import 'package:dart_frog/dart_frog.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:shared_models/fart_logger.dart';
|
||||
|
||||
bool _listening = false;
|
||||
|
||||
@@ -12,54 +12,11 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
|
||||
// Logic to prevent multiple listeners with hot-reload
|
||||
// Changes to this are not hot-reloaded, you must restart the server
|
||||
if (!_listening) {
|
||||
final logLevel = Platform.environment['LOG_LEVEL'] ?? (isDevelopment ? 'FINEST' : 'INFO');
|
||||
Logger.root.level =
|
||||
Level.LEVELS.firstWhere((l) => l.name == logLevel, orElse: () => Level.INFO); // defaults to Level.INFO
|
||||
Logger.root.onRecord.listen((record) {
|
||||
writeLogRecord(record, record.level.value >= Level.SEVERE.value ? stderr : stdout);
|
||||
});
|
||||
FartLogger.listen(isDevelopment: isDevelopment);
|
||||
_listening = true;
|
||||
}
|
||||
|
||||
checkEnvironment(isDevelopment);
|
||||
|
||||
for (final lvl in Level.LEVELS) {
|
||||
writeLogRecord(LogRecord(lvl, 'Test message', 'main'), stdout);
|
||||
}
|
||||
checkEnvironment(isDevEnv: isDevelopment);
|
||||
|
||||
return serve(handler, ip, port);
|
||||
}
|
||||
|
||||
const Map<String, String> _levelColors = {
|
||||
'FINEST': '\x1B[1;37m', // White
|
||||
'FINER': '\x1B[1;38m', // Gray
|
||||
'FINE': '\x1B[1;35m', // Purple
|
||||
'CONFIG': '\x1B[1;36m', // Cyan
|
||||
'INFO': '\x1B[1;32m', // Green
|
||||
'WARNING': '\x1B[1;33m', // Yellow
|
||||
'SEVERE': '\x1B[1;31m', // Red
|
||||
'SHOUT': '\x1B[1;38;5;52m\x1B[1;48;5;213m', // Red on pink
|
||||
};
|
||||
|
||||
const String _resetColor = '\x1B[0m';
|
||||
|
||||
String _getColoredLevel(String levelName) {
|
||||
return '${_levelColors[levelName] ?? ''}$levelName$_resetColor';
|
||||
}
|
||||
|
||||
void writeLogRecord(LogRecord record, IOSink iosink) {
|
||||
// Write the basic log message with colored level
|
||||
iosink.writeln(
|
||||
'[${_getColoredLevel(record.level.name)}]:[${record.loggerName}] '
|
||||
'${record.time}: ${record.message}',
|
||||
);
|
||||
|
||||
// Additional details for severe logs
|
||||
if (record.level.value >= Level.SEVERE.value) {
|
||||
iosink.writeln(
|
||||
'[${_getColoredLevel(record.level.name)}]:[${record.loggerName}] '
|
||||
'${record.error?.toString() ?? "No error provided"}\n'
|
||||
'${record.stackTrace?.toString() ?? "No trace provided"}',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ Future<Response> onRequest(RequestContext context) async {
|
||||
|
||||
try {
|
||||
// Parse the request body
|
||||
final body = await context.request.json() as Map<String, dynamic>;
|
||||
final createUserReq = CreateUserRequest.fromJson(body);
|
||||
final body = await context.request.json();
|
||||
final createUserReq = CreateUserRequest.fromJson(body as Map<String, dynamic>);
|
||||
|
||||
// Generate token
|
||||
final authenticator = context.read<Authenticator>();
|
||||
|
||||
@@ -3,6 +3,6 @@ environment:
|
||||
sdk: ">=3.0.0 <4.0.0"
|
||||
|
||||
dev_dependencies:
|
||||
test: ^1.24.0
|
||||
http: ^1.1.0
|
||||
test: ^1.24.0
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ void main() async {
|
||||
final sub = serverLogs.length > 10 ? serverLogs.length - 10 : 0;
|
||||
stdout.write("Server logs:\n${serverLogs.sublist(sub).join('\n')}");
|
||||
} else {
|
||||
stdout.writeln("💨 Passed like a light breeze 😮💨");
|
||||
stdout.writeln('💨 Passed like a light breeze 😮💨');
|
||||
}
|
||||
|
||||
// Exit with the same code as the test process
|
||||
|
||||
@@ -7,7 +7,7 @@ environment:
|
||||
dependencies:
|
||||
backend:
|
||||
path: ..
|
||||
http: ^1.1.0
|
||||
shared_models:
|
||||
path: ../../shared_models
|
||||
test: ^1.24.0
|
||||
http: ^1.1.0
|
||||
|
||||
Reference in New Issue
Block a user