/
app.d
38 lines (31 loc) · 976 Bytes
/
app.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module app;
import vibe.core.core;
import vibe.core.file;
import vibe.core.log;
import vibe.core.path;
import vibe.http.router;
import vibe.http.server;
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, NativePath(".") ~ pf.filename);
catch (Exception e) {
logWarn("Failed to move file to destination folder: %s", e.msg);
logInfo("Performing copy+delete instead.");
copyFile(pf.tempPath, NativePath(".") ~ pf.filename);
}
res.writeBody("File uploaded!", "text/plain");
}
int main(string[] args)
{
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"];
auto listener = listenHTTP(settings, router);
return runApplication(&args);
}