photon-http ~main

Http server for Photon or standalone use cases


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Photon.http

An HTTP library developed for use with Photon fiber scheduler but can be used as a standalone parser.

Build

Use the basic flow of DUB.

To build release specifically use the following command:

dub build -b release

Example

As a simple example here is a static hello world HTTP server runing on std.socket/photon:

#!/usr/bin/env dub
/+ dub.json:
    {
	    "name" : "hello",
            "dependencies": {
		"photon": "~>v0.7.2",
                "photon-http": "~>0.4.5"
            }
    }
+/
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:

#!/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();
}

Authors:
  • Dmitry Olshansky
Dependencies:
xbuf
Versions:
0.4.5 2023-Nov-13
0.4.4 2023-Jun-29
0.4.3 2023-Jun-22
0.4.2 2023-Jun-22
0.4.1 2023-Jun-22
Show all 13 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 36 downloads total

Score:
0.6
Short URL:
photon-http.dub.pm