[+] Dockerize
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
FROM python:3.13-slim
|
||||
|
||||
# Install dependencies: Fastapi and uvicorn
|
||||
RUN pip install fastapi uvicorn
|
||||
|
||||
# Copy ./server to /app
|
||||
COPY ./server /app
|
||||
WORKDIR /app
|
||||
|
||||
# Run the server
|
||||
CMD ["python", "host.py"]
|
||||
@@ -0,0 +1,9 @@
|
||||
name: Colorful Link
|
||||
services:
|
||||
slither:
|
||||
build: .
|
||||
container_name: slither
|
||||
ports:
|
||||
- 51562:51562
|
||||
volumes:
|
||||
- ./server/data:/app/data
|
||||
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
import random
|
||||
import tempfile
|
||||
from fastapi.responses import RedirectResponse
|
||||
import uvicorn
|
||||
from subprocess import check_output
|
||||
@@ -65,6 +66,22 @@ async def get_puzzle(id: str):
|
||||
return json.loads(tf.read_text())
|
||||
|
||||
|
||||
@app.post("/solve")
|
||||
async def input_request(request: Request):
|
||||
body = (await request.body()).decode().replace("\r\n", "\n")
|
||||
|
||||
# Write body to a file
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
tf = Path(tmpdir) / "puzzle.slk"
|
||||
tf.write_text(body)
|
||||
|
||||
# Run the solver (example: ./slsolver mypuzzle.slk)
|
||||
# Solver is https://github.com/davidjosepha/slitherlink
|
||||
solver_path = src / "slsolver"
|
||||
output = check_output([solver_path, str(tf)], cwd=src).decode()
|
||||
return output
|
||||
|
||||
|
||||
# Getting / redirects to main page
|
||||
@app.get("/")
|
||||
async def get_main():
|
||||
|
||||
Executable
BIN
Binary file not shown.
@@ -1,38 +0,0 @@
|
||||
import uvicorn
|
||||
from subprocess import check_output
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
src = Path(__file__).parent
|
||||
|
||||
origins = ["*"]
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.post("/")
|
||||
async def input_request(request: Request):
|
||||
body = (await request.body()).decode().replace("\r\n", "\n")
|
||||
|
||||
# Write body to a file
|
||||
tf = src / "input.txt"
|
||||
tf.write_text(body)
|
||||
|
||||
# Run the solver (example: ./slsolver mypuzzle.slk)
|
||||
# Solver is https://github.com/davidjosepha/slitherlink
|
||||
solver_path = src / "slsolver"
|
||||
output = check_output([solver_path, str(tf)], cwd=src).decode()
|
||||
return output
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
Reference in New Issue
Block a user