I am under the impression that a DART program cannot be hosted in a webserver. Could someone please enlighten me on this?
Yes, it can (although that's not its primary use case).
From Google Plus, 28th Feb 2013:
Finally I managed to make Dart work in Apache CGI ! I didn't find any information about this, so I tryed by myself. Here is how I did it (Apache 2.2, and Ubuntu) ...
From news.dartlang.org, 26th May, 2012
Today, Sam McCall announced mod_dart: the ability to run Dart apps embedded in Apache! Just like PHP, Perl, Python, and many other scripting languages, you can now use Dart to power your server-side web apps from inside the Apache web server.
Both these are "proof of concepts", but they show that Dart can be embedded within a web server such as Apache.
Now the "but..."
Although it's proven that Dart can be embedded within a webserver, Dart is more like node.js, in that the server side dart binary provides a VM for an application to use. That application could include its own webserver, for example:
main() {
var server = new HttpServer();
server.addRequestHandler(
(req) => true, // matcher - should this function handle this request?
(req, res) { // handler - what should happen when this request matches?
res.outputStream.write("${req.method}: ${req.path}"); // eg: GET: /foo
res.outputStream.close();
});
server.listen('127.0.0.1', 8080);
Example Apache CGI in Dartlang 2.3:
Copy "dartaotruntime" to "/var/www/your-site/cgi-bin"
$ chown apache:apache dartaotruntime
Create a file test.cgi
#!/bin/sh
BASE=/var/www/your-site/cgi-bin
$BASE/dartaotruntime $BASE/test.aot
$ chmod 0755 test.cgi
$ chown apache:apache test.cgi
Create a file test.dart
import 'dart:io';
void main(List<String> args) {
Map<String, String> envVars = Platform.environment;
print("Content-Type: text/html\n");
String input = "";
if (envVars['REQUEST_METHOD'] == 'POST') {
var content_length = int.parse(envVars['CONTENT_LENGTH']);
while(input.length < content_length) {
input += stdin.readLineSync();
}
}
print("""
<html>
<form method="post" action="test.cgi">
Name: <input type="text" name="name" value="" />
Email: <input type="text" name="email" value="" />
<input type="submit" value="Submit" />
</form>
<p><strong>ENV:</strong> {$envVars}</p>
<p><strong>INPUT:</strong> {$input}</p>
</html>""");
}
$ dart2aot test.dart test.aot
$ chown apache:apache test.aot
Run the cgi: https://www.your-site.com/cgi-bin/test.cgi
You can run dart like a perl script using cgi-bin apache environment. I test it on my Mac with XAMPP and it works:
#!/usr/local/bin/dart
import 'dart:io';
void main() {
Map<String, String> envVars = Platform.environment;
print("Content-Type: text/html\n");
String input = "";
if (envVars['REQUEST_METHOD'] == 'POST') {
var content_length = int.parse(envVars['CONTENT_LENGTH']);
while(input.length < content_length) {
input += stdin.readLineSync();
}
}
print("""
<html>
<form method="post">
Name: <input type="text" name="name" value="" />
Email: <input type="text" name="email" value="" />
<input type="submit" value="Submit" />
</form>
<p><strong>QUERY_STRING:</strong> ${envVars['QUERY_STRING']}</p>
<p><strong>ENV:</strong> {$envVars}</p>
<p><strong>INPUT:</strong> {$input}</p>
</html>
"""
);
}
Initially some errors appeared in the error_log, the xampp libraries were not working. I just had to copy from the place indicated in the log to the lib directory of xampp and it works normally.
==> /Applications/XAMPP/xamppfiles/logs/error_log <== [Sun Feb 23 14:08:12.799968 2020] [cgi:error] [pid 91112] [client 127.0.0.1:64109] AH01215: dyld: Symbol not found: _sqlite3_bind_blob64: /Applications/XAMPP/xamppfiles/cgi-bin/dart.dart
[Sun Feb 23 14:08:12.800332 2020] [cgi:error] [pid 91112] [client 127.0.0.1:64109] AH01215: Referenced from: /System/Library/PrivateFrameworks/BaseBoard.framework/Versions/A/BaseBoard: /Applications/XAMPP/xamppfiles/cgi-bin/dart.dart
[Sun Feb 23 14:08:12.800432 2020] [cgi:error] [pid 91112] [client 127.0.0.1:64109] AH01215: Expected in: /Applications/XAMPP/xamppfiles/lib/libsqlite3.dylib: /Applications/XAMPP/xamppfiles/cgi-bin/dart.dart
[Sun Feb 23 14:08:12.800529 2020] [cgi:error] [pid 91112] [client 127.0.0.1:64109] AH01215: in /System/Library/PrivateFrameworks/BaseBoard.framework/Versions/A/BaseBoard: /Applications/XAMPP/xamppfiles/cgi-bin/dart.dart
[Sun Feb 23 14:08:12.800581 2020] [cgi:error] [pid 91112] [client 127.0.0.1:64109] End of script output before headers: dart.dart
I used these commands to update the libraries:
ln -s `which dart` /usr/local/bin/dart
sudo cp /System/Library/Frameworks/ImageIO.framework//Versions/A/Resources/libJPEG.dylib /Applications/XAMPP/xamppfiles/lib/libJPEG.dylib
sudo cp /System/Library/Frameworks/ImageIO.framework//Versions/A/Resources/libTIFF.dylib /Applications/XAMPP/xamppfiles/lib/
sudo cp /System/Library/Frameworks/ImageIO.framework//Versions/A/Resources/libPng.dylib /Applications/XAMPP/xamppfiles/lib/
sudo cp /usr/lib/libsqlite3.dylib /Applications/XAMPP/xamppfiles/lib/
Take a look on result using QueryString and Post:
Do not forget to set the correct path to dart executable on the first line of script. I usually link it to /usr/local/bin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With