Advertisement
Guest User

Untitled

a guest
Sep 10th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.05 KB | None | 0 0
  1. import vibe.appmain;
  2. import vibe.core.file;
  3. import vibe.core.log;
  4. import vibe.http.router;
  5. import vibe.http.server;
  6. import vibe.core.core;
  7. import vibe.core.concurrency;
  8. import std.datetime;
  9.  
  10. import std.exception;
  11.  
  12.  
  13. void uploadFile(scope HTTPServerRequest req, scope HTTPServerResponse res)
  14. {
  15.     auto pf = "file" in req.files;
  16.     enforce(pf !is null, "No file uploaded!");
  17.     try moveFile(pf.tempPath, Path(".") ~ pf.filename);
  18.     catch (Exception e) {
  19.         logWarn("Failed to move file to destination folder: %s", e.msg);
  20.         logInfo("Performing copy+delete instead.");
  21.         copyFile(pf.tempPath, Path(".") ~ pf.filename);
  22.     }
  23.    
  24.     auto worker = runWorkerTaskH(&someWork, Task.getThis);
  25.     receive((string msg) { res.writeBody(msg); });
  26.  
  27.     res.writeBody("File uploaded!", "text/plain");
  28. }
  29.  
  30. void someWork(Task caller)
  31. {
  32.     sleep(1.seconds);
  33.    
  34.     caller.send("finished");
  35. }
  36.  
  37. shared static this()
  38. {
  39.     auto router = new URLRouter;
  40.     router.get("/", staticTemplate!"upload_form.dt");
  41.     router.post("/upload", &uploadFile);
  42.  
  43.     auto settings = new HTTPServerSettings;
  44.     settings.port = 8080;
  45.     settings.bindAddresses = ["::1", "127.0.0.1"];
  46.     listenHTTP(settings, router);
  47. }
  48.  
  49. int main()
  50. {
  51.     import vibe.core.args : finalizeCommandLineOptions;
  52.     import vibe.core.core : runEventLoop, lowerPrivileges;
  53.     import vibe.core.log;
  54.     import std.encoding : sanitize;
  55.  
  56.     version (unittest) {
  57.         logInfo("All unit tests were successful.");
  58.         return 0;
  59.     } else {
  60.         try if (!finalizeCommandLineOptions()) return 0;
  61.         catch (Exception e) {
  62.             logDiagnostic("Error processing command line: %s", e.msg);
  63.             return 1;
  64.         }
  65.  
  66.         lowerPrivileges();
  67.  
  68.         logDiagnostic("Running event loop...");
  69.         int status;
  70.         version (VibeDebugCatchAll) {
  71.             try {
  72.                 status = runEventLoop();
  73.             } catch( Throwable th ){
  74.                 logError("Unhandled exception in event loop: %s", th.msg);
  75.                 logDiagnostic("Full exception: %s", th.toString().sanitize());
  76.                 return 1;
  77.             }
  78.         } else {
  79.             status = runEventLoop();
  80.         }
  81.  
  82.         logDiagnostic("Event loop exited with status %d.", status);
  83.         return status;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement