-
Notifications
You must be signed in to change notification settings - Fork 26
/
server.ts
43 lines (34 loc) · 1.04 KB
/
server.ts
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
import express, { Request, Response, NextFunction } from "express"
export interface BattlesnakeHandlers {
info: Function;
start: Function;
move: Function;
end: Function;
}
export default function runServer(handlers: BattlesnakeHandlers) {
const app = express();
app.use(express.json());
app.get("/", (req: Request, res: Response) => {
res.send(handlers.info());
});
app.post("/start", (req: Request, res: Response) => {
handlers.start(req.body);
res.send("ok");
});
app.post("/move", (req: Request, res: Response) => {
res.send(handlers.move(req.body));
});
app.post("/end", (req: Request, res: Response) => {
handlers.end(req.body);
res.send("ok");
});
app.use(function(req: Request, res: Response, next: NextFunction) {
res.set("Server", "battlesnake/github/starter-snake-typescript");
next();
});
const host = '0.0.0.0';
const port = parseInt(process.env.PORT || '8000');
app.listen(port, host, () => {
console.log(`Running Battlesnake at http://${host}:${port}...`);
});
}