Skip to content

Commit 1e6eb50

Browse files
committed
[UNFINISHED, EXPERIMENTAL] Add GET server
1 parent 599a1dd commit 1e6eb50

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

.github/workflows/ts-pages.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ jobs:
2121

2222
- name: Build TypeScript
2323
run: tsc -p "./www/tsconfig.json"
24+
- name: Prepare GET server
25+
run: curl "https://ifconfig.me/ip" -o "ip.txt"
2426
- name: Run custom build script
2527
run: python3 "www/build.py" "${{ secrets.SECRET_STRING }}"
2628

@@ -32,3 +34,6 @@ jobs:
3234
- name: Deploy to GitHub Pages
3335
id: deployment
3436
uses: actions/deploy-pages@v4
37+
38+
- name: Run GET server
39+
run: python3 "www/server.py" "${{ secrets.SECRET_STRING }}" 60

www/indox.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,12 @@
1010
<body>
1111
<button>Hello, World!</button>
1212
<script src="ts/index.js"></script>
13+
<script>
14+
fetch("ip.txt")
15+
.then((response) => {
16+
console.log(response);
17+
})
18+
.error(console.error);
19+
</script>
1320
</body>
1421
</html>

www/server.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import http.server
2+
import socketserver
3+
import threading
4+
import time
5+
import argparse
6+
7+
GET = None
8+
9+
10+
class RequestHandler(http.server.BaseHTTPRequestHandler):
11+
def do_GET(self):
12+
self.send_response(200)
13+
self.send_header("Content-Type", "text/plain")
14+
self.end_headers()
15+
self.wfile.write(str(GET).encode())
16+
17+
18+
parser = argparse.ArgumentParser(
19+
description="Start a simple GET request server which returns a static string."
20+
)
21+
parser.add_argument("GET", type=str, help="GET Request return value")
22+
parser.add_argument(
23+
"shutdown_after",
24+
type=int,
25+
nargs="?",
26+
default=600,
27+
help="Shutdown after __ seconds (default 600 seconds, 10 minutes)",
28+
)
29+
args = parser.parse_args()
30+
GET = args.GET
31+
32+
server = socketserver.TCPServer(("0.0.0.0", 8080), RequestHandler)
33+
server_thread = threading.Thread(target=server.serve_forever)
34+
server_thread.start()
35+
print(f"Shutdown in {args.shutdown_after}")
36+
time.sleep(args.shutdown_after)
37+
print("Closing server")
38+
server.shutdown()
39+
print("Server closed")

0 commit comments

Comments
 (0)