From ebc3013b4a3ebfb758c7d843a1b17e7386414a00 Mon Sep 17 00:00:00 2001 From: RemRem Date: Tue, 22 Nov 2022 15:12:31 +0100 Subject: [PATCH] define all routes --- bin/server.dart | 33 ++++++++++----------- lib/api.dart | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 lib/api.dart diff --git a/bin/server.dart b/bin/server.dart index 8c053b7..e1886ec 100755 --- a/bin/server.dart +++ b/bin/server.dart @@ -1,27 +1,25 @@ import 'dart:io'; -import 'dart:async'; - +import 'package:r_api/api.dart'; import 'package:shelf/shelf.dart'; import 'package:shelf/shelf_io.dart'; import 'package:shelf_router/shelf_router.dart'; -import 'package:path/path.dart' as path; // Configure routes. final _router = Router() - ..get('/', _rootHandler) - ..get('/echo/', _echoHandler) - ..get('/down/', _fileHandler); - -Response _rootHandler(Request req) { - return Response.ok('Hello, World!\n'); -} - -Response _echoHandler(Request req) { - print(req.url); - final message = req.params['message']; - return Response.ok('$message\n'); -} - + // GET + ..get('/', API.rootHandler) + ..get('/auth', API.authenticator) + ..get('/user/down-password-file', API.downloadPasswordDb) + // POST + ..post('/user/create-account', API.createAccount) + // PUT + ..put('/user/change-master-password', API.changeMasterPassword) + ..put('/user/up-password-file', API.uploadPasswordDb) + ..put('/user/change-mail', API.changeMail) + // DELETE + ..delete('/user/delete-account', API.deleteAccount); + +/* Response _fileHandler(Request req) { final String _basePath = '/home/hel/Projets/r_api/res/'; final String reqFile = path.join(_basePath, req.params['file']); @@ -38,6 +36,7 @@ Future fileExist(String path) { print(exist); return exist; } +*/ void main(List args) async { // Use any available host or container IP (usually `0.0.0.0`). diff --git a/lib/api.dart b/lib/api.dart new file mode 100644 index 0000000..b2fd701 --- /dev/null +++ b/lib/api.dart @@ -0,0 +1,77 @@ +import 'package:shelf/shelf.dart'; +import 'package:shelf_router/shelf_router.dart'; + +class API { + /*---------------| + |-------GET------| + |---------------*/ + + // Default response for / + static Response rootHandler(Request req) { + return Response.ok('Greetings from PassWorld!\n'); + } + + // Request for authentification + // Compare given cyphered_hash_password with db cyphered_hash_password + // Return boolean -> true (hash match) false (no match) + static Response authenticator(Request req) { + final mail = req.params['mail']; + final password = req.params['cyphered_password_hash']; + + return Response.ok('true'); + } + + // Request sqlite password db + // Check auth + // Return sqlite file + static Response downloadPasswordDb(Request req) { + final mail = req.params['mail']; + final password = req.params['cyphered_password_hash']; + + // Database query -> return file (List) + // Create stream from List + // Rename file -> db_password__ + // Send file + + return Response.ok(""); + + /* + Stream> fileStream = file.openRead(); + return Response.ok(fileStream, headers: { + 'Content-Type': 'application/octet-stream', + 'Content-Disposition': 'attachment, filename="$reqFile"' + }); + */ + } + /*---------------| + |------POST------| + |---------------*/ + + static Response createAccount(Request req) { + return Response.ok(""); + } + + /*---------------| + |-------PUT------| + |---------------*/ + + static Response changeMasterPassword(Request req) { + return Response.ok("master password chnaged"); + } + + static Response changeMail(Request req) { + return Response.ok("master password chnaged"); + } + + static Response uploadPasswordDb(Request req) { + return Response.ok(""); + } + + /*---------------| + |-----DELETE-----| + |---------------*/ + + static Response deleteAccount(Request req) { + return Response.ok(""); + } +}