I am attempting to create a very simple http server that does one thing. Upon receiving an HttpRequest, it runs a query on the local database server, and returns a string based on that query.
I am learning Dart, and I am having trouble grasping Futures. I thought I understood them, but this example leads me to believe I really have no idea how they work. So, not only am I looking for a solution to this problem, but any pointers I will gladly accept as well.
Note: This code is a very primitive example of what I have been trying to accomplish, and for the sake of reaching out to the Stackoverflow community, I have shortened / simplified it as much as possible, while keeping the problem intact.
Here is my server.dart code
import 'dart:io';
import 'package:sqljocky/sqljocky.dart';
final connection = new ConnectionPool(host: 'localhost', port: 3306, user: 'root', password: null, db: 'server1');
main() {
HttpServer.bind(InternetAddress.ANY_IP_V4, 9090)..then((server) {
print("serving generic database query on localhost:9090");
server.listen((request) {
if (request.method == "GET") {
request.response.write(getResults());
request.response.close();
}
else {
request.response.statusCode = HttpStatus.BAD_REQUEST;
}
});
});
}
String getResults() {
StringBuffer sb = new StringBuffer();
sb.write("START--");
connection.query("select name, email, zkey from users")
..then((results) {
results.forEach((row) {
sb.write(row.toString());
print(row.toString());
});
});
sb.write("--END");
print(sb.toString());
return sb.toString();
}
So if I send a request to this server it returns "START----END". The server prints out the expected query result, and then prints out "START----END". This leads me to believe that my request response is closing and returning before the query result is done processing.
So whether I curl localhost:9090/asdf or actually build a client http request sender, I do not get the response I am expecting... which is a database query result.
Thanks in advance