-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.pt
More file actions
71 lines (57 loc) · 1.9 KB
/
Copy pathserver.pt
File metadata and controls
71 lines (57 loc) · 1.9 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// PT Web Demo Server
// Run locally: ./pt server.pt
// Open: http://localhost:3000
let homePage = readFile("docs/index.html");
let docsPage = readFile("docs/docs.html");
let playgroundPage = readFile("docs/playground.html");
let styleCSS = readFile("docs/style.css");
show("Loading pages...");
if (homePage is nil) { show("ERROR: Could not load index.html"); return; }
show("Pages loaded successfully!");
let port = 3000;
let envPort = getenv("PORT");
if (envPort isnt "nil") {
port = toNum(envPort);
}
httpListen(port, (req) => {
let path = req.path;
// --- Static files ---
if (path is "/style.css") {
return {status: 200, headers: {"content-type": "text/css"}, body: styleCSS};
}
// --- Pages ---
if (path is "/") {
return homePage;
}
if (path is "/docs") {
return docsPage;
}
if (path is "/playground") {
return playgroundPage;
}
// --- Dynamic routes ---
if (path is "/greet/World") {
return "<h1>Hello, World!</h1><p>Welcome to PT.</p><a href='/'>Home</a>";
}
if (path is "/greet/PT") {
return "<h1>Hello, PT!</h1><p>The language that serves itself.</p><a href='/'>Home</a>";
}
// --- API ---
if (path is "/api/stats") {
let stats = {
language: "PT",
version: "v12",
features: {"classes": true, "match": true, "httpServer": true, "database": true, "closures": true, "bytecode": true},
uptime: toString(clock())
};
return {status: 200, headers: {"content-type": "application/json"}, body: toString(stats)};
}
if (path is "/api/hello") {
return {status: 200, headers: {"content-type": "application/json"}, body: "{\"message\": \"Hello from PT!\", \"language\": \"PT\"}"};
}
if (path is "/api/error") {
return {status: 500, body: "<h1>500 - Server Error</h1><p>Something went wrong!</p>"};
}
// --- 404 ---
return {status: 404, body: "<h1>404 - Not Found</h1><p>Page not found.</p><a href='/'>Home</a>"};
});