Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dmitryolshansky/photon-http
Http library for Photon framework or to be used standalone
https://github.com/dmitryolshansky/photon-http
Last synced: 1 day ago
JSON representation
Http library for Photon framework or to be used standalone
- Host: GitHub
- URL: https://github.com/dmitryolshansky/photon-http
- Owner: DmitryOlshansky
- License: bsl-1.0
- Created: 2023-06-22T09:23:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-18T13:50:23.000Z (6 days ago)
- Last Synced: 2025-01-23T00:46:35.142Z (2 days ago)
- Language: D
- Size: 40 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Photon.http
An HTTP library developed for use with [Photon](https://github.com/DmitryOlshansky/photon) fiber scheduler but can be used as a standalone parser.
## Build
Use the basic flow of [DUB](https://dub.pm/getting-started/first-steps/#building-a-third-party-project).
To build release specifically use the following command:
```
dub build -b release
```
## ExampleAs a simple example here is a static hello world HTTP server running on std.socket/photon:
```d
#!/usr/bin/env dub
/+ dub.json:
{
"name" : "hello",
"dependencies": {
"photon": "~>0.10.2",
"photon-http": "~>0.5.1"
}
}
+/
import std.stdio;
import std.socket;import photon, photon.http;
class HelloWorldProcessor : HttpProcessor {
HttpHeader[] headers = [HttpHeader("Content-Type", "text/plain; charset=utf-8")];this(Socket sock){ super(sock); }
override void handle(HttpRequest req) {
respondWith("Hello, world!", 200, headers);
}
}void server_worker(Socket client) {
scope processor = new HelloWorldProcessor(client);
try {
processor.run();
}
catch(Exception e) {
stderr.writeln(e);
}
}void server() {
Socket server = new TcpSocket();
server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
server.bind(new InternetAddress("0.0.0.0", 8080));
server.listen(1000);debug writeln("Started server");
void processClient(Socket client) {
go(() => server_worker(client));
}while(true) {
try {
debug writeln("Waiting for server.accept()");
Socket client = server.accept();
debug writeln("New client accepted");
processClient(client);
}
catch(Exception e) {
writefln("Failure to accept %s", e);
}
}
}void main() {
startloop();
go(() => server());
runFibers();
}```
And here is the same but running on threads and std.socket without Photon:
```d
#!/usr/bin/env dub
/+ dub.json:
{
"name" : "hello",
"dependencies": {
"photon-http": "~>0.4.5"
}
}
+/
import std.stdio;
import std.socket;
import core.thread;import photon.http;
class HelloWorldProcessor : HttpProcessor {
HttpHeader[] headers = [HttpHeader("Content-Type", "text/plain; charset=utf-8")];this(Socket sock){ super(sock); }
override void handle(HttpRequest req) {
respondWith("Hello, world!", 200, headers);
}
}void server_worker(Socket client) {
scope processor = new HelloWorldProcessor(client);
try {
processor.run();
}
catch(Exception e) {
stderr.writeln(e);
}
}void server() {
Socket server = new TcpSocket();
server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
server.bind(new InternetAddress("0.0.0.0", 8080));
server.listen(1000);debug writeln("Started server");
void processClient(Socket client) {
new Thread(() => server_worker(client)).start();
}while(true) {
try {
debug writeln("Waiting for server.accept()");
Socket client = server.accept();
debug writeln("New client accepted");
processClient(client);
}
catch(Exception e) {
writefln("Failure to accept %s", e);
}
}
}void main() {
new Thread(() => server()).start();
}```