I’ve now been running the DWScript Sample Web Server for 8 months in a metered host (Ikoula’s 1€/month VM), so it’s time for a more formal introduction article.
It served web pages and JSON dynamically generated by DWScript, using an SQLite database for storage. It ran on a Windows 2008 R2 Standard installation, minimized so that OS+Server stayed around 256 MB RAM.
I didn’t mention it much before I grew confident that, provided you’re careful, you can indeed run a Windows-based, high-performance Pascal-based web server at a very low cost, even lower than most Linux-based VPS and with a higher performance than the cheaper Linux options.
DWScript Web Server
The DWScript Sample Web Server source can be found in the Demos\HttpSys2WebServer folder of the DWScript source code.
- It makes use of the SimpleServer library, which leverages an HTTP.SYS 2.0 server (based on Synopse mORMot).
- It serves pages through the TdwsHTMLFilter component, which allows to intermingle Pascal with HTML/CSS/JS in a way similar to what PHP does.
- It implements a multi-threaded compiled script cache, which is automatically updated when website source files are changed.
- It has multiple options to limit CPU usage, bandwidth, etc. which help make it safe to run in a metered environment (where you want to cap your costs) or in a shared environment (where you don’t want a service using all available resources)
The executable can run both as an application and a windows service. Though it can be used on its own merits, it’s more intended as a starting point/demonstrator.
The HTTP Server API is is the foundation upon which IIS or WCF are built, it’s a low-level, high-performance server layer that allows multiple services to share the same http port. Security-wise, for the wild web it handles SSL, and for intranets, it can handle NTLM and Kerberos domain authentication.
What this means in practice is that you can use the DWS Web Server to handle just a portion of traffic of a website, leaving the rest to IIS, WCF services, or other HTTP.SYS-capable servers like mORMot. On an Entreprise network, it means you benefit from Windows Domain Single-Sign On for all three major browsers (Chrome, FireFox, and Internet Explorer).
To use http.sys, there are some restriction which involve administrative rights at least at some point. To run the server executable you need to ensure at least one of the following:
- run it as an application with administrator rights
- run it as a service (you need administrator rights to install the service)
- run it as an application as a normal user (after having registered the url acl)
For the last option, you’ll need to use the netsh http add urlacl command. To enable SSL, you’ll need to register your certificate (for testing purposes, you can get a free SSL certificate from StartSSL).
You can also use HttpSysConfig or HttpSysManager, if you prefer GUI tools. They are very basic, but I’ve found you usually won’t need to do much in the way of http.sys administration.
Though it has ‘html’ in its name, the filter can be used for generating just anything really (css, js, json, xml, or even Pascal). In the DWS Web Server, it’s paired with the WebEnvironment module, which if still limited, provides access to HTTP request and response details.
It’s named “filter” because it works by turning a “.dws” file into a Pascal program, which will then be executed. For instance the following “.dws” source
<h1><?pas=FormatDateTime("yyyy-mm-dd", Now)?></h1> <p><?pas for var i := 1 to 3 do PrintLn(i*10); ?></p>
would be filtered to the following Pascal source
Print('<h1>'); Print(FormatDateTime("yyyy-mm-dd", Now); Print('</h1>'#13#10'<p>'); for var i := 1 to 3 do PrintLn(i*10); Print('</p>');
which if executed on the 13th of August 2013 would generate
<h1>2013-08-13</h1> <p>10 20 30 </p>
So to summarize <?pas ?> delimiters encapsulates Pascal statements, and <?pas= ?> encapsulate a Pascal expression. The delimiters can be adjusted in the TdwsHTMLFilter component, and besides being powerful, the whole filtering+script combination is very competitive performance-wise.
The TdwsHtmlFilter got various performance enhancement, but mostly dates back to the early days of DWScript, so it’s a kinda the original essence of DWScript programming.
You can easily make a syntax highlighting editor for .dws files with SynEdit, use a TSynMultiSyn component to combine the HTML highlighter with the DWS highlighter (and probably the JS and CSS highlighter as well, if you want a complete solution).
In the sample web server, pages (aka scripts) are compiled only once, then executed multiple times, with parallel executions of the same page being possible. The engine is coupled with http.sys through IOCP.
A TdwsFileNotifier (which also uses IOCP) watches the website directory and invalidates compiled scripts when necessary, this means you can edit the .dws & .pas files, and just hit F5 in your browser to see the result.
While these provide a lot of capability, you can and should of course use your own modules as well to expose business or other functionality to your scripts.
Since DWS is under a sand-boxed model, script users will only be able to access what functionality you expose them. The engine is also aiming at safeguarding for issues that might plague ISAPI DLLs or native Delphi-based servers, such as dangling pointers, memory leaks or infinite loops.
8 thoughts on “DWScript WebServer in the metered cloud”
I like too much DWS and it’s modern language. But in my opinion you need to center more on the WebServer than in DWS. Delphi failed in the web arena and this project can be converted in the Delphi “defacto” technology for web/webapi easily.
You have all the pieces. It’s your choice.
Francisco is right. This is exciting.
DWS for websites would be awesome!
Eric, this is awesome! People know PHP must find the tags familiar.
A great modern Pascal language, and a ready-to-use web server, what’s left?
Yes! A code editor with good code-complete support for *.DWS files!
Anybody/company working on that? Do you think my new project which is in beta has potential? Anybody interested?
Cool. Cool. Really cool. I have purchased ikoula but experience some issues with the setup. This address I can send a private message to ask you a dull question about setting up this environment?
@Gad you can probably post here, that way others will see the answers (assuming I have them), or better on StackOverflow. When (if) I get time, I’m planning to wipe a VM a write a quick tutorial on setting up Win 2008 to be low-memory and then installing DWS Web Server. But that’s for later, so you might as well ask away!
I’m very interrested in your quick tutorial on setting up Win2008.
It would meat the requirements of mORMot servers, also.
Can’t wait for it, even a small fact list! 🙂
Eric, that’s really great!
I’m Also would like to see your tutorial on setting up Win2008!
This manual may inspire me to finally start using dws and Smart!
Comments are closed.