Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import vibe.appmain;
- import vibe.core.file;
- import vibe.core.log;
- import vibe.http.router;
- import vibe.http.server;
- import vibe.core.core;
- import vibe.core.concurrency;
- import std.datetime;
- import std.exception;
- void uploadFile(scope HTTPServerRequest req, scope HTTPServerResponse res)
- {
- auto pf = "file" in req.files;
- enforce(pf !is null, "No file uploaded!");
- try moveFile(pf.tempPath, Path(".") ~ pf.filename);
- catch (Exception e) {
- logWarn("Failed to move file to destination folder: %s", e.msg);
- logInfo("Performing copy+delete instead.");
- copyFile(pf.tempPath, Path(".") ~ pf.filename);
- }
- auto worker = runWorkerTaskH(&someWork, Task.getThis);
- receive((string msg) { res.writeBody(msg); });
- res.writeBody("File uploaded!", "text/plain");
- }
- void someWork(Task caller)
- {
- sleep(1.seconds);
- caller.send("finished");
- }
- shared static this()
- {
- auto router = new URLRouter;
- router.get("/", staticTemplate!"upload_form.dt");
- router.post("/upload", &uploadFile);
- auto settings = new HTTPServerSettings;
- settings.port = 8080;
- settings.bindAddresses = ["::1", "127.0.0.1"];
- listenHTTP(settings, router);
- }
- int main()
- {
- import vibe.core.args : finalizeCommandLineOptions;
- import vibe.core.core : runEventLoop, lowerPrivileges;
- import vibe.core.log;
- import std.encoding : sanitize;
- version (unittest) {
- logInfo("All unit tests were successful.");
- return 0;
- } else {
- try if (!finalizeCommandLineOptions()) return 0;
- catch (Exception e) {
- logDiagnostic("Error processing command line: %s", e.msg);
- return 1;
- }
- lowerPrivileges();
- logDiagnostic("Running event loop...");
- int status;
- version (VibeDebugCatchAll) {
- try {
- status = runEventLoop();
- } catch( Throwable th ){
- logError("Unhandled exception in event loop: %s", th.msg);
- logDiagnostic("Full exception: %s", th.toString().sanitize());
- return 1;
- }
- } else {
- status = runEventLoop();
- }
- logDiagnostic("Event loop exited with status %d.", status);
- return status;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement