After seeing Kirill Belov's impressive CSockets paclet, I wondered whether it would be possible to make something similar in entirely top-level Wolfram Language code using the new Foreign Function Interface (FFI).
The result is is WebServer paclet, which lets you host a simple web server from Wolfram Language, and which was written entirely in the top-level. It still has some quirks which I will discuss at the end, but first here is a quick demo.

Making a simple web server

The first step is to install and load the paclet:
In[]:=
PacletInstall["ChristopherWolfram/WebServer"]
Out[]=
PacletObject
Name: ChristopherWolfram/WebServer
Version: 1.0.0

In[]:=
Needs["ChristopherWolfram`WebServer`"]
Now we are ready to start a simple web sever. This one just returns the text "Hello, World!" on the port 3000:
StartWebServer["Hello, World!",3000]
One of the "quirks" of StartWebServer is that it currently is single threaded, and runs in the same thread as the Wolfram Language kernel. That means that the kernel is blocked while the web server is running.

Making a more complex web server

StartWebServer uses
GenerateHTTPResponse
to respond to requests. That means it automatically supports anything that you might put in a CloudDeploy including APIFunction, FormPage, URLDispatcher, etc.
StartWebServer[​​URLDispatcher[{​​"/api"APIFunction["x""String"],​​"/form"FormFunction["x""String"],​​"/image"->Delayed[RandomImage[1,{1000,1000},ColorSpace"RGB"]]​​}],​​3000​​]

Viewing requests

Taking advantage of the way that the web server is running in the main kernel and how it supports constructs like Delayed, it is possible to easily create a web server that logs information about requests into the kernel:
In[]:=
StartWebServer[Delayed[Echo[HTTPRequest@HTTPRequestData[]];"Hello, World!"],3010]
»
HTTPRequest
URL:
http:///test/dir?arg=value
ContentType: None

»
HTTPRequest
URL:
http:///test/dir?arg=value
ContentType: None

»
HTTPRequest
URL:
http:///
ContentType: None

»
HTTPRequest
URL:
http:///test/dir?arg=value
ContentType: None

»
HTTPRequest
URL:
http:///
ContentType: None


Implementation

The WebServer paclet is written entirely in Wolfram Language. The source can be viewed in GitHub at https://github.com/chriswolfram/WebServer/tree/master/WebServer/Kernel

Quirks

As mentioned above, there are still a few quirks with this paclet:
◼
  • There are currently some problems around aborting a web server, so right now StartWebServer essentially takes over the current kernel.
  • ◼
  • Current builds of the uWebSockets library that WebServer depends on are not code signed on macOS. This means that the first time the paclet is loaded on macOS, the "unidentified developer" dialog will pop up and will only be satisfied by going into preferences and allowing the library to be loaded.
  • CITE THIS NOTEBOOK

    A WebServer in top-level Wolfram Language with Foreign Function Interface​
    by Christopher Wolfram​
    Wolfram Community, STAFF PICKS, September 5, 2023
    ​https://community.wolfram.com/groups/-/m/t/3006142